Home > Web Front-end > JS Tutorial > Detailed explanation of the usage of call(), apply() and bind() in js

Detailed explanation of the usage of call(), apply() and bind() in js

php是最好的语言
Release: 2018-08-10 17:50:08
Original
2908 people have browsed it

I finally understand the usage of call(), apply(), and bind() in javascript

It’s actually a very simple thing. After ten minutes of careful reading, I went from being confused to fully understanding. !

First understand the following:

Example 1

Detailed explanation of the usage of call(), apply() and bind() in js

##obj.objAge; //17

obj.myFun () //Xiao Zhang’s age is undefined

Example 2


Detailed explanation of the usage of call(), apply() and bind() in js

shows() //Blind monk

Compare the difference between the two this, This in the first print points to obj, and in the second globally declared shows() function, this is window;

1, call(), apply(), and bind() are all used to redefine this This object!

For example:

Detailed explanation of the usage of call(), apply() and bind() in js

obj.myFun.call(db); //Dema’s age is 99

obj.myFun.apply( db); //Dema’s age is 99

obj.myFun.bind(db)(); //Dema’s age is 99

Above, there is an extra () after the bind method , the results returned are consistent!

It can be concluded from this that bind returns a new function, which you must call before it will be executed

2. Compare call, bind and apply parameter passing situations

 

 Detailed explanation of the usage of call(), apply() and bind() in js

obj.myFun.call(db,'Chengdu','Shanghai');   //Dema is 99 years old from Chengdu to Shanghai

obj.myFun.apply(db ,['Chengdu','Shanghai']); //Dema's age is 99. From Chengdu to Shanghai

obj.myFun.bind(db,'Chengdu','Shanghai')(); // Dema's age is 99. From Chengdu to Shanghai

obj.myFun.bind(db,['Chengdu','Shanghai'])(); //Dema's age is 99. From Chengdu, to Shanghai to undefined

Subtle difference!

It is not difficult to see from the above four results
The first parameters of the three functions call, bind, and apply are all the objects pointed to by this. The difference comes from the second parameter:
The parameters of call It is put in directly. The second, third and nth parameters are all separated by commas and placed directly at the end. obj.myFun.call(db,'Chengdu', ...,'string');
 All parameters of apply are It must be placed in an array and passed in obj.myFun.apply(db,['Chengdu', ..., 'string' ]);
Except that it returns a function, bind has the same parameters as call.
    
  Of course, the parameters of the three are not limited to string type, and are allowed to be of various types, including functions, objects, etc.!

Related recommendations:


Detailed introduction to the usage of bind, call, and apply functions in JavaScript

call, apply, in Javascript Detailed explanation of the difference between the source and contact of bind method

The above is the detailed content of Detailed explanation of the usage of call(), apply() and bind() in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template