Home >Web Front-end >JS Tutorial >Detailed explanation of function call method instance usage in Javascript

Detailed explanation of function call method instance usage in Javascript

伊谢尔伦
伊谢尔伦Original
2017-07-20 14:07:264070browse

Function.prototype.call()

# The call method of a function instance can specify the pointer of this inside the function (that is, the scope in which the function is executed), Then call the function in the specified scope. And the function will be executed immediately.

Let’s look at an example to understand this passage.


 var keith = {
 rascal: 123
 };
 var rascal = 456;
 function a() {
 console.log(this.rascal);
 }
 a(); //456
 a.call(); //456
 a.call(null); //456
 a.call(undefined); //456
 a.call(this); //456
 a.call(keith); //123

In the above code, if the this keyword in the a function points to the global object, the return result is 456. It can be seen that if the call method has no parameters, or the parameters are null or undefined or this, it is equivalent to pointing to the global object. If you use the call method to point the this keyword to the keith object, that is, the scope in which the function is executed is the keith object, the return result is 123.

The call() method can pass two parameters. The first parameter specifies the pointer of this inside the function (that is, the scope in which the function is executed), and the second parameter is the parameter that needs to be passed when the function is called.


function keith(a, b) {
 console.log(a + b);
 }
keith.call(null, 1, 2); //3

The first parameter is required and can be null, undefined, this, but cannot be empty. Set to null, undefined, this indicates that the function keith is in the global scope at this time. The second parameter must be added one by one. In apply, it must be added in the form of an array.

One application of the call method is to call the native method of the object. Can also be used to convert array-like objects to arrays.


var obj = {};
 console.log(obj.hasOwnProperty('toString')); //false
 obj.hasOwnProperty = function() {
 return true;
 }
 console.log(obj.hasOwnProperty('toString')); //true
 console.log(Object.prototype.hasOwnProperty.call(obj, 'toString')); //false

In the above code, hasOwnProperty is a method inherited by the obj object. If this method is overridden, the correct result will not be obtained. The call method can solve this problem. It puts the original definition of the hasOwnProperty method on the obj object for execution, so that regardless of whether there is a method with the same name on obj, it will not affect the result. It should be noted that hasOwnProperty is a method of the native object of Object.prototype, and call is a method inherited from Function.prototype.

The above is the detailed content of Detailed explanation of function call method instance usage in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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