call

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

Zawartość wersji

{{template.Dopracuj()}}

Summary

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

Method of Function
Implemented in: JavaScript 1.3
ECMA Version: ECMA-262 Edition 3

Syntax

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

Parameters

thisArg 
Parameter for the calling object.
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 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");

See Also

apply

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

Źródło wersji

<p>
{{template.Dopracuj()}}
</p>
<h3 name="Summary"> Summary </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">Method of <a href="pl/Core_JavaScript_1.5_Reference/Objects/Function">Function</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.3</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262 Edition 3</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
call(<i>thisArg</i>[, <i>arg1</i>[, <i>arg2</i>{{mediawiki.external(', ...')}}]])
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>thisArg</code> </dt><dd> Parameter for the calling object.
</dd></dl>
<dl><dt> <code>arg1, arg2, ...</code> </dt><dd> Arguments for the object.
</dd></dl>
<h3 name="Description"> Description </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="Examples"> Examples </h3>
<h4 name="Example:_Using_call_to_chain_constructors_for_an_object"> Example: Using <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="See_Also"> See Also </h3>
<p><a href="pl/Core_JavaScript_1.5_Reference/Objects/Function/apply">apply</a>
</p>{{ wiki.languages( { "en": "en/Core_JavaScript_1.5_Reference/Global_Objects/Function/call" } ) }}
Przywróć do tej wersji