javascript - 返回一个对象 - javaScript

标签 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?"

谢谢。

最佳答案

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

i = new String("hello world");

关于javascript - 返回一个对象 - javaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22963240/

相关文章:

javascript - 在现有页面中嵌入 React JS

javascript - 在包含标签的 contenteditable div 中获取插入符号索引

mongodb - 带有嵌套对象的 Mongoose 聚合 $group

python - openerp return中如何返回多个表单 View ?

c - C 中的递归和返回语句

c# - 为什么continue放在yield return之后呢?

javascript - 当表单可能包含tinyMCE文本区域时如何检测html表单更改?

javascript - 检查并计算选中复选框的数量

javascript - 新对象或新对象()

java - 未选中的强制转换警告 : cast Object to Generic