Esta tradução está incompleta. Ajude atraduzir este artigo.
Introdução
O método call() invoca uma função com um dado valor this e argumentos passados individualmente.
Nota: Apesar de a sintaxe desta função ser quase idêntica à de apply(), a principal diferença é que call() aceita uma lista de argumentos, enquanto apply() aceita um único array de argumentos.
Sintaxe
fun.call(thisArg[, arg1[, arg2[, ...]]])
Parâmetros
-
thisArg -
The value of
thisprovided for the call tofun. Note thatthismay not be the actual value seen by the method: if the method is a function in non-strict mode code,nullandundefinedwill be replaced with the global object, and primitive values will be boxed. -
arg1, arg2, ... - Arguments for the object.
Description
You can assign a different this object when calling an existing function. this refers to the current object, the calling object. With call, you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.
Examples
Example: Using call to chain constructors for an object
You can use call to chain constructors for an object, similar to Java. In the following example, the constructor for the Product object is defined with two parameters, name and price. Two other functions Food and Toy invoke Product passing this and name and price. Product initializes the properties name and price, both specialized functions define the category.
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);
function Toy(name, price) {
Product.call(this, name, price);
this.category = 'toy';
}
Toy.prototype = Object.create(Product.prototype);
var cheese = new Food('feta', 5);
var fun = new Toy('robot', 40);
Example: Using call to invoke an anonymous function
In this purely constructed example, we create anonymous function and use call to invoke it on every object in an array. The main purpose of the anonymous function here is to add a print function to every object, which is able to print the right index of the object in the array. Passing the object as this value was not strictly necessary, but is done for explanatory purpose.
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);
}
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 3rd Edition. | Standard | Initial definition. Implemented in JavaScript 1.3. |
| ECMAScript 5.1 (ECMA-262) The definition of 'Function.prototype.call' in that specification. |
Standard | |
| ECMAScript 6 (ECMA-262) The definition of 'Function.prototype.call' in that specification. |
Draft |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |