Object

Object 建構式可用於建立物件包裝(object wrapper)。

語法

// Object initialiser or literal
{ [ nameValuePair1[, nameValuePair2[, ...nameValuePairN] ] ] }

// Called as a constructor
new Object([value])

參數

nameValuePair1, nameValuePair2, ... nameValuePairN

Pairs of names (strings) and values (any value) where the name is separated from the value by a colon.

value

任意值。

描述

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().

也可以參考 object initializer / literal syntax.

Object 建構式屬性

Object.length

Has a value of 1.

Object.prototype

Allows the addition of properties to all objects of type Object.

Object 建構式方法

Object.assign()

Creates a new object by copying 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() (en-US) 實驗性質

Returns an array of a given object's own enumerable property [key, value] pairs.

Object.freeze()

Freezes an object: other code can't delete or change any properties.

Object.getOwnPropertyDescriptor() (en-US)

Returns a property descriptor for a named property on an object.

Object.getOwnPropertyDescriptors() (en-US)

Returns an object containing all own property descriptors for an object.

Object.getOwnPropertyNames() (en-US)

Returns an array containing the names of all of the given object's own enumerable and non-enumerable properties.

Object.getOwnPropertySymbols() (en-US)

Returns an array of all symbol properties found directly upon a given object.

Object.getPrototypeOf()

Returns the prototype of the specified object.

Object.is() (en-US)

Compares if two values are distinguishable (ie. the same)

Object.isExtensible() (en-US)

Determines if extending of an object is allowed.

Object.isFrozen() (en-US)

Determines if an object was frozen.

Object.isSealed() (en-US)

Determines if an object is sealed.

Object.keys()

Returns an array containing the names of all of the given object's own enumerable properties.

Object.preventExtensions()

Prevents any extensions of an object.

Object.seal() (en-US)

Prevents other code from deleting properties of an object.

Object.setPrototypeOf() (en-US)

Sets the prototype (i.e., the internal [[Prototype]] property)

Object.values() (en-US) 實驗性質

Returns an array of a given object's own enumerable values.

Object 物件實體與 Object 原型物件

All objects in JavaScript are descended from Object; all objects inherit methods and properties from Object.prototype, although they may be overridden. For example, other constructors' prototypes override the constructor property and provide their own toString() methods. Changes to the Object prototype object are propagated to all objects unless the properties and methods subject to those changes are overridden further along the prototype chain.

屬性

Object.prototype.constructor (en-US)

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.

方法

Object.prototype.__defineGetter__() (en-US)

Associates a function with a property that, when accessed, executes that function and returns its return value.

Object.prototype.__defineSetter__() (en-US)

Associates a function with a property that, when set, executes that function which modifies the property.

Object.prototype.__lookupGetter__() (en-US)

Returns the function bound as a getter to the specified property.

Object.prototype.__lookupSetter__() (en-US)

Returns the function bound as a setter to the specified property.

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() (en-US)

Returns a boolean indicating whether the object this method is called upon is in the prototype chain of the specified object.

Object.prototype.propertyIsEnumerable() (en-US)

Returns a boolean indicating whether the specified property is the object's enumerable own property.

Object.prototype.toLocaleString() (en-US)

Calls toString() (en-US).

Object.prototype.toString() (en-US)

Returns a string representation of the object.

Object.prototype.valueOf() (en-US)

Returns the primitive value of the specified object.

範例

Using Object given undefined and null types

下面例子儲存一個空物件至變數 o

var o = new Object();
var o = new Object(undefined);
var o = new Object(null);

Using Object to create Boolean objects

下面例子儲存 Boolean 物件在 o:

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

規範

Specification
ECMAScript Language Specification
# sec-object-objects

瀏覽器相容性

BCD tables only load in the browser

參見