The javascriptcall () method calls a function or method on the premise of using a specified this value and several specified parameter values. this article describes how to use the call () method, for more information about the coders, see.
call()
When using a specified
this
To call a function or method.
Call () basic syntax
fun.call(thisArg[, arg1[, arg2[, ...]]])
Call () parameters:
Parameter Name |
Parameter description |
ThisArg |
Infun Thethis Value。 Note that the specifiedthis The value is not necessarily true when the function is executed.this Value. If this function is in non-strict mode, it is specifiednull Andundefined OfThis value automatically points Global Object (window object in the browser), and the value is the original value (number, String, Boolean value)this Will point to the automatically wrapped object of the original value. |
Arg1, arg2 ,... |
The list of specified parameters. |
Call () basic functions
When calling a function, you can assign a different valuethis
Object.this
Reference the current object, that iscall
The first parameter of the method.
PassCall method,
You can borrow methods from another object, suchObject.prototype.toString.call([]),
IsArray
The object has been borrowed.Methods on the Object.
Call () instance 1: call the parent constructor using the call Method
In a sub-constructor, you can callcall
Method To implement inheritance,
SimilarJava
. In the following exampleFood
AndToy
The object instance created by the constructor will haveProduct
The name and price attributes added in the constructor,category
Attributes are defined in their constructors.
function Product(name, price) { this.name = name; this.price = price; if (price < 0) { throw RangeError('Cannot create product ' + this.name + ' with a negative price'); } return this;}function Food(name, price) { Product.call(this, name, price); this.category = 'food';}Food.prototype = Object.create(Product.prototype);Food.prototype.constructor = Food; // Reset the constructor from Product to Foodfunction Toy(name, price) { Product.call(this, name, price); this.category = 'toy';}Toy.prototype = Object.create(Product.prototype);Toy.prototype.constructor = Toy; // Reset the constructor from Product to Toyvar cheese = new Food('feta', 5);var fun = new Toy('robot', 40);
Call () instance 2:
Usecall
Method call anonymous Function
In the following examplefor
In the loop body, we create an anonymous function and then callCall method,
Use each array element as the specifiedThis value
The anonymous function is executed. The main purpose of this anonymous function is to addprint
Method, thisprint
Method to print the correct index number of each element in the array. Of course, it is not necessary to make the array elementthis
The value is passed into the anonymous function (normal parameters are acceptable) to demonstratecall
.
var animals = [ {species: 'Lion', name: 'King'}, {species: 'Whale', name: 'Fail'}];for (var i = 0; i < animals.length; i++) { (function (i) { this.print = function () { console.log('#' + i + ' ' + this.species + ': ' + this.name); } this.print(); }).call(animals[i], i);}
Call () instance 3:
Usecall
Method to call an anonymous function and specify the 'eas' of the context'
In the following examplegreet
Methodthis
The value is boundi
Object.
function greet() { var reply = [this.person, 'Is An Awesome', this.role].join(' '); console.log(reply);}var i = { person: 'Douglas Crockford', role: 'Javascript Developer'};greet.call(i); // Douglas Crockford Is An Awesome Javascript Developer
Note:Functions andapply()
The method is similar. There is only one difference:call()
The method acceptsList of several parameters, Andapply()
The method acceptsAn array containing multiple parameters.