call、apply、bind今天分享自己对于call、apply、bind新的认识,并手写一个自己的call、apply、bind。 三个方法的语法大体一样:
fnction fn() {}
fn.call(thisArg, arg1, arg2, ...)
fn.apply(thisArg, [arg1,arg2,...])
fn.bind (thisArg, arg1, arg2, ...)
call和bind的参数一样,apply的参数是一个数组(a开头,Array),call和apply返回的是fn执行的结果,bind返回的是fn的拷贝并指定 比如求一个数组的最大最小值:
var arr = [5, 6, 2, 8, 1];
console.log(Math.max.apply(Math, arr));//8
console.log(Math.min.apply 实现一个apply:
Function.prototype.myApply = function (context) {
if(context === undefined || context ==