call

  • Ciąg znaków tej wersji: Dokumentacja_języka_JavaScript_1.5/Obiekty/Function/call
  • Tytuł wersji: call
  • Identyfikator wersji: 236384
  • Utworzono:
  • Autor: Ptak82
  • Czy jest aktualna? Nie
  • Komentarz

Zawartość wersji

{{ Dopracuj() }}

Podsumowanie

Allows you to call (execute) a method of another object in the context of a different object (the calling object).

Metoda obiektu Function
Zaimplementowane w: JavaScript 1.3
Wersja ECMA: ECMA-262 edycja 3

Składnia

call(thisArg[, arg1[, arg2{{ mediawiki.external(', ...') }}]])

Parametry

thisArg 
Parametr do wywołania obiektu.
arg1, arg2, ... 
Argumenty dla obiektu.

Opis

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.

Przykłady

Przykład: Zastosowanie 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 value. Another object, prod_dept, initializes its unique variable (dept) and calls the constructor for product in its constructor to initialize the other variables.

function product(name, value){
   this.name = name;
   if(value > 1000)
      this.value = 999;
   else
      this.value = value;
}

function prod_dept(name, value, dept){
   this.dept = dept;
   product.call(this, name, value);
}

prod_dept.prototype = new product();

// since 5 is less than 100 value is set
cheese = new prod_dept("feta", 5, "food");

// since 5000 is above 1000, value will be 999
car = new prod_dept("honda", 5000, "auto");

Zobacz także

apply

{{ languages( { "en": "en/Core_JavaScript_1.5_Reference/Global_Objects/Function/call" } ) }}

Źródło wersji

<p>
{{ Dopracuj() }}
</p>
<h3 name="Podsumowanie"> Podsumowanie </h3>
<p>Allows you to call (execute) a method of another object in the context of a different object (the calling object).
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Metoda obiektu <a href="pl/Dokumentacja_j%c4%99zyka_JavaScript_1.5/Obiekty/Function">Function</a></td>
</tr>
<tr>
<td>Zaimplementowane w:</td>
<td>JavaScript 1.3</td>
</tr>
<tr>
<td>Wersja ECMA:</td>
<td>ECMA-262 edycja 3</td>
</tr>
</tbody></table>
<h3 name="Sk.C5.82adnia"> Składnia </h3>
<p><code>
call(<i>thisArg</i>[, <i>arg1</i>[, <i>arg2</i>{{ mediawiki.external(', ...') }}]])
</code>
</p>
<h3 name="Parametry"> Parametry </h3>
<dl><dt> <code>thisArg</code> </dt><dd> Parametr do wywołania obiektu.
</dd></dl>
<dl><dt> <code>arg1, arg2, ...</code> </dt><dd> Argumenty dla obiektu. </dd></dl>
<h3 name="Opis"> Opis </h3>
<p>You can assign a different <code>this</code> object when calling an existing function. <code>this</code> refers to the current object, the calling object.
</p><p>With <code>call</code>, you can write a method once and then inherit it in another object, without having to rewrite the method for the new object. </p>
<h3 name="Przyk.C5.82ady"> Przykłady </h3>
<h4 name="Przyk.C5.82ad:_Zastosowanie_call_to_chain_constructors_for_an_object"> Przykład: Zastosowanie <code>call</code> to chain constructors for an object </h4>
<p>You can use <code>call</code> to chain constructors for an object, similar to Java. In the following example, the constructor for the product object is defined with two parameters, <code>name</code> and <code>value</code>. Another object, <code>prod_dept</code>, initializes its unique variable (<code>dept</code>) and calls the constructor for <code>product</code> in its constructor to initialize the other variables.
</p>
<pre>function product(name, value){
   this.name = name;
   if(value &gt; 1000)
      this.value = 999;
   else
      this.value = value;
}

function prod_dept(name, value, dept){
   this.dept = dept;
   product.call(this, name, value);
}

prod_dept.prototype = new product();

// since 5 is less than 100 value is set
cheese = new prod_dept("feta", 5, "food");

// since 5000 is above 1000, value will be 999
car = new prod_dept("honda", 5000, "auto");
</pre>
<h3 name="Zobacz_tak.C5.BCe"> Zobacz także </h3>
<p><code><a href="pl/Dokumentacja_j%c4%99zyka_JavaScript_1.5/Obiekty/Function/apply">apply</a></code>
</p>{{ languages( { "en": "en/Core_JavaScript_1.5_Reference/Global_Objects/Function/call" } ) }}
Przywróć do tej wersji