diff options
author | Ryan Johnson <rjohnson@mozilla.com> | 2021-04-29 16:16:42 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-29 16:16:42 -0700 |
commit | 95aca4b4d8fa62815d4bd412fff1a364f842814a (patch) | |
tree | 5e57661720fe9058d5c7db637e764800b50f9060 /files/vi/web/javascript/reference/classes/constructor | |
parent | ee3b1c87e3c8e72ca130943eed260ad642246581 (diff) | |
download | translated-content-95aca4b4d8fa62815d4bd412fff1a364f842814a.tar.gz translated-content-95aca4b4d8fa62815d4bd412fff1a364f842814a.tar.bz2 translated-content-95aca4b4d8fa62815d4bd412fff1a364f842814a.zip |
remove retired locales (#699)
Diffstat (limited to 'files/vi/web/javascript/reference/classes/constructor')
-rw-r--r-- | files/vi/web/javascript/reference/classes/constructor/index.html | 127 |
1 files changed, 0 insertions, 127 deletions
diff --git a/files/vi/web/javascript/reference/classes/constructor/index.html b/files/vi/web/javascript/reference/classes/constructor/index.html deleted file mode 100644 index dddb2555f3..0000000000 --- a/files/vi/web/javascript/reference/classes/constructor/index.html +++ /dev/null @@ -1,127 +0,0 @@ ---- -title: constructor -slug: Web/JavaScript/Reference/Classes/constructor -translation_of: Web/JavaScript/Reference/Classes/constructor ---- -<div>{{jsSidebar("Classes")}}</div> - -<p><code><font face="Arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">Phương thức </span></font>constructor</code> là một phương thức đặc biệt dùng để khởi tạo 1 object và được tạo ở trong <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/class">class</a></code>.</p> - -<div>{{EmbedInteractiveExample("pages/js/classes-constructor.html")}}</div> - - - -<h2 id="Cú_pháp">Cú pháp</h2> - -<pre class="syntaxbox">constructor([<em>arguments</em>]) { ... }</pre> - -<h2 id="Mô_tả">Mô tả</h2> - -<p>Chỉ có duy nhất 1 phương thức đặc biệt tên là "constructor" ở trong class. Có nhiều hơn 1 phương thức <code>constructor</code> ở trong class thì sẽ gây ra lỗi{{jsxref("SyntaxError")}}.</p> - -<p>Một constructor có thể sử dụng từ khóa {{jsxref("Operators/super", "super")}} để gọi đến constructor của class cha.</p> - -<p>Nếu bạn không chỉ định 1 phương thức constructor thì constructor mặc định sẽ được sử dụng</p> - -<h2 id="Ví_dụ">Ví dụ</h2> - -<h3 id="Sử_dụng_phương_thức_constructor">Sử dụng phương thức <code>constructor</code> </h3> - -<p>Đoạn code này được lấy từ <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/classes-es6/index.html">classes sample</a> (<a href="https://googlechrome.github.io/samples/classes-es6/index.html">live demo</a>).</p> - -<pre class="brush: js">class Square extends Polygon { - constructor(length) { - // Here, it calls the parent class' constructor with lengths - // provided for the Polygon's width and height - super(length, length); - // Note: In derived classes, super() must be called before you - // can use 'this'. Leaving this out will cause a reference error. - this.name = 'Square'; - } - - get area() { - return this.height * this.width; - } - - set area(value) { - this.area = value; - } -}</pre> - -<h3 id="Ví_dụ_khác">Ví dụ khác</h3> - -<p>Hãy xem đoạn code sau</p> - -<pre class="brush: js">class Polygon { - constructor() { - this.name = "Polygon"; - } -} - -class Square extends Polygon { - constructor() { - super(); - } -} - -class Rectangle {} - -Object.setPrototypeOf(Square.prototype, Rectangle.prototype); - -console.log(Object.getPrototypeOf(Square.prototype) === Polygon.prototype); //false -console.log(Object.getPrototypeOf(Square.prototype) === Rectangle.prototype); //true - -let newInstance = new Square(); -console.log(newInstance.name); //Polygon</pre> - -<p>Ở đây prototype của class <strong>Square</strong> đã bị thay đổi nhưng constructor kế thừa từ class <strong>Polygon </strong>vẫn được gọi khi tạo ra 1 thực thể mới.</p> - -<h3 id="Default_constructors">Default constructors</h3> - -<p>Như đã nói ởi trước, nếu bạn không chỉ đỉnh 1 phương thức constructor thì default constructor sẽ được sử dụng. Với những class cơ bản thì default contructor sẽ là:</p> - -<pre class="brush: js">constructor() {} -</pre> - -<p>Với những class dẫn xuất, default constructor sẽ là:</p> - -<pre class="brush: js">constructor(...args) { - super(...args); -}</pre> - -<h2 id="Thông_số_kĩ_thuật">Thông số kĩ thuật</h2> - -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Specification</th> - <th scope="col">Status</th> - <th scope="col">Comment</th> - </tr> - <tr> - <td>{{SpecName('ES2015', '#sec-static-semantics-constructormethod', 'Constructor Method')}}</td> - <td>{{Spec2('ES2015')}}</td> - <td>Initial definition.</td> - </tr> - <tr> - <td>{{SpecName('ESDraft', '#sec-static-semantics-constructormethod', 'Constructor Method')}}</td> - <td>{{Spec2('ESDraft')}}</td> - <td> </td> - </tr> - </tbody> -</table> - -<h2 id="Browser_compatibility">Browser compatibility</h2> - - - -<p>{{Compat("javascript.classes.constructor")}}</p> - -<h2 id="Bài_viết_liên_quan">Bài viết liên quan</h2> - -<ul> - <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/super">super()</a></li> - <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/class"><code>class</code> expression</a></li> - <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/class"><code>class</code> declaration</a></li> - <li><a href="/en-US/docs/Web/JavaScript/Reference/Classes">Classes</a></li> -</ul> |