Object : undefined
Object is the base in the prototype chain.
JavaScript **Object**s can have any number values stored as properties
(specified by %%/String|**String**%% names or %%/Symbol|**Symbol**%%s).
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2
----
Object() : Object
Same as %%#new_Object_|**new Object()**%%.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.1.1
----
Object(value : Object) : Object
Same as %%#new_Object_Object|**new Object(value)**%%.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.1.1
----
new Object() : Object
Creates an empty object.
Can also be constructed as **{}**.
// The following are all equivalent
var x = {};
var y = Object();
var z = new Object();
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.2.1
----
new Object(value : Object) : Object
If **value** is a primitive type, creates a box for **value**. Otherwise,
returns **value**.
var x = true;
console.log(typeof x + ' / ' + typeof Object(x));
console.log(x === Object(x));
var y = {};
console.log(typeof y + ' / ' + typeof Object(y));
console.log(y === Object(y));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.2.1
----
instance[propertyName : String] : Object
Returns the value stored in this at the specified **propertyName**.
This is the same as the **.** style access but also allows the
property name to be specified at runtime using a String of being hardcoded and allows
access to properties that are invalid identifiers (such as including spaces or
starting with a number).
var x = { foo: 'bar'};
// The following are equivalent
console.log(x.foo);
console.log(x['foo']);
// Use [] indexing when the name is not known ahead of time
var propertyName = 'f' + 'oo';
console.log(x[propertyName]);
----
instance[symbol : Symbol] : Object
Returns the value stored in this at the specified **symbol**.
var myObject = { foo: 'bar' };
var mySymbol = Symbol('MySymbol');
myObject[mySymbol] = 'baz';
console.log(myObject[mySymbol]);
// The property cannot be accessed using a string
console.log(myObject['MySymbol']);
Version:
ECMAScript 2015
----
assign(target : Object, source1 : Object, [source2 : Object, [...]]) : Object
Copies properties from each **source** to **target**. Any properties already
set on **target** are overwritten. Returns **target**.
const source1 = { a: 1, b: 2, c: 3 };
const source2 = { b: 'foo', d: 4 };
// Often target is a new object to combine sources without modifying either source
const combined = Object.assign({}, source1, source2);
console.dir(combined);
// The new spread operator can achieve the same result:
const spreadCombined = { ...source1, ...source2 };
console.dir(spreadCombined);
Spec:
https://262.ecma-international.org/12.0/#sec-object.assign
----
getPrototypeOf(obj : Object) : Object
Returns the prototype of **obj**.
console.log(Object.getPrototypeOf([]) === Array.prototype);
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.2
----
getOwnPropertyDescriptor(obj : Object, propertyName : String) : PropertyDescriptor
Returns a **PropertyDescriptor** that describes the **propertyName** property on **obj**.
var x = { foo: 1,
get bar() {
return 'a';
} };
Object.defineProperty(x, 'baz', { value: 2,
writable: true,
enumerable: false,
configurable: true } );
console.dir(Object.getOwnPropertyDescriptor(x, 'foo'));
console.dir(Object.getOwnPropertyDescriptor(x, 'bar'));
console.dir(Object.getOwnPropertyDescriptor(x, 'baz'));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.3
----
getOwnPropertyDescriptors(obj : Object) : PropertyDescriptor
Returns an object key-value pairs of the property name and **PropertyDescriptor**s
that describes **obj**.
See also %%#getOwnPropertyDescriptor|**getOwnPropertyDescriptor**%% and
%%#create|**create()**%% and %%#defineProperties|**defineProperties()**%%.
var x = { foo: 1,
get bar() {
return 'a';
} };
Object.defineProperty(x, 'baz', { value: 2,
writable: true,
enumerable: false,
configurable: true } );
var descriptors = Object.getOwnPropertyDescriptors(x);
for (var p in descriptors) {
console.log(p + ':');
console.dir(descriptors[p]);
console.log();
}
Spec:
https://tc39.es/ecma262/#sec-object.getownpropertydescriptors
----
getOwnPropertyNames(obj : Object) : Array
Returns an **Array** of the names of all properties set on **obj** including
those that are not enumerable.
See also %%#keys|**keys()**%%.
var x = { foo: 1 };
Object.defineProperty(x, 'bar', { value: 2,
writable: true,
enumerable: false,
configurable: true } );
console.log(Object.keys(x));
console.log(Object.getOwnPropertyNames(x));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.4
----
getOwnPropertySymbols(obj : Object) : Array
Returns an **Array** of the symbols of properties set on **obj** including
those that are not enumerable.
var myObject = { foo: 'bar' };
var mySymbol = Symbol('MySymbol');
myObject[mySymbol] = 'baz';
for (var symbol of Object.getOwnPropertySymbols(myObject)) {
console.log('myObject[' + symbol.toString() + '] = ' + myObject[symbol]);
}
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-object.getownpropertysymbols
----
create(prototype : Object, [propertyDescriptors : Object]) : Object
Returns a new **Object** with prototype equal to **prototype**. Pass
**null** for **prototype** to create an object with no prototype.
Calls
%%#defineProperties|**defineProperties(obj, propertyDescriptors)**%%
with the new object if **propertyDescriptors** is specified.
var x = Object.create({}, { foo: { value: 1,
writable: true,
enumerable: true,
configurable: true },
bar: { value: 2,
writable: true,
enumerable: false,
configurable: true }});
console.log(x.foo);
console.log(x.bar);
console.log(Object.keys(x));
console.log(Object.getOwnPropertyNames(x));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.5
----
defineProperty(obj : Object, propertyName : String, propertyDescriptor : PropertyDescriptor) : Object
Defines a new property with name equal to **propertyName** on **obj** with the
supplied descriptor. Returns **obj**. See %%/Reflect#defineProperty|Reflect.defineProperty()%%.
var x = { foo: 1 };
Object.defineProperty(x, 'bar', { value: 2,
writable: true,
enumerable: false,
configurable: true } );
console.log(x.foo);
console.log(x.bar);
console.log(Object.keys(x));
console.log(Object.getOwnPropertyNames(x));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.6
----
defineProperties(obj : Object, propertyDescriptors : Object) : Object
For each property on **propertyDescriptors**, defines a new property on
**obj** with the same name and supplied %%/PropertyDescriptor|**PropertyDescriptor**%%.
Returns **obj**. See also %%#getOwnPropertyDescriptors|**getOwnPropertyDescriptors()**%%.
var x = {};
Object.defineProperties(x, { foo: { value: 1,
writable: true,
enumerable: true,
configurable: true },
bar: { value: 2,
writable: true,
enumerable: false,
configurable: true }});
console.log(x.foo);
console.log(x.bar);
console.log(Object.keys(x));
console.log(Object.getOwnPropertyNames(x));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.7
----
entries(obj : Object) : Array
Returns an Array of key/value pairs for the properies of **obj**. See also
%%#fromEntries|fromEntries()%%, %%#keys|keys()%%, and %%#values|values()%%.
var x = { foo: 1, bar: 2 };
console.dir(Object.entries(x));
Version:
ECMAScript 2017
Spec:
https://tc39.es/ecma262/#sec-object.entries
----
freeze(obj : Object) : Object
For each property on **obj**, sets its
%%/PropertyDescriptor#configurable|**PropertyDescriptor.configurable**%%
property to **false** and
%%/PropertyDescriptor#writable|**PropertyDescriptor.writable**%%
property to **false**. Also calls
%%#preventExtensions|**preventExtensions**%% on **obj**. Returns **obj**.
var x = { foo: 1 };
Object.freeze(x);
x.foo = 3;
console.log(x.foo);
x.bar = 1;
console.log(x.bar);
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.9
----
fromEntries(keyValuePairs : Iterator) : Object
Constructs a new Object from the specified key-value pairs. See also
%%#entries|entries()%%.
var x = [['foo', 1], ['bar', 2]];
console.dir(Object.fromEntries(x));
Version:
ECMAScript 2019
Spec:
https://tc39.es/ecma262/#sec-object.fromentries
----
preventExtensions(obj : Object) : Object
Prevents new properties from being added to **obj**. Unlike %%#freeze|**freeze**%%,
existing properties can be modified. Returns **obj**.
var x = { foo: 1 };
Object.preventExtensions(x);
x.foo = 3;
console.log(x.foo);
x.bar = 1;
console.log(x.bar);
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.10
----
is(obj1 : Object, obj2 : Object) : Boolean
Returns **true** if **obj1** is identical to **obj2**. This is mostly the same as
**obj1 === obj2** except for some %%/Number|Number%% comparisons involving
%%/Global#NaN|NaN%%, **0**, and **-0**.
console.log(0 === -0);
console.log(Object.is(0, -0));
console.log();
console.log(NaN === NaN);
console.log(Object.is(NaN, NaN));
Spec:
https://www.ecma-international.org/ecma-262/#sec-object.is
----
isSealed(obj : Object) : Boolean
Returns **true** if %%#seal|**seal**%% was called on **obj**. A sealed object cannot have
new properties added to it but the value of existing properties can be changed.
var x = { foo: 1 };
Object.seal(x);
console.log(Object.isSealed(x));
console.log(Object.isExtensible(x));
console.log(Object.isFrozen(x));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.11
----
isFrozen(obj : Object) : Boolean
Returns **true** if %%#freeze|**freeze**%% was called on **obj**. A frozen object cannot have
new properties added and existing properties cannot be modified.
var x = { foo: 1 };
Object.freeze(x);
console.log(Object.isSealed(x));
console.log(Object.isExtensible(x));
console.log(Object.isFrozen(x));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.12
----
isExtensible(obj : Object) : Boolean
Returns **false** if %%#preventExtensions|**preventExtensions**%% was called on **obj**. A
non-extensible object cannot have new properties added to it.
var x = { foo: 1 };
console.log(Object.isExtensible(x));
Object.preventExtensions(x);
console.log(Object.isExtensible(x));
console.log(Object.isSealed(x));
console.log(Object.isFrozen(x));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.13
----
keys(obj : Object) : Array
Returns an **Array** containing the enumerable properties on **obj**.
See also %%#getOwnPropertyNames|**getOwnPropertyNames()**%%,
%%#entries|**entries()**%% and %%#values|**values()**%%.
var x = { foo: 1, bar: 2 };
console.log(Object.keys(x));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.14
----
prototype.constructor : Function
The **Function** that constructed **this**.
console.log({}.constructor === Object);
console.log([].constructor === Array);
console.log('foo'.constructor === String);
----
prototype.toString() : String
Returns a string representation of **this**. This method is called
each time the object is used in a place a string is expected. Define your own
**toString** to give a better string representation to your objects.
var pt = { x: 1, y: 2 };
console.log(pt);
pt.toString = function() {
return '(' + this.x + ', ' + this.y + ')';
};
console.log(pt);
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.2
----
prototype.toLocaleString() : String
Returns a locale specific (if possible) string representation of **this**.
var x = 1.234;
console.log(x.toString());
console.log(x.toLocaleString());
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.3
----
prototype.valueOf() : Object
For boxed primitive types, returns the unboxed value.
var x = Object(1);
console.log(x === 1);
console.log(x.valueOf() === 1);
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.4
----
prototype.hasOwnProperty(propertyName : String) : Boolean
Returns **true** if **this** has a property named **propertyName** stored on it.
This method can be used to see if a property is set on the object, even
if the value is set to **undefined**.
Properties stored in the prototype chain do not count.
var x = { foo: 1 };
console.log(x.hasOwnProperty('foo'));
console.log(x.hasOwnProperty('bar'));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.5
----
prototype.isPrototypeOf(obj : Object) : Boolean
Returns **true** if **this** is in the prototype chain of **obj**.
// Array instances are Objects
console.log(Object.prototype.isPrototypeOf(Array.prototype));
// Array instances are not Functions
console.log(Function.prototype.isPrototypeOf(Array.prototype));
// Array constructor is a Function
console.log(Function.prototype.isPrototypeOf(Array));
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.6
----
prototype.propertyIsEnumerable(propertyName : String) : Boolean
Returns **true** if the property named **propertyName** on **this** is
enumerable and should be returned during a **for (x in y)** loop.
var x = { foo: 1 };
Object.defineProperty(x, 'bar', { value: 2,
writable: true,
enumerable: false,
configurable: true } );
console.log(x.propertyIsEnumerable('foo'));
console.log(x.propertyIsEnumerable('bar'));
for (var propertyName in x) {
console.log(propertyName + ' is ' + x[propertyName]);
}
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.7
----
seal(obj : Object) : Object
Same as %%#preventExtensions|**preventExtensions**%% except it also
sets each property as not
%%/PropertyDescriptor#configurable|**configurable**%%. Returns **obj**.
var x = { foo: 1 };
Object.seal(x);
x.foo = 3;
console.log(x.foo);
x.bar = 1;
console.log(x.bar);
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.8
----
setPrototypeOf(obj : Object, prototype : Object) : Object
Changes the prototype of **obj** to **prototype**. Returns **obj**.
const base = { a: 1, b: 2 };
const obj = { c: 3 };
console.dir(obj);
console.log(Object.getPrototypeOf(obj) === Object.prototype);
Object.setPrototypeOf(obj, base);
console.dir(obj);
console.log(Object.getPrototypeOf(obj) === base);
Spec:
https://262.ecma-international.org/12.0/#sec-object.setprototypeof
----
values(obj : Object) : Array