<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 用 JavaScript 創建對象 > 原文: [http://zetcode.com/javascript/createobject/](http://zetcode.com/javascript/createobject/) JavaScript 創建對象教程展示了如何在 JavaScript 中創建對象。 可以使用對象字面值,函數構造器或類定義來創建對象。 通常使用創建性生成器和工廠設計模式來創建對象。 在本教程中,我們使用 Node.js 執行示例。 ## 對象字面值 在對象字面值表示法中,我們將用逗號分隔的對象屬性放在大括號`{}`中。 屬性名稱和值用冒號分隔。 `object_literal.js` ```js const person = { firstName: 'John', lastName: 'Doe', email: 'jdoe@example.com', info: function() { return `${this.firstName} ${this.lastName}, ${this.email}` } }; console.log(person.info()); ``` 該示例使用字面值表示法創建一個對象。 ```js $ node object_literal.js John Doe, jdoe@example.com ``` 這是輸出。 ## 對象構造器 可以使用`new Object()`構造器創建對象。 然后使用點運算符動態添加屬性。 `object_constructor.js` ```js let person = new Object(); person.firstName = "John"; person.lastName = "Doe"; person.email = 'jdoe@example.com'; person.info = function(){ return `${this.firstName} ${this.lastName}, ${this.email}`; }; console.log(person.info()); ``` 該示例使用`Object`構造器創建一個對象。 ## 函數構造器 使用`function`關鍵字創建函數構造器。 它以值作為參數。 使用`this`關鍵字設置屬性。 使用`this`和`function`關鍵字創建方法。 使用`new`關鍵字創建新對象。 `function_constructor.js` ```js function Person(firstName, lastName, email) { this.firstName = firstName; this.lastName = lastName; this.email = email; this.info = function() { return `${this.firstName} ${this.lastName}, ${this.email}`; } } let person = new Person('John', 'Doe', 'jdoe@example.com'); console.log(person.info()); ``` 該示例使用函數構造器創建一個對象。 ## 類定義 對象用`class`關鍵字定義,并用`new`關鍵字生成。 這是創建從諸如 C# 或 Java 之類的語言已知的對象的經典方法。 JavaScript 使用`constructor`關鍵字定義對象構造器。 使用`this`關鍵字設置屬性。 `class_definition.js` ```js class Person { constructor(firstName, lastName, email) { this.firstName = firstName; this.lastName = lastName; this.email = email; } info() { return `${this.firstName} ${this.lastName}, ${this.email}`; } } let person = new Person('John', 'Doe', 'jdoe@example.com'); console.log(person.info()); ``` 該示例使用類定義創建對象。 ## 構建器模式 構建器模式是一種用于創建對象的創新性設計模式。 它通過提供逐步的方法,使用簡單的對象來構建復雜的對象。 構建器模式使用流利的 API 創建對象。 `builder_pattern.js` ```js let Person = function (firstName, lastName, email) { this.firstName = firstName; this.lastName = lastName; this.email = email; } let PersonBuilder = function () { let firstName; let lastName; let email; return { setFirstName: function (firstName) { this.firstName = firstName; return this; }, setLastName: function (lastName) { this.lastName = lastName; return this; }, setEmail: function (email) { this.email = email; return this; }, info: function () { return `${this.firstName} ${this.lastName}, ${this.email}`; }, build: function () { return new Person(firstName, lastName, email); } }; }; var person = new PersonBuilder().setFirstName('John').setLastName('Doe') .setEmail('jdoe@example.com'); console.log(person.info()); ``` 該示例使用構建器設計模式創建一個對象。 ## 工廠模式 使用工廠模式,我們可以在不將創建邏輯暴露給客戶端的情況下創建對象。 `factory_pattern.js` ```js const personFactory = (firstName, lastName, email) => { return { firstName: firstName, lastName: lastName, email: email, info() { return `${this.firstName} ${this.lastName}, ${this.email}`; } }; }; let person = personFactory('John', 'Doe', 'jdoe@example.com'); console.log(person.info()); ``` 該示例使用工廠模式創建一個對象。 在本教程中,我們使用不同的語法創建了 JavaScript 對象。 我們還介紹了兩種創新的設計模式,即構建器模式和工廠模式。 您可能也對以下相關教程感興趣: [JavaScript 構建器模式教程](/javascript/builderpattern/),或列出[所有 JavaScript 教程](/all/#js)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看