返回一个对象 - javaScript

时间:2014-04-09 12:55:34

标签: javascript object return return-value

“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?"

感谢。

1 个答案:

答案 0 :(得分:4)

使用字符串对象而不是字符串值。

i = new String("hello world");