# Object+arrayLike

[JS](../../../..) ⟩ [object](../../../obj) ⟩ [built-in](../..) ⟩ [Array](..) ⟩ [array-like]() ⟩ Object+arrayLike

{% hint style="success" %}
extend [array-like]() objects with custom methods.
{% endhint %}

{% tabs %}
{% tab title="⬇️ 應用" %}
* [touchlist+ext](../../../../../browser/event/type/touch/touchlist+ext "mention") - TouchList is an [array-like]().
{% endtab %}

{% tab title="💾 程式" %}
:floppy\_disk: replit：[Object+arrayLike](https://replit.com/@pegasusroe/ObjectarrayLike#ext/Object+arrayLike.js)

```javascript
// 2023.01.17 - 07:57 - first draft
// --------------------------------------------------

// helpers
function checkType(obj) {
    if (!('length' in obj)) {
        throw Error(`${obj} is not an "array-like" object!`)
    }
}

// ⭐️ Object + array-like
// --------------------------------------------------
// 🔹 .map()
//
Object.defineProperties(Object.prototype, {

    // 🔹 .map()
    map: {
        value: function(f) {
            checkType(this);
            const arr = [];
            for (let i = 0; i < this.length; i++) {
                arr.push(f(this[i]));
            }
            return arr;
        }
    },
    
});

// export 
// -------------------------------------------
export {};              // ES module
// module.exports = {};    // CommonJS module
```
{% endtab %}
{% endtabs %}
