for (var i = 0, len = arr2.length; i < len; i += QUANTUM) {
Array.prototype.push.apply(
arr1,
arr2.slice(i, Math.min(i + QUANTUM, len) )
);
}
Math.min(1,2,0,-9)
或者
var array=[2,7,5,8,9];
Math.min.apply(null,array); or Math.min.apply(Math, array)
arguments是數(shù)組類對象,不能直接使用數(shù)組的方法驶沼,需要轉(zhuǎn)一次
Array.prototype.slice.call(arguments)
或
[ ].slice.call(arguments)
ES6則直接提供了Array.from(arguments) 或 [...arguments] 可遍歷的對象
繼承:只能繼承實(shí)例屬性,不能繼承原型屬性。復(fù)制副本也影響性能
SuperType.call(this);
new SubType()
var foo = {value:1}
var bar = function(){console.log(this.value)}
bar.call(foo) // 這給foo額外綁定了一個(gè)函數(shù)
Function.prototype.call2 = function(context){
context.fn = this;
context.fn();
delete context.fn;
}
bar.call2(foo); // 擴(kuò)展Function的方法徽惋,但是沒法給bar傳參
Function.prototype.call3 = function(context){
context = context ? Object(context) : window;
context.fn = this;
for(arguments){
argArr.push('arguments[' + i + ']')
}
val result = eval("context.fn(argArr)")
delete context.fn;
return result;
}
如果是es6的話,可以直接
let args = [...arguments].slice(1);
let result = context.fn(...args);
apply的原理座韵,同理险绘。注意參數(shù)的區(qū)別就可以了踢京。
判空 + context.fn(...arr);