aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/global_objects/function/call
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
commit218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch)
treea9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/zh-tw/web/javascript/reference/global_objects/function/call
parent074785cea106179cb3305637055ab0a009ca74f2 (diff)
downloadtranslated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip
initial commit
Diffstat (limited to 'files/zh-tw/web/javascript/reference/global_objects/function/call')
-rw-r--r--files/zh-tw/web/javascript/reference/global_objects/function/call/index.html105
1 files changed, 105 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/global_objects/function/call/index.html b/files/zh-tw/web/javascript/reference/global_objects/function/call/index.html
new file mode 100644
index 0000000000..1d1d2017ee
--- /dev/null
+++ b/files/zh-tw/web/javascript/reference/global_objects/function/call/index.html
@@ -0,0 +1,105 @@
+---
+title: Function.prototype.call
+slug: Web/JavaScript/Reference/Global_Objects/Function/call
+translation_of: Web/JavaScript/Reference/Global_Objects/Function/call
+---
+<p>{{JSRef}}</p>
+
+<h2 id="概述">概述</h2>
+
+<p>使用給定的<code>this</code>參數以及分別給定的參數來呼叫某個函數</p>
+
+<div class="note"><strong>附註:</strong> 此函數的所有語法大致上與<code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply" title="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply">apply()</a></code>相同,他們基本上不同處只有 <code>call()</code> 接受一連串的參數,而 <code>apply()</code> 單一的array作為參數</div>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th class="header" colspan="2"><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function" title="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function">Function </a>物件的方法</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>被實作於</td>
+ <td>JavaScript 1.3</td>
+ </tr>
+ <tr>
+ <td>ECMAScript 版本</td>
+ <td>ECMAScript 第三版</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="語法">語法</h2>
+
+<pre class="syntaxbox"><code><em>fun</em>.call(<em>thisArg</em>[, <em>arg1</em>[, <em>arg2</em>[, ...]]])</code></pre>
+
+<h3 id="參數">參數</h3>
+
+<dl>
+ <dt><code>thisArg</code></dt>
+ <dd>呼叫<em><code>fun</code></em>時提供的<code>this</code>值。 注意,它可能是一個無法在函數內看到的值:若這個函數是在非嚴苛模式( <a href="/en-US/docs/JavaScript/Reference/Functions_and_function_scope/Strict_mode" title="JavaScript/Strict mode">non-strict mode</a> ), <code>null</code> <code>、undefined</code> 將會被置換成全域變數,而原生型態的值將會被封裝</dd>
+ <dt><code>arg1, arg2, ...</code></dt>
+ <dd>其他參數</dd>
+</dl>
+
+<h2 id="描述">描述</h2>
+
+<p>你可以在呼叫一個現存的函數時,使用不一樣的 <code>this</code> 物件。 <code>this</code> 會參照到目前的物件,呼叫的物件上</p>
+
+<p>使用 <code>call,</code> 你可以實作函數一次,然後在其他的物件上直接繼承它,而不用在新的物件上重寫該函數</p>
+
+<h2 id="範例">範例</h2>
+
+<h3 id="使用_call_來串接物件上的建構子">使用 <code>call</code> 來串接物件上的建構子</h3>
+
+<p>你可以使用 <code>call</code> 來串接其他物件的建構子,就像 Java. 下面的例子中,<code>Product</code> 物件的建構子定義了兩個參數 <code>name</code> 以及 <code>price</code>. 其他函數<code>Food</code> 及 <code>Toy</code> 引用了 <code>Product</code> 並傳入 <code>this</code> 、 <code>name</code> 和 <code>price</code>。 Product 初始化它的屬性 <code>name</code> 和 <code>price</code>, 而兩個子函數則定義了<code>category。</code></p>
+
+<pre class="brush: js">function Product(name, price) {
+ this.name = name;
+ this.price = price;
+
+ if (price &lt; 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 = new Product();
+
+function Toy(name, price) {
+ Product.call(this, name, price);
+ this.category = 'toy';
+}
+Toy.prototype = new Product();
+
+var cheese = new Food('feta', 5);
+var fun = new Toy('robot', 40);
+</pre>
+
+<h3 id="使用_call_來呼叫匿名的函數">使用 <code>call</code> 來呼叫匿名的函數</h3>
+
+<p>下面這個簡易的例子中,我們做了一個匿名的函數,並用 <code>call</code> 來讓它應用在每個在串列中的物件中. 這個匿名函數的主要用途是加入一個print函數到每個物件上,這個函數可以印出每個物件的index指標。 傳入物件作為 <code>this</code> 的值並不是必要的,但他有解釋的用途。</p>
+
+<pre class="brush: js">var animals = [
+ {species: 'Lion', name: 'King'},
+ {species: 'Whale', name: 'Fail'}
+];
+
+for (var i = 0; i &lt; animals.length; i++) {
+ (function (i) {
+ this.print = function () {
+ console.log('#' + i + ' ' + this.species + ': ' + this.name);
+ }
+ this.print();
+ }).call(animals[i], i);
+}
+</pre>
+
+<h2 id="參見">參見</h2>
+
+<ul>
+ <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply" title="JavaScript/Reference/Global_Objects/Function/apply">apply</a></li>
+</ul>