JavaScript: 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)
-
Return
new Boolean(boolean)
Object(number)
-
Return
new Number(number)
Object(string)
-
Return
new String(string)
Object(obj)
-
Return
obj
// 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
Object Literal Expression
Normally, you use literal expression to create object. [see Object Literal Expression]
JavaScript Object and Inheritance
- Object Tutorial
- Object Overview
- Object Type
- Test If a Value is Object Type ð
- Find Object's Type
- Prototype and Inheritance
- Prototype Chain
- Is in Prototype Chain?
- Get/Set Parent
- Show Parent Chain ð
- Create Object
- Object Literal Expr
- Create Object + Parent
- Prevent Adding Property
- Clone Object ð
- Test Object Equality ð
- Add Method to Prototype