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? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
以下内容参考 冴羽大神的文章
call() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数或方法。
function.call(thisArg, arg1, arg2, ...)
var foo = { value: 1 }; function bar() { console.log(this.value); } bar.call(foo); // 1
可以看到:
var foo = { value: 1, bar: function() { console.log(this.value) } }; foo.bar(); // 1
Function.prototype.callAl = function (context) { // 判断是否传入null 或者 undefined,如果是,则指向window var context = context || window; context.fn = this; var arr = []; // 遍历参数 for (var i = 1, total = arguments.length; i < total; i++) { arr.push(arguments[i]); } var result = context.fn(...args); // 删除context对象原本没有的fn函数 delete context.fn; return result; }; var value = 'I am window'; var obj = { value: 'I am obj value', }; var bar = function (name, age) { console.log(this.value); return { value: this.value, name: name, age: age, }; }; bar.callAl(null); bar.callAl(obj, 'mton', '24');
The text was updated successfully, but these errors were encountered:
No branches or pull requests
以下内容参考 冴羽大神的文章
call的定义是什么?
语法
用法
可以看到:
理解它
实现原理
The text was updated successfully, but these errors were encountered: