JavaScript: Object Object Properties

By Xah Lee. Date: . Last updated: .

Object is the value of a property key "Object" of the global object.

console.log ( window.hasOwnProperty ( "Object" ) ); // true

Object is a function object.

// type of Object
console.log ( typeof Object === "function" ); // true

The parent of Object is Function.prototype.

// parent of Object object
console.log ( Object.getPrototypeOf ( Object ) === Function.prototype ); // true

The purpose of Object is:

  1. To convert primitive value (such as string, number, boolean) to a object. (by Object(…))
  2. Used as a namespace to hold general purpose methods for working with objects. For example, Object.isExtensible(…).
  3. Holds the property key "prototype". The value of Object.prototype is the root parent object of all standard objects. (so they inherit useful methods.) 〔➤see JavaScript: Prototype and Inheritance

Object Consructor

〔➤see JavaScript: Data Types

Here's the full list of possible argument and their result:

// 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
var o1 = {};
var o2 = Object(o1);
console.log ( o1 === o2); // true

// array
var a1 = [];
var a2 = Object(a1);
console.log ( a1 === a2); // true

// function
var f1 = function () { };
var f2 = Object(f1);
console.log ( f1 === f2); // true

// date
var d1 = new Date();
var d2 = Object(d1);
console.log ( d1 === d2); // true

ECMAScript 2015 §Fundamental Objects#sec-object-constructor

Properties


Object.create

Object.create(obj) → returns a new object such that its parent is obj

Object.create(obj, {p1:property_descriptor_1, p2:property_descriptor_2, …}) → return a new object such that its prototype (parent) is obj. Also defines the new object's properties and their attributes.

JavaScript: Creating Object with Object.create()

ECMAScript 2015 §Fundamental Objects#sec-object.create

Object.assign

(ES2015)

Object.assign ( target, source, … )

Copy the values of all of the enumerable own properties from one or more source objects to a target object.

〔➤see JavaScript: Property Attributes: Writable, Enumerable, Configurable

var A = {a1: 3};
var B = {b1: 3};
var C = {};

// add properties form A and B to C. Returns the new C
console.log ( Object.assign ( C, A, B ) ); // { a1: 3, b1: 3 }

// value of variable C is changed
console.log ( C ); // { a1: 3, b1: 3 }

ECMAScript 2015 §Fundamental Objects#sec-object.assign


Object.getPrototypeOf

Object.getPrototypeOf ( O )

Returns the parent object.

JavaScript: Find Object's Prototype/Parent

ECMAScript 2015 §Fundamental Objects#sec-object.getprototypeof

Object.setPrototypeOf

(ES2015)

Object.setPrototypeOf ( O, proto )

var a = {};
var b = Object.create (a);

// b's parent is a
console.log (
Object.getPrototypeOf ( b ) === a
); // true

// now change parent
Object.setPrototypeOf ( b, Array );

// b's parent is no longer a
console.log (
Object.getPrototypeOf ( b ) === a
); // false

console.log (
Object.getPrototypeOf ( b ) === Array
); // true

ECMAScript 2015 §Fundamental Objects#sec-object.setprototypeof


Object.getOwnPropertyDescriptor

Object.getOwnPropertyDescriptor ( O, P )

Return a property's attributes.

〔➤see JavaScript: Property Attributes: Writable, Enumerable, Configurable

ECMAScript 2015 §Fundamental Objects#sec-object.getownpropertydescriptor

Object.defineProperty

Object.defineProperty ( O, P, Attributes )

Object.defineProperty(obj, property_key, property_descriptor) → create a property with specified attribute values (if property_key doesn't exist), or modify a existing property's attributes. Returns the modified object obj.

JavaScript: Property Attributes: Writable, Enumerable, Configurable

ECMAScript 2015 §Fundamental Objects#sec-object.defineproperty

Object.defineProperties

Object.defineProperties ( O, Properties )

JavaScript: Property Attributes: Writable, Enumerable, Configurable

ECMAScript 2015 §Fundamental Objects#sec-object.defineproperties


Object.getOwnPropertyNames

Object.getOwnPropertyNames ( O )

Return a array of all own property names as strings, excluding symbol keys. (including non-enumerable ones).

ECMAScript 2015 §Fundamental Objects#sec-object.getownpropertynames

Object.getOwnPropertySymbols

(ES2015)

Object.getOwnPropertySymbols ( O )

Return a array of all own symbol key properties . (including non-enumerable ones).

var x = Symbol("x");
var obj = {};
obj[x] = 3;
console.log ( obj[x] ); // 3

// get a array of all symbol key properties
console.log ( Object.getOwnPropertySymbols ( obj ) ); // [ Symbol(x) ]

〔➤see JavaScript: Symbol (ES2015)

// get a array of all symbol key properties
console.log ( Object.getOwnPropertySymbols ( Array.prototype ) );
// prints
// [ Symbol(Symbol.unscopables), Symbol(Symbol.iterator) ]

ECMAScript 2015 §Fundamental Objects#sec-object.getownpropertysymbols


Object.keys

Object.keys ( O )

Return a array of property names that are own and enumerable.

var s = Symbol("x");

var u =
Object.create ( Object.prototype, {
    "p": { value : 3, writable: true, enumerable: false, configurable: true },
    "q": { value : 3, writable: true, enumerable: true, configurable: true },
    [Symbol()]: { value : 3, writable: true, enumerable: true, configurable: true },
});

// get all own property string keys that are enumerable
console.log ( Object.keys ( u ) ); // ['q']

// symbol key and non-enumerable properties are ignored
console.log ( Reflect.ownKeys ( u ) ); // [ 'p', 'q', Symbol() ]

〔➤see JavaScript: Access/List Properties

〔➤see JavaScript: Property Attributes: Writable, Enumerable, Configurable

ECMAScript 2015 §Fundamental Objects#sec-object.keys


Object.is

Object.is ( value1, value2 )

(ES2015)

similar to value1 === value2, except when values are signed zeroes and NaN.

console.log( NaN === NaN); // prints false
console.log( Object.is (NaN, NaN) ); // prints true

console.log( -0 === +0); // prints true
console.log( Object.is (-0, +0) ); // prints false

ECMAScript 2015 §Fundamental Objects#sec-object.is

Object.isExtensible

(ES2015)

Object.isExtensible ( O )

JavaScript: Prevent Adding/Deleting/Modifying Object Properties

ECMAScript 2015 §Fundamental Objects#sec-object.isextensible

Object.isFrozen

Object.isFrozen ( O )

JavaScript: Prevent Adding/Deleting/Modifying Object Properties

ECMAScript 2015 §Fundamental Objects#sec-object.isfrozen

Object.isSealed

Object.isSealed ( O )

JavaScript: Prevent Adding/Deleting/Modifying Object Properties

ECMAScript 2015 §Fundamental Objects#sec-object.issealed


Object.preventExtensions

Object.preventExtensions ( O )

JavaScript: Prevent Adding/Deleting/Modifying Object Properties

ECMAScript 2015 §Fundamental Objects#sec-object.preventextensions

Object.freeze

Object.freeze ( O )

JavaScript: Prevent Adding/Deleting/Modifying Object Properties

ECMAScript 2015 §Fundamental Objects#sec-object.freeze

Object.seal

Object.seal ( O )

JavaScript: Prevent Adding/Deleting/Modifying Object Properties

ECMAScript 2015 §Fundamental Objects#sec-object.seal

Object.prototype

The value of Object.prototype is the root parent of all standard objects.

JavaScript: Object.prototype Properties

ECMAScript 2015 §Fundamental Objects#sec-object.prototype

Reference

ES2015 §Fundamental Objects#sec-object-objects

Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.