JS: Object Constructor
The purpose of Object(arg)
is to convert a Primitive Value to a Object Type.
new Object(arg)
-
equivalent to
Object(arg)
. Object(value)
- Return a object, by converting value to a object version of value. If value is aready of type object, it is returned.
Object()
-
Return a empty object. Same as
{}
.
Here is the full list of possible argument and their result:
Object()
→ Return{}
Object(null)
→ Return{}
Object(undefined)
→ Return{}
Object(boolean)
→ Returnnew Boolean(boolean)
Object(number)
→ Returnnew Number(number)
Object(string)
→ Returnnew String(string)
Object(obj)
→ Returnobj
// conversion of value to object console.log( Object() ); // {} console.log( Object(null) ); // {} console.log( Object(undefined) ); // {} console.log( Object(true) ); // [Boolean: true] console.log( Object(3) ); // [Number: 3] console.log( Object("x") ); // [String: 'x'] console.log( Object({}) ); // {}
When argument is already a object, that object itself is returned.
// Object(arg) returns arg itself if arg is a object type already // object const x1 = {}; const x2 = Object(x1); console.log( x1 === x2); // true // array const a1 = []; const a2 = Object(a1); console.log( a1 === a2); // true // function const f1 = function () { }; const f2 = Object(f1); console.log( f1 === f2); // true // date const d1 = new Date(); const d2 = Object(d1); console.log( d1 === d2); // true
JavaScript, Object and Inheritance
- JS: Object Tutorial
- JS: Object Overview
- JS: Object Type
- JS: Test is Object Type 🚀
- JS: Determine Type of Object
- JS: Prototype and Inheritance
- JS: Prototype Chain
- JS: Object.prototype.isPrototypeOf
- JS: Get Set Prototype
- JS: Show Prototype Chain 🚀
- JS: Create Object
- JS: Object Literal Expression
- JS: Create Object with Parent X
- JS: Prevent Adding Property
- JS: Deep Copy Array or Object 🚀
- JS: Test Equality of Array and Object by Content 🚀
- JS: Add Method to Prototype
- JS: Object Object
- JS: Object Constructor
- JS: Object.prototype