2011-04-15 31 views
0

我想有接受與喜歡定界符一些輸入字符串對象「狗:貓:鯨魚」,並希望「splittedText」裏面的財產是分割後的輸入對象的數組"spittedText[0] = dog, spittedText[1] = cat, spittedText[2] = whale".如何在Javascript對象中自動分割文本?

以下是通用什麼我要完成的僞代碼,但不工作...

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

爲了測試,我應該能夠做到這一點:

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

我在做什麼錯?我該如何做到這一點?

回答

1

您不應該再次調用該函數。

alert(theObject.splittedText[0]); 
0

這個工作對我來說:

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

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

alert(someObj.splittedText);