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);call
Function.prototype.call();
Fun.call(thisArg,arg1,arg2);
参数:thisArg:在Fun函数运行时指定的this值 。非严格模式下,null,undefined的this自动指向window
function Product(name,price){ this.name = name; this.price = price; } function Food(name,price){ Product.call(this,name,price); } console.log(new Food('馒头',‘0.5’).name);//馒头
转载于:https://www.cnblogs.com/liuxinxin4288/p/9032698.html