---
description: an object without prototype.
---

# "pure" object

[JS](../../../..) ⟩ [value](../../..) ⟩ [object](../..) ⟩ [create](..) ⟩ [Object.create()]() ⟩ pure object&#x20;

{% hint style="info" %}
<mark style="color:yellow;">`Object.create(null)`</mark> is an object without [**prototype**](../../proto).
{% endhint %}

{% tabs %}
{% tab title="🧨 雷區" %}
{% hint style="danger" %}
this "<mark style="color:purple;">**pure**</mark>" <mark style="color:purple;">**object**</mark> <mark style="color:yellow;">**will**</mark>**&#x20;**<mark style="color:red;">**not**</mark>**&#x20;**<mark style="color:yellow;">**inherit**</mark>**&#x20;**<mark style="color:red;">**anything**</mark>:exclamation:

* does not have <mark style="color:blue;">`toString()`</mark> method.
* won't work with <mark style="color:blue;">`+`</mark> operator.
* can't call <mark style="color:blue;">`__proto__`</mark> (accessor).
{% endhint %}
{% endtab %}

{% tab title="💈範例" %}
* replit ⟩ [pure object](https://replit.com/@pegasusroe/JS-object-pure-object#index.js)

```javascript
const { log } = console;

// -----------------------------------
// ⭐ pure object without "prototype"
const obj = Object.create(null);
// -----------------------------------

// ⭐ can't get obj's [[Prototype]] using `__proto__` now.
// ⭐ `__proto__` is a getter/setter that resides in `Object.prototype`.
const proto = Object.getPrototypeOf(obj);    // null

log(obj);        // ⭐ [Object: null prototype]

// to silence the ⛔ TypeError, define `toString`:

// ⭐ regular property is "enumerable"
// obj.toString = () => `hi`;

// ⭐ use `Object.defineProperty` to define a non-enumerable property.
Object.defineProperty(obj, 'toString', {
    value: () => `hi`,
})

// -------------------------------------------------------
// ⛔ TypeError: Cannot convert object to primitive value
// -------------------------------------------------------
log(`${obj}`);    // ⭐ `obj` has no `toString()`❗
// -------------------------------------------------------

;
[
    typeof obj,            // 'object'
    obj instanceof Object, // ⭐ false❗ (你說，JavaScript 是不是很亂搞 🙄)

    Object.keys(obj),      // [ 'toString' ] (if `toString` is enumerable)
                           // [ ]            (if `toString` is non-enumerable)
    
    proto === null,        // true
    obj.__proto__,         // ⭐ undefined❗
    
].forEach(x => log(x));
```
{% endtab %}

{% tab title="📗 參考" %}
* [x] JS.info ⟩ ["very plain" objects](https://javascript.info/prototype-methods#very-plain)
{% endtab %}

{% tab title="📘 手冊" %}
* [Object.defineProperty()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty)
{% endtab %}
{% endtabs %}
