Teiria
Teiria

Reputation: 1

How to Auto Split Text Within Javascript Object?

I want to have an object that accepts some input string with delimeters like "dog:cat:whale" and want the property inside "splittedText" to be an array of post-split input objects "spittedText[0] = dog, spittedText[1] = cat, spittedText[2] = whale".

The following is generic pseudocode of what I want to accomplish, but doesnt work...

function someObject(input) {
    this.splittedText=input.split(':');
}

To test, I should be able to do this:

theObject = new someObject("dog:cat:whale");
alert(someObject(theObject.splittedText[0])); // should print out dog

What am I doing wrong? How do I accomplish this?

Upvotes: 0

Views: 209

Answers (2)

locrizak
locrizak

Reputation: 12279

This works for me:

var someObj = new SomeObject("dog:cat:whale");

function SomeObject(str){
    this.splittedText = str.split(':');
}

alert(someObj.splittedText);

Upvotes: 0

SLaks
SLaks

Reputation: 888243

You shouldn't be calling the function again.

alert(theObject.splittedText[0]);

Upvotes: 1

Related Questions