JAVASCRIPT:数组构造函数是否自动具有名为arguments的变量?
因此,在下面的代码中,我想知道为什么apply方法中的“参数”会自动填充数组构造函数中的参数JAVASCRIPT:数组构造函数是否自动具有名为arguments的变量?,javascript,arrays,constructor,Javascript,Arrays,Constructor,因此,在下面的代码中,我想知道为什么apply方法中的“参数”会自动填充数组构造函数中的参数 function SpecialArray(){ var values = new Array(); values.push.apply(values, arguments); // does arguments descend automatically from the constructor? values.toPipedString = function() {
function SpecialArray(){
var values = new Array();
values.push.apply(values, arguments); // does arguments descend automatically from the constructor?
values.toPipedString = function() {
return this.join("|");
};
return values;
}
var colors = new SpecialArray("red","blue","green");
console.log(colors.toPipedString()); // prints: "red|blue|green"
这对于构造函数或数组来说并不特别
每个函数内部都可以通过这种方式访问。我认为,答案是“arguments”特殊对象属于SpecialArray函数的上下文。如果参数是在toPipedString()函数表达式的push()中调用的,那么它将引用该函数的本地上下文