mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-15 15:20:30 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
<p><strong> <code>constructor </code></strong>是一种用于创建和初始化<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class">class</a></code>创建的对象的特殊方法。</p>
|
||||
<p><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/classes-constructor.html" width="100%"></iframe></p>
|
||||
<h2 id="语法">语法</h2>
|
||||
<pre class="syntaxbox">constructor([arguments]) { ... }</pre>
|
||||
<pre><code class="language-javascript">constructor([arguments]) { ... }</code></pre>
|
||||
<h2 id="描述">描述</h2>
|
||||
<p>在一个类中只能有一个名为 “constructor” 的特殊方法。 一个类中出现多次构造函数 (<code>constructor)</code>方法将会抛出一个 <a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>SyntaxError</code></a> 错误。</p>
|
||||
<p>在一个构造方法中可以使用<code>super</code>关键字来调用一个父类的构造方法。</p>
|
||||
@@ -12,7 +12,7 @@
|
||||
<h2 id="示例">示例</h2>
|
||||
<h3 id="使用constructor方法">使用<code>constructor</code>方法</h3>
|
||||
<p>以下代码片段来自 <a class="external" href="https://github.com/GoogleChrome/samples/blob/gh-pages/classes-es6/index.html" rel="noopener">类的实例</a>(<a class="external" href="https://googlechrome.github.io/samples/classes-es6/index.html" rel="noopener">在线 demo</a>)。</p>
|
||||
<pre class="brush: js">class Square extends Polygon {
|
||||
<pre><code class="language-javascript">class Square extends Polygon {
|
||||
constructor(length) {
|
||||
// 在这里, 它调用了父类的构造函数, 并将 lengths 提供给 Polygon 的"width"和"height"
|
||||
super(length, length);
|
||||
@@ -30,10 +30,10 @@
|
||||
}
|
||||
}
|
||||
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="另一个例子">另一个例子</h3>
|
||||
<p>看看这个代码片段</p>
|
||||
<pre class="brush: js">class Polygon {
|
||||
<pre><code class="language-javascript">class Polygon {
|
||||
constructor() {
|
||||
this.name = "Polygon";
|
||||
}
|
||||
@@ -54,15 +54,15 @@ console.log(Object.getPrototypeOf(Square.prototype) === Rectangle.prototype); //
|
||||
|
||||
let newInstance = new Square();
|
||||
console.log(newInstance.name); //Polygon
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>这里,<strong>Square</strong>类的原型被改变,但是在正在创建一个新的正方形实例时,仍然调用前一个基类<strong>Polygon</strong>的构造函数。</p>
|
||||
<h3 id="默认构造方法">默认构造方法</h3>
|
||||
<p>如前所述,如果不指定构造方法,则使用默认构造函数。对于基类,默认构造函数是:</p>
|
||||
<pre class="brush: js">constructor() {}</pre>
|
||||
<pre><code class="language-javascript">constructor() {}</code></pre>
|
||||
<p>对于派生类,默认构造函数是:</p>
|
||||
<pre class="brush: js">constructor(...args) {
|
||||
<pre><code class="language-javascript">constructor(...args) {
|
||||
super(...args);
|
||||
}</pre>
|
||||
}</code></pre>
|
||||
<h2 id="标准">标准</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
Reference in New Issue
Block a user