【问题标题】:Returning an object - javaScript返回一个对象 - javaScript
【发布时间】:2014-05-22 16:42:12
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: javascript object return return-value


    【解决方案1】:

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

    i = new String("hello world");
    

    【讨论】:

    • 在这种情况下,alert(foo()); 可能会显示类似{ [String: 'hello world'] a: 'hello kitten', b: 'hello... Is there anybody out there?' }?
    • 谢谢先生!辉煌(而且如此之快!)。完美运行,立即实施。 (我会等几个小时才接受礼貌)。
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    相关资源
    最近更新 更多