“foo”下面的函数不起作用。我想要一种方法来获得如下所示的输出,并围绕“foo”中所示的原理进行工作。
function foo() {
var i;
i = "hello world";
i.a = "hello kitten";
i.b = "hello... Is there anybody out there?"
return i; // This doesn't work
}
这就是我想要的:
alert(foo()); // "hello world"
var bar = foo();
alert(bar.a); // "hello kitten"
alert(bar.b); // "hello... Is there anybody out there?"
谢谢。
最佳答案
使用字符串对象而不是字符串值。
i = new String("hello world");
关于javascript - 返回一个对象 - javaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22963240/