This translation is incomplete. Please help translate this article from English.
Rezumat
Metoda call() apeleaza o functie ce primeste ca prim argument valoarea this si un numar de argumente specificate individual.
apply(), diferenta fundamentala consta in faptul ca functia call() accepta o lista de argumente, in timp ce apply() accepta un singur array de argumente..Sintaxa
fun.call(thisArg[, arg1[, arg2[, ...]]])
Parametri
-
thisArg -
Valoare
thispentru apelulfun. Este posibil ca aceasta valoare sa nu fie vizibila functiei: daca metoda este on functie in cod non-strict mode,nullsiundefinedvor fi inlocuite cu obiectul global si valorile promitive vor fi boxed. -
arg1, arg2, ... - Argumente pentru obiect.
Descriere
Poti asigna un alt obiect this atunci cand apelezi o functie existenta. this se refera la obiectul curent, cel care face apelul.
Cu ajutorul functiei call poti scrie o singura data o metoda ce poate fi mostenita de un alt obiect fara a fi nevoit sa rescrii acea metoda pentru noul obiect.
Exemple
Folosind call pentru a inlantui constructorii unui obiect
Poti folosi call pentru a inlantui constructorii unui obiect, metoda asemanatoare cu cea din Java. In exemplul urmator, constructorul pentru obiectul Product primeste doi parametri: name si price. Alte doua functii, Food and Toy apeleaza Product avand ca parametru this, name si price. Product initializeaza proprietatile name and price, amandoua functii definind category.
function Product(name, price) {
this.name = name;
this.price = price;
if (price < 0)
throw RangeError('Cannot create product
"' + 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);
Folosind call pentru a apela functii anonime
In acest exemplu creem o functie anonima si o apelam pentru orice obiect din Array cu ajutorul functiei call . Scopul acestei functii anonime este de a adauga o functie de tip print pentru fiecare obict, capabila sa printeze indexul corect pentru fiecare obiect din Array. Nu este absolut necesara specificarea obiectului prin valoarea this.
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);
}
Specificatii
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 3rd Edition. Implemented in JavaScript 1.3. | Standard | Definitia initiala. |
| 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. |
Release Candidate |
Compatibilitatea pentru Browsere
| 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) |