重点是arguments 是一个类似数组的对象。正在做...
instance.init(arguments);
... 传递一个参数,它是一个包含某些参数的类数组对象。另一方面,做...
instance.init.apply(instance, arguments);
... 将类数组对象作为单独的参数传递。确实,设置instance 有点用处,因为您已经编写了它,但是如果使用.apply,您只需设置this 的值。
差异的简单示例:
function log(a, b, c) {
console.log(a, b, c);
}
function log2() {
log.apply(null, arguments); // `this` value is not meaningful here,
// it's about `arguments`
}
function log3() {
log(arguments);
}
log(1, 2, 3); // logs: 1, 2, 3
log2(1, 2, 3); // logs: 1, 2, 3
log3(1, 2, 3); // logs: <Arguments>, undefined, undefined
// where <Arguments> contains the values 1, 2, 3