Apply call bind

Apply, call exists to change the direction of this inside of the function body and change the scope of the function.

The function People () {}

People. The prototype = {

Age = 18,

SayAge = function () {

Return this. The age

}

Let me = new People()

Me. SayAge () / / 18

Let you = {age: 22}

Me. SayAge. Apply (you) / / 22

Me. SayAge. Call (you) / / 22

You can see that apply, the function of call is the same, all of them change this direction dynamically, but they accept parameters differently

Apply (this, (arg1, arg2, arg3))

Apply the parameter as an array

Call (this, arg1, arg2, arg3)

The call parameters are passed in order

Bind the explanation was the MDN: bind () method to create a new function, when invoked, the this keyword is set to provide value, when a new function called, before any offer provide the parameter of a given sequence.

Fun. Bind (thisArg [, arg1, arg2 [[,...]]])

When the binding function is called, this parameter will be used as this point when the original function is running. When the binding function is called with the new operator, the parameter is invalid.

Arg1, arg2... When the binding function is invoked, the parameters are passed to the bound method before being placed in the argument

Apply, call is an immediate function, and bind is when you change the context and not execute immediately, but when the callback is executed.

Knock on the blackboard and sum up

Apply, call, bind are all points to which this object is used to change the function

The first parameter of apply, call, bind is the object to be pointed to, which is to specify the top or bottom

wen

Apply, call, bind all can use the following parameter to pass the reference

Bind is to return the corresponding function for later invocation; Apply, call, is an immediate call

Today, xiao shifu took a class and asked for the maximum minimum value of the array

The es6 solution math.max (... [1,2,3] // 3 use... Array deconstruction

Math.max. Apply (null, [1,2,3]) // 3 is magic

Math. Max () [1, 2, 3] / / NaN

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容