开发者

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])); 开发者_Python百科// should print out dog

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


You shouldn't be calling the function again.

alert(theObject.splittedText[0]);


This works for me:

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

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

alert(someObj.splittedText);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜