1、people.apply(student,arg);
把people的属性和方法给student,按照arg顺序传入参数
2、Math.max.apply(null,arr);
本来是Math.max(a,b,c,d)这种,无法直接输入一个数组,此时利用apply传参性质就可以直接传数组。
这里的null,我的理解是因为这个方法返回就是一个数字,不涉及带作用域的返回,所以用null不影响
3、Array.prototype.push.apply(arr1,arr2);
原本是arr1.push(a,b,c,d);
此时Array.prototype也可以写成任何数组,因为每个数组的原型链上都有push这个方法。
总结:A.apply(B,arg);是A把自己的方法交给B来做,这时候B就有了A的方法;第二可以使传参从散的参数变成数组。这是apply最重要的两个作用点。
4、Array.apply(null, {length:20}).map(function(){
returncreateElement('p','hi')
})