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
fun.call(thisArg, arg1, arg2, ...)
调用一个函数, 其具有一个指定的this值和分别地提供的参数(参数的列表)。
func.apply(thisArg, [argsArray])
调用一个函数,以及作为一个数组(或类似数组对象)提供的参数。
1、call、apply、bind都是用来改变函数的this指向,第一个参数都是this要指向的新对象。 2、call、apply类似,都是立即调用函数,但第二个参数后续传参格式不同,call后续传参是 ,隔开,apply第二个参数是数组格式。 3、bind是返回对应的函数,便于后续调用。
,
Function.prototype.newCall = function(context = window, ...args) { context.fn = this; let result = context.fn(...args); delete context.fn; return result; } let foo = { value: 1 } function bar(name, age) { console.log(name) console.log(age) console.log(this.value); } bar.newCall(foo, 'black', '18') // black 18 1
Function.prototype.newApply = function(context = window, argArr){ context.fn = this; let result = context.fn(...argArr); delete context.fn; return result; } function bar(name, age) { console.log(name) console.log(age) console.log(this.value); } bar.newApply(foo, ['black', 18]) // black 18 1
Function.prototype.newBind = function(context = window, ...args) { if (typeof this !== "function") { throw new Error("this must be a function"); } context.fn = this; return function(..._args) { args = args.concat(_args); context.fn(...args); delete context.fn; } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
语法
调用一个函数, 其具有一个指定的this值和分别地提供的参数(参数的列表)。
调用一个函数,以及作为一个数组(或类似数组对象)提供的参数。
call、apply、bind的功能
1、call、apply、bind都是用来改变函数的this指向,第一个参数都是this要指向的新对象。
2、call、apply类似,都是立即调用函数,但第二个参数后续传参格式不同,call后续传参是
,
隔开,apply第二个参数是数组格式。3、bind是返回对应的函数,便于后续调用。
模拟call、apply、bind
模拟call()
模拟apply()
模拟bind()
The text was updated successfully, but these errors were encountered: