寄生构造函数拓展功能问题

function SpecialArray(){
var values=new Array();
values.push.apply(values,arguments);
values.toPipedString=function(){
return this.join("|");
};
return values;
}
var colors=new SpecialArray("red","blue","green");
console.log(colors.toPipedString();
 
注:values.push.apply(values,arguments)的理解(用构造函数接收所有参数)
相当于
  for(let i=0;i<arguments.length;i++){
    values.push(arguments[i]);
  }

猜你喜欢

转载自www.cnblogs.com/huanbaby/p/9777030.html
今日推荐