神奇的apply

var Cat = function(name){
  this.name = name;
}
var Animal = function{
  this.type = 'animal';  
}

问题:Cat 如何能继承Animal?利用apply

var Cat = function(name){
  Animal.apply(this,arguments);  //添加这行代码
  this.name = name;
}
var Animal = function(){
  this.type = 'animal';
}
var cat = new Cat('xinxin');
cat.type; => 'animal';Math.max.apply(null,arr);

一行代码得到数组最大or最小值:(隐患:参数数量有可能超出限制)

Math.max.apply(null,arr);

利用apply实现arr.push的时候推入一个arr;

Array.prototype.push.apply(arr1,arr2);

猜你喜欢

转载自www.cnblogs.com/liuxinxin4288/p/9032698.html
今日推荐