We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
call() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数。
var foo = { value: 1 }; function bar() { console.log(this.value); } bar.call(foo); // 1
思考:
call 改变了 this 的指向,指向到 foo bar 函数执行了
模拟实现步骤:
将函数设为对象的属性 执行该函数 删除该函数
关键点:
函数执行可带不定长参数
解决办法
var args = []; //第一个参数是this指定对象,所以i从1开始 for(var i=1;i<arguments.length;i++){ args.push('arguments['+i+']') }
Function.prototype.call2 = function(target){ target = target || window; target.fun = this; var args = []; for(var i=1;i<arguments.length;i++){ args.push('arguments['+i+']') } var result = eval('target.fun('+args+')') delete target.fun; return result; } // 测试一下 var value = 2; var obj = { value: 1 } function bar(name, age) { console.log(this.value); return { value: this.value, name: name, age: age } } bar.call2(null); // 2 console.log(bar.call2(obj, 'manny', 18)); // 1 // Object { // value: 1, // name: 'manny', // age: 18 // }
apply() 方法调用一个具有给定this值的函数,以及作为一个数组(或类似数组对象)提供的参数。
和apply的差别就是在于参数的格式
Function.prototype.apply2 = function (target, arr) { target = target || window; target.fun = this; var result; if (!arr) { result = target.fun(); } else { var args = []; for (var i = 0, len = arr.length; i < len; i++) { args.push('arr[' + i + ']'); } result = eval('target.fun(' + args + ')') } delete target.fun return result; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
call
先看一个例子
思考:
模拟实现步骤:
关键点:
解决办法
动手实现:
apply
模拟实现
JavaScript深入之call和apply的模拟实现
The text was updated successfully, but these errors were encountered: