mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-15 23:37:25 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/function-call.html" width="100%"></iframe></div>
|
||||
<p class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
|
||||
<h2 id="语法">语法</h2>
|
||||
<pre class="syntaxbox"><code><em>fun</em>.call(<em>thisArg</em>, <em>arg1</em>, <em>arg2</em>, ...)</code></pre>
|
||||
<pre><code class="language-javascript"><code><em>fun</em>.call(<em>thisArg</em>, <em>arg1</em>, <em>arg2</em>, ...)</code></code></pre>
|
||||
<h3 id="参数">参数</h3>
|
||||
<dl>
|
||||
<dt><code>thisArg</code></dt>
|
||||
@@ -21,7 +21,7 @@
|
||||
<h2 id="示例">示例</h2>
|
||||
<h3 id="使用_call_方法调用父构造函数">使用 <code>call</code> 方法调用父构造函数</h3>
|
||||
<p>在一个子构造函数中,你可以通过调用父构造函数的 <code>call</code> 方法来实现继承,类似于 <code>Java</code> 中的写法。下例中,使用 <code>Food</code> 和 <code>Toy </code>构造函数创建的对象实例都会拥有在 <code>Product</code> 构造函数中添加的 <code>name</code> 属性和 <code>price</code> 属性,但 <code>category</code> 属性是在各自的构造函数中定义的。</p>
|
||||
<pre class="brush: js">function Product(name, price) {
|
||||
<pre><code class="language-javascript">function Product(name, price) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
@@ -38,10 +38,10 @@ function Toy(name, price) {
|
||||
|
||||
var cheese = new Food('feta', 5);
|
||||
var fun = new Toy('robot', 40);
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="使用_call_方法调用匿名函数">使用 <code>call</code> 方法调用匿名函数</h3>
|
||||
<p>在下例中的 <code>for</code> 循环体内,我们创建了一个匿名函数,然后通过调用该函数的 <code>call</code> 方法,将每个数组元素作为指定的 <code>this</code> 值执行了那个匿名函数。这个匿名函数的主要目的是给每个数组元素对象添加一个 <code>print</code> 方法,这个 <code>print</code> 方法可以打印出各元素在数组中的正确索引号。当然,这里不是必须得让数组元素作为 <code>this</code> 值传入那个匿名函数(普通参数就可以),目的是为了演示 <code>call</code> 的用法。</p>
|
||||
<pre class="brush: js">var animals = [
|
||||
<pre><code class="language-javascript">var animals = [
|
||||
{ species: 'Lion', name: 'King' },
|
||||
{ species: 'Whale', name: 'Fail' }
|
||||
];
|
||||
@@ -55,10 +55,10 @@ for (var i = 0; i < animals.length; i++) {
|
||||
this.print();
|
||||
}).call(animals[i], i);
|
||||
}
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="使用_call_方法调用函数并且指定上下文的_'this'">使用 <code>call</code> 方法调用函数并且指定上下文的 '<code>this</code>'</h3>
|
||||
<p>在下面的例子中,当调用 <code>greet</code> 方法的时候,该方法的<code>this</code>值会绑定到 <code>obj</code> 对象。</p>
|
||||
<pre class="brush: js">function greet() {
|
||||
<pre><code class="language-javascript">function greet() {
|
||||
var reply = [this.animal, 'typically sleep between', this.sleepDuration].join(' ');
|
||||
console.log(reply);
|
||||
}
|
||||
@@ -68,20 +68,20 @@ var obj = {
|
||||
};
|
||||
|
||||
greet.call(obj); // cats typically sleep between 12 and 16 hours
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="使用_call_方法调用函数并且不指定第一个参数(argument)" style="margin-bottom: 20px; line-height: 30px;">使用 <code><strong>call</strong></code> 方法调用函数并且不指定第一个参数(<code>argument</code>)</h3>
|
||||
<p>在下面的例子中,我们调用了 <code>display</code> 方法,但并没有传递它的第一个参数。如果没有传递第一个参数,<code>this</code> 的值将会被绑定为全局对象。</p>
|
||||
<pre class="brush: js">var sData = 'Wisen';
|
||||
<pre><code class="language-javascript">var sData = 'Wisen';
|
||||
|
||||
function display() {
|
||||
console.log('sData value is %s ', this.sData);
|
||||
}
|
||||
|
||||
display.call(); // sData value is Wisen</pre>
|
||||
display.call(); // sData value is Wisen</code></pre>
|
||||
<div class="note">
|
||||
<p><strong>注意:</strong>在严格模式下,<code>this</code> 的值将会是 <code>undefined</code>。见下文。</p>
|
||||
</div>
|
||||
<pre class="brush: js">'use strict';
|
||||
<pre><code class="language-javascript">'use strict';
|
||||
|
||||
var sData = 'Wisen';
|
||||
|
||||
@@ -89,7 +89,7 @@ function display() {
|
||||
console.log('sData value is %s ', this.sData);
|
||||
}
|
||||
|
||||
display.call(); // Cannot read the property of 'sData' of undefined</pre>
|
||||
display.call(); // Cannot read the property of 'sData' of undefined</code></pre>
|
||||
<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
Reference in New Issue
Block a user