Web/JavaScript/Reference/Global objects/Object

From Get docs


The Object class represents one of JavaScript's data types. It is used to store various keyed collections and more complex entities. Objects can be created using the Object() constructor or the object initializer / literal syntax.

Description

Nearly all objects in JavaScript are instances of Object; a typical object inherits properties (including methods) from Object.prototype, although these properties may be shadowed (a.k.a. overridden). However, an Object may be deliberately created for which this is not true (e.g. by Object.create(null)), or it may be altered so that this is no longer true (e.g. with Object.setPrototypeOf).

Changes to the Object prototype object are seen by all objects through prototype chaining, unless the properties and methods subject to those changes are overridden further along the prototype chain. This provides a very powerful although potentially dangerous mechanism to override or extend object behavior.

The Object constructor creates an object wrapper for the given value.

  • If the value is null or undefined, it will create and return an empty object.
  • Otherwise, it will return an object of a Type that corresponds to the given value.
  • If the value is an object already, it will return the value.

When called in a non-constructor context, Object behaves identically to new Object().

See also the object initializer / literal syntax.

Deleting a property from an object

There isn't any method in an Object itself to delete its own properties (such as Map.prototype.delete()). To do so, one must use the delete operator.

Constructor

Object()
Creates a new Object object. It is a wrapper for the given value.

Static methods

Object.assign()
Copies the values of all enumerable own properties from one or more source objects to a target object.
Object.create()
Creates a new object with the specified prototype object and properties.
Object.defineProperty()
Adds the named property described by a given descriptor to an object.
Object.defineProperties()
Adds the named properties described by the given descriptors to an object.
Object.entries()
Returns an array containing all of the [key, value] pairs of a given object's own enumerable string properties.
Object.freeze()
Freezes an object. Other code cannot delete or change its properties.
Object.fromEntries()
Returns a new object from an iterable of [key, value] pairs. (This is the reverse of Object.entries).
Object.getOwnPropertyDescriptor()
Returns a property descriptor for a named property on an object.
Object.getOwnPropertyDescriptors()
Returns an object containing all own property descriptors for an object.
Object.getOwnPropertyNames()
Returns an array containing the names of all of the given object's own enumerable and non-enumerable properties.
Object.getOwnPropertySymbols()
Returns an array of all symbol properties found directly upon a given object.
Object.getPrototypeOf()
Returns the prototype (internal Web/JavaScript/Reference/Global objects/Prototype property) of the specified object.
Object.is()
Compares if two values are the same value. Equates all NaN values (which differs from both Abstract Equality Comparison and Strict Equality Comparison).
Object.isExtensible()
Determines if extending of an object is allowed.
Object.isFrozen()
Determines if an object was frozen.
Object.isSealed()
Determines if an object is sealed.
Object.keys()
Returns an array containing the names of all of the given object's own enumerable string properties.
Object.preventExtensions()
Prevents any extensions of an object.
Object.seal()
Prevents other code from deleting properties of an object.
Object.setPrototypeOf()
Sets the object's prototype (its internal Web/JavaScript/Reference/Global objects/Prototype property).
Object.values()
Returns an array containing the values that correspond to all of a given object's own enumerable string properties.

Instance properties

Object.prototype.constructor
Specifies the function that creates an object's prototype.
Object.prototype.__proto__
Points to the object which was used as prototype when the object was instantiated.
[[../../../../../Archive/Web/JavaScript/Object-2|Object.prototype.__noSuchMethod__]]
Allows a function to be defined that will be executed when an undefined object member is called as a method.

Instance methods

Object.prototype.__defineGetter__()
Associates a function with a property that, when accessed, executes that function and returns its return value.
Object.prototype.__defineSetter__()
Associates a function with a property that, when set, executes that function which modifies the property.
Object.prototype.__lookupGetter__()
Returns the function associated with the specified property by the __defineGetter__() method.
Object.prototype.__lookupSetter__()
Returns the function associated with the specified property by the __defineSetter__() method.
Object.prototype.hasOwnProperty()
Returns a boolean indicating whether an object contains the specified property as a direct property of that object and not inherited through the prototype chain.
Object.prototype.isPrototypeOf()
Returns a boolean indicating whether the object this method is called upon is in the prototype chain of the specified object.
Object.prototype.propertyIsEnumerable()
Returns a boolean indicating if the internal [[Web/JavaScript/Data_structures#Properties|ECMAScript Enumerable attribute]] is set.
Object.prototype.toLocaleString()
Calls toString().
Object.prototype.toString()
Returns a string representation of the object.
[[../../../../../Archive/Web/JavaScript/Object-4|Object.prototype.unwatch()]]
Removes a watchpoint from a property of the object.
Object.prototype.valueOf()
Returns the primitive value of the specified object.
[[../../../../../Archive/Web/JavaScript/Object-5|Object.prototype.watch()]]
Adds a watchpoint to a property of the object.

Examples

Using Object given undefined and null types

The following examples store an empty Object object in o:

let o = new Object()
let o = new Object(undefined)
let o = new Object(null)

Using Object to create Boolean objects

The following examples store Boolean objects in o:

// equivalent to o = new Boolean(true)
let o = new Object(true)
// equivalent to o = new Boolean(false)
let o = new Object(Boolean())

Object prototypes

When altering the behavior of existing Object.prototype methods, consider injecting code by wrapping your extension before or after the existing logic. For example, this (untested) code will pre-conditionally execute custom logic before the built-in logic or someone else's extension is executed.

When a function is called, the arguments to the call are held in the array-like "variable" arguments. For example, in the call myFn(a, b, c), the arguments within myFn's body will contain 3 array-like elements corresponding to (a, b, c).

When modifying prototypes with hooks, pass this and the arguments (the call state) to the current behavior by calling apply() on the function. This pattern can be used for any prototype, such as Node.prototype, Function.prototype, etc.

var current = Object.prototype.valueOf;

// Since my property "-prop-value" is cross-cutting and isn't always
// on the same prototype chain, I want to modify Object.prototype: 
Object.prototype.valueOf = function() {
  if (this.hasOwnProperty('-prop-value')) {
    return this['-prop-value'];
  } else {
    // It doesn't look like one of my objects, so let's fall back on 
    // the default behavior by reproducing the current behavior as best we can.
    // The apply behaves like "super" in some other languages.
    // Even though valueOf() doesn't take arguments, some other hook may.
    return current.apply(this, arguments);
  }
}

Since JavaScript doesn't exactly have sub-class objects, prototype is a useful workaround to make a “base class” object of certain functions that act as objects. For example:

var Person = function(name) {
  this.name = name;
  this.canTalk = true;
};

Person.prototype.greet = function() {
  if (this.canTalk) {
    console.log('Hi, I am ' + this.name);
  }
};

var Employee = function(name, title) {
  Person.call(this, name);
  this.title = title;
};

Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee; //If you don't set Object.prototype.constructor to Employee, 
                                           //it will take prototype.constructor of Person (parent). 
                                           //To avoid that, we set the prototype.constructor to Employee (child).


Employee.prototype.greet = function() {
  if (this.canTalk) {
    console.log('Hi, I am ' + this.name + ', the ' + this.title);
  }
};

var Customer = function(name) {
  Person.call(this, name);
};

Customer.prototype = Object.create(Person.prototype);
Customer.prototype.constructor = Customer; //If you don't set Object.prototype.constructor to Customer, 
                                           //it will take prototype.constructor of Person (parent). 
                                           //To avoid that, we set the prototype.constructor to Customer (child).


var Mime = function(name) {
  Person.call(this, name);
  this.canTalk = false;
};

Mime.prototype = Object.create(Person.prototype);
Mime.prototype.constructor = Mime; //If you don't set Object.prototype.constructor to Mime,
                                   //it will take prototype.constructor of Person (parent).
                                   //To avoid that, we set the prototype.constructor to Mime (child).


var bob = new Employee('Bob', 'Builder');
var joe = new Customer('Joe');
var rg = new Employee('Red Green', 'Handyman');
var mike = new Customer('Mike');
var mime = new Mime('Mime');

bob.greet();
// Hi, I am Bob, the Builder

joe.greet();
// Hi, I am Joe

rg.greet();
// Hi, I am Red Green, the Handyman

mike.greet();
// Hi, I am Mike

mime.greet();

Specifications

Specification
ECMAScript (ECMA-262)The definition of 'Object' in that specification.

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
Object Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 3

Opera

Full support 3

Safari

Full support 1

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

Object() constructor Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 3

Opera

Full support 3

Safari

Full support 1

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

assign Chrome

Full support 45

Edge

Full support 12

Firefox

Full support 34

IE

No support No

Opera

Full support 32

Safari

Full support 9

WebView Android

Full support 45

Chrome Android

Full support 45

Firefox Android

Full support 34

Opera Android

Full support 32

Safari iOS

Full support 9

Samsung Internet Android

Full support 5.0

nodejs

Full support 4.0.0

constructor Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 4

Opera

Full support 4

Safari

Full support 1

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

create Chrome

Full support 5

Edge

Full support 12

Firefox

Full support 4

IE

Full support 9

Opera

Full support 11.6

Safari

Full support 5

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12

Safari iOS

Full support 5

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

__defineGetter__

Deprecated'

Chrome

Full support 1

Edge

Full support 12

Firefox Full support 1

Notes'

Full support 1

Notes'

Notes' Starting with Firefox 48, this method can no longer be called at the global scope without any object. A TypeError will be thrown otherwise. Previously, the global object was used in these cases automatically, but this is no longer the case.

IE

Full support 11

Opera

Full support 9.5

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

defineProperties Chrome

Full support 5

Edge

Full support 12

Firefox

Full support 4

IE

Full support 9

Opera

Full support 11.6

Safari

Full support 5

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12

Safari iOS

Full support 5

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

defineProperty Chrome

Full support 5

Edge

Full support 12

Firefox

Full support 4

IE Full support 9


Full support 9


Partial support 8

Notes'

Notes' In Internet Explorer 8, this was only supported on DOM objects and with some non-standard behaviors. This was later fixed in Internet Explorer 9.

Opera

Full support 11.6

Safari Full support 5.1

Notes'

Full support 5.1

Notes'

Notes' Also supported in Safari 5, but not on DOM objects.

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12

Safari iOS Full support 6

Notes'

Full support 6

Notes'

Notes' Also supported in Safari for iOS 4.2, but not on DOM objects.

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

__defineSetter__

Deprecated'

Chrome

Full support 1

Edge

Full support 12

Firefox Full support 1

Notes'

Full support 1

Notes'

Notes' Starting with Firefox 48, this method can no longer be called at the global scope without any object. A TypeError will be thrown otherwise. Previously, the global object was used in these cases automatically, but this is no longer the case.

IE

Full support 11

Opera

Full support 9.5

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

entries Chrome

Full support 54

Edge

Full support 14

Firefox

Full support 47

IE

No support No

Opera

Full support 41

Safari

Full support 10.1

WebView Android

Full support 54

Chrome Android

Full support 54

Firefox Android

Full support 47

Opera Android

Full support 41

Safari iOS

Full support 10.3

Samsung Internet Android

Full support 6.0

nodejs Full support 7.0.0


Full support 7.0.0


Full support 6.5.0

Disabled'

Disabled' From version 6.5.0: this feature is behind the --harmony runtime flag.

freeze Chrome

Full support 6

Edge

Full support 12

Firefox

Full support 4

IE

Full support 9

Opera

Full support 12

Safari

Full support 5.1

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

fromEntries Chrome

Full support 73

Edge

Full support 79

Firefox

Full support 63

IE

No support No

Opera

Full support 60

Safari

Full support 12.1

WebView Android

Full support 73

Chrome Android

Full support 73

Firefox Android

Full support 63

Opera Android

No support No

Safari iOS

Full support 12.2

Samsung Internet Android

No support No

nodejs

Full support 12.0.0

getOwnPropertyDescriptor Chrome

Full support 5

Edge

Full support 12

Firefox

Full support 4

IE Full support 9


Full support 9


Partial support 8

Notes'

Notes' In Internet Explorer 8, this was only supported on DOM objects and with some non-standard behaviors. This was later fixed in Internet Explorer 9.

Opera

Full support 12

Safari

Full support 5

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12

Safari iOS

Full support 5

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

getOwnPropertyDescriptors Chrome

Full support 54

Edge

Full support 15

Firefox

Full support 50

IE

No support No

Opera

Full support 41

Safari

Full support 10

WebView Android

Full support 54

Chrome Android

Full support 54

Firefox Android

Full support 50

Opera Android

Full support 41

Safari iOS

Full support 10

Samsung Internet Android

Full support 6.0

nodejs Full support 7.0.0


Full support 7.0.0


Full support 6.5.0

Disabled'

Disabled' From version 6.5.0: this feature is behind the --harmony runtime flag.

getOwnPropertyNames Chrome

Full support 5

Edge

Full support 12

Firefox

Full support 4

IE

Full support 9

Opera

Full support 12

Safari

Full support 5

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12

Safari iOS

Full support 5

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

getOwnPropertySymbols Chrome

Full support 38

Edge

Full support 12

Firefox

Full support 36

IE

No support No

Opera

Full support 25

Safari

Full support 9

WebView Android

Full support 38

Chrome Android

Full support 38

Firefox Android

Full support 36

Opera Android

Full support 25

Safari iOS

Full support 9

Samsung Internet Android

Full support 3.0

nodejs

Full support 0.12

getPrototypeOf Chrome

Full support 5

Edge

Full support 12

Firefox

Full support 3.5

IE

Full support 9

Opera

Full support 12.1

Safari

Full support 5

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12.1

Safari iOS

Full support 5

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

hasOwnProperty Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 5.5

Opera

Full support 5

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

is Chrome

Full support 30

Edge

Full support 12

Firefox

Full support 22

IE

No support No

Opera

Full support 17

Safari

Full support 9

WebView Android

Full support ≤37

Chrome Android

Full support 30

Firefox Android

Full support 22

Opera Android

Full support 18

Safari iOS

Full support 9

Samsung Internet Android

Full support 2.0

nodejs

Full support 0.10

isExtensible Chrome

Full support 6

Edge

Full support 12

Firefox

Full support 4

IE

Full support 9

Opera

Full support 12

Safari

Full support 5.1

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

isFrozen Chrome

Full support 6

Edge

Full support 12

Firefox

Full support 4

IE

Full support 9

Opera

Full support 12

Safari

Full support 5.1

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

isPrototypeOf Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 9

Opera

Full support 4

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

isSealed Chrome

Full support 6

Edge

Full support 12

Firefox

Full support 4

IE

Full support 9

Opera

Full support 12

Safari

Full support 5.1

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

keys Chrome

Full support 5

Edge

Full support 12

Firefox

Full support 4

IE

Full support 9

Opera

Full support 12

Safari

Full support 5

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12

Safari iOS

Full support 5

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

__lookupGetter__

Deprecated'

Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 11

Opera

Full support 9.5

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

__lookupSetter__

Deprecated'

Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 11

Opera

Full support 9.5

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

preventExtensions Chrome

Full support 6

Edge

Full support 12

Firefox

Full support 4

IE

Full support 9

Opera

Full support 12

Safari

Full support 5.1

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

propertyIsEnumerable Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 5.5

Opera

Full support 4

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

__proto__

Deprecated'

Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 11

Opera

Full support 10.5

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 11

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

seal Chrome

Full support 6

Edge

Full support 12

Firefox

Full support 4

IE

Full support 9

Opera

Full support 12

Safari

Full support 5.1

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 12

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

setPrototypeOf Chrome

Full support 34

Edge

Full support 12

Firefox

Full support 31

IE

Full support 11

Opera

Full support 21

Safari

Full support 9

WebView Android

Full support 37

Chrome Android

Full support 34

Firefox Android

Full support 31

Opera Android

Full support 21

Safari iOS

Full support 9

Samsung Internet Android

Full support 2.0

nodejs

Full support 0.12

toLocaleString Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 5.5

Opera

Full support 4

Safari

Full support 1

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

toSource

Deprecated'Non-standard'

Chrome

No support No

Edge

No support No

Firefox No support 1 — 74

Notes'

No support 1 — 74

Notes'

Notes' Starting in Firefox 74, toSource() is no longer available for use by web content. It is still allowed for internal and privileged code.

IE

No support No

Opera

No support No

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android

Full support 4

Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

nodejs

No support No

toString() Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 3

Opera

Full support 3

Safari

Full support 1

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

valueOf Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 4

Opera

Full support 3

Safari

Full support 1

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

nodejs

Full support Yes

values Chrome

Full support 54

Edge

Full support 14

Firefox

Full support 47

IE

No support No

Opera

Full support 41

Safari

Full support 10.1

WebView Android

Full support 54

Chrome Android

Full support 54

Firefox Android

Full support 47

Opera Android

Full support 41

Safari iOS

Full support 10.3

Samsung Internet Android

Full support 6.0

nodejs Full support 7.0.0


Full support 7.0.0


Full support 6.5.0

Disabled'

Disabled' From version 6.5.0: this feature is behind the --harmony runtime flag.

Legend

Full support  
Full support
No support  
No support
Non-standard. Expect poor cross-browser support.'
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.'
Deprecated. Not for use in new websites.
See implementation notes.'
See implementation notes.
User must explicitly enable this feature.'
User must explicitly enable this feature.


See also

Object by Mozilla Contributors is licensed under CC-BY-SA 2.5.