function test(txt){ this.key=['red','green','blue'] this.newArray=txt //this.key.push(txt)} function test1(){ test.apply(this,['yellow','orgen']) }var fun=new test1()fun.newArray=====>"yellow"
传入的是一个数组test里通过txt拿到的却是个字符串"yellow"
但是将apply换成call
function test(txt){ this.key=['red','green','blue'] this.newArray=txt //this.key.push(txt)} function test1(){ test.call(this,['yellow','orgen']) }var fun=new test1()fun.newArray=====>["yellow", "orgen"]
得到的却是["yellow", "orgen"]
如果放开this.key.push(txt)在构造函数内部拼接数组最后得到的是["red", "green", "blue", "yellow"](apply换成call也一样)
但是将push放到函数外在实例引用时拼接便能拿到两个数组拼接到一起的情况
function test(txt){ this.key=['red','green','blue'] this.newArray=txt //this.key.push(txt)} function test1(){ test.call(this,['yellow','orgen']) }var fun=new test1()fun.key.concat(fun.newArray)=====>["red", "green", "blue", "yellow", "orgen"]
目的在构造函数内部完成数组正确拼接
相关分类