> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/chapter1/11bi-ji/13object.md).

# 1.4.Object

* 在JS中, 物件具有指向委託原型(delegate prototype)的內部參考
* 所有的數據都是對象（javascript中除了字元串字面量、數字字面量、true、false、null、undefined之外，其他值都是對象）
* 要得到一個對象,不是通過實例化類,而是找到一個對象作為原型並複製它
* 當物件要求方法及特性時, JS引擎會先檢查該物件, 如果請求之鍵不在物件身上, 他就會去檢查委托原型, 此原型練通常會結束於object.
* 1.Object.create(): ES5後被建立
  * 傳入一個參數,Object.create將會建立此參數的副本
  * (1) 如果改變實例上的特性(的值)或陣列特性,該變更將於原型上被共用&#x20;
  * (2) 如果替換實例上的整個特性,該變更只會被反映在該實例上
  * example:

    ```
    var switchPotato = {
      state: false;
      isOn: function isOn{
        return this.state;
      },
      toggle: function toggle(){
        this.state = !this.state;
        return this;
      },
      meta: {
        name: 'light switch'
      }
    },
    switch1 = Object.create(switchPotato),
    switch2 = Object.create(switchPotato);

    test('Prototype mutations', function(){
        switch2.meta.name = 'Breaker switch';

        equal(switch1.meta.name, 'Breaker switch', 'Object and array mutations are shared');

        switch2.meta = {name : 'Power switch'};

        equal(switch1.meta.name, 'Breaker switch', 'Property replacement is instance-specific');
    });
    ```
* 2.使用new調用建構子
  * 實際上使用new運算子後接上一個函式時, 其實是呼叫call以建立新物件

    * 1.屬性或是函式以this.xxx的方式定義在物件中, 以new建立物件
      * 例如:

    ![](/files/-M4M0MxLD0ZJ3FSWb8Bd)

    * 2.屬性或是函式以this.xxx的方式定義在物件中, 以call建立物件
      * 例如:

    ![](/files/-M4M0MxNSMJTAUDzEJSQ)

    * 3.屬性或是函式都以json格式回傳
      * 例如:

    ![](/files/-M4M0MxPdWjtpSDmibQ9)

    * <http://codepen.io/JenHsuan/pen/yMxeXV?editors=0012#>
    * <http://codepen.io/JenHsuan/pen/bqxVYQ?editors=1011>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/chapter1/11bi-ji/13object.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
