-1

试图分析这段代码,但不确定它的作用?这如何演示函数在 JS 中的工作方式?对不起,新手的问题。使困惑?谢谢。

function merge(root) {
    for (var i = 1; i < arguments.length; i++) {
        for (var key in arguments[i]) {
            root[key] = arguments[i][key];
        }
    }
}

var merged = merge(
    {name: "Batou"},
    {city: "Niihama"},
    (activity: "Weights", min: 0, max: 35, points: 2500, scale: "sum"});

assert(merged.name === "Batou",
    "The original name is intact.");
assert(merged.max === 35,
    "The maximum number of sets is 35.");
4

5 回答 5

2

首先,修复三个(语法)错误:

  • 第2行:I小写i
  • 第 6 行:你想return root;从函数中使它工作,否则undefined将被分配给merged
  • 第 12 行:({.

除此之外,代码非常简单。该函数 merge采用任意数量的参数。它从索引 1迭代arguments对象(即不包括root参数),并为每个项目枚举其属性,将它们的值复制root对象。括号语法用于通过名称访问属性值。

因此,将所有传递的对象merge 合并到第一个对象中,覆盖已经存在的键。

于 2012-12-06T18:42:53.817 回答
0

这会将两个或多个对象合并为一个。

{name: "Batou"}

是一个快捷方式:

var obj = new Object();
obj.name = "Batou";

函数内部arguments是调用时传递的参数数组,您可以像在数组中一样更改对象的属性,但使用字符串:

obj["name"] = "Batou";

assert关键字将检查条件是否为真,它用于调试建议。

于 2012-12-06T18:40:26.613 回答
0

Javascript 有一个 Object 被调用arguments,它包含所有参数,一个函数被调用。

这个

function merge(root) {
    for (var i = 1; I < arguments.length; i++) {
        for (var key in arguments[i]) {
            root[key] = arguments[i][key];
        }
    }
}

基本上接受多个参数并将其他参数的所有属性(从第二个参数开始)放入传递给函数的第一个对象

请讲 :

这是传递给函数的第一个参数: {name: "Batou"}

然后它遍历其他 2 个对象的属性

{city: "Niihama"},
(activity: "Weights", min: 0, max: 35, points: 2500, scale: "sum"});

并将它们的属性添加到第一个属性中,结果是

{name: "Batou",
city: "Niihama",
activity: "Weights", min: 0, max: 35, points: 2500, scale: "sum"}
于 2012-12-06T18:42:55.453 回答
0

首先,我在您的代码中看到 3 个错误:大写I应该是i; (activity:应该是{activity:;功能应该return root

如果你解决了这个问题,该函数应该将传递给它的所有对象合并为一个对象。您正在传递 3 个对象:

  • {name: "Batou"}
  • {city: "Niihama"}
  • {activity: "Weights", min: 0, max: 35, points: 2500, scale: "sum"}

给定这 3 个对象作为输入,该函数返回一个合并对象:

{
    name: "Batou",
    city: "Niihama",
    activity: "Weights", 
    min: 0,
    max: 35, 
    points: 2500, 
    scale: "sum"
}
于 2012-12-06T18:41:59.383 回答
0

为了使代码按所写的那样工作,需要对其进行修改:

function merge(root) {
    for (var i = 1; i < arguments.length; i++) {
        for (var key in arguments[i]) {
            root[key] = arguments[i][key];
        }
    }
    return root; // without this line, "merged" below will always be undefined. 
}

var merged = merge(
    {name: "Batou"},
    {city: "Niihama"},
    {activity: "Weights", min: 0, max: 35, points: 2500, scale: "sum"});

assert(merged.name === "Batou",
    "The original name is intact.");
assert(merged.max === 35,
    "The maximum number of sets is 35.");

或者,它需要以不同的方式使用:

function merge(root) {
    for (var i = 1; i < arguments.length; i++) {
        for (var key in arguments[i]) {
            root[key] = arguments[i][key];
        }
    }
}

var merged = {name: "Batou"};
merge(
    merged,
    {city: "Niihama"},
    {activity: "Weights", min: 0, max: 35, points: 2500, scale: "sum"});

assert(merged.name === "Batou",
    "The original name is intact.");
assert(merged.max === 35,
    "The maximum number of sets is 35.");
于 2012-12-06T18:48:08.577 回答