如何在 Javascript 对象中自动分割文本?
我想要一个对象接受一些带有分隔符的输入字符串,如“dog:cat:whale”,并希望“splittedText”内的属性是分割后输入对象的数组“spittedText [0] =狗, spittedText[1] = cat, spittedText[2] = Whale".
以下是我想要完成的通用伪代码,但不起作用...
function someObject(input) {
this.splittedText=input.split(':');
}
为了测试,我应该能够执行以下操作:
theObject = new someObject("dog:cat:whale");
alert(someObject(theObject.splittedText[0])); // should print out dog
What am我做错了吗?我该如何实现这个目标?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该再次调用该函数。
You shouldn't be calling the function again.
这对我有用:
This works for me: