Methods
(static) assign(target, …sources) → {object}
Copies all enumerable own properties from one or more source objects to a target object. MDN
Example
var object1 = { a: 1, b: 2, c: 3 };
Object.assign(object1, { c: 4, d: 5 });
// Returns: Object { a: 1, b: 2, c: 4, d: 5 }
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
target |
object | The target object — what to apply the sources’ properties to, which is returned after it is modified. |
|
sources |
Object |
<repeatable> |
The source object(s) — objects containing the properties you want to apply. |
Returns:
- The target object.
- Type
- object
(static) create(proto, propertiesopt) → {object}
Creates a new object, using an existing object as the prototype of the newly created object. MDN
Example
var person = {
isHuman: false,
printIntroduction: function () {
console.log('My name is ' + this.name + '. Am I human? ' + this.isHuman);
}
};
var me = Object.create(person);
me.name = 'Matthew'; // "name" is a property set on "me", but not on "person"
me.isHuman = true; // inherited properties can be overwritten
me.printIntroduction();
// expected output: "My name is Matthew. Am I human? true"
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
proto |
object | The object which should be the prototype of the newly-created object. |
|
properties |
object |
<optional> |
Returns:
- A new object with the specified prototype object and properties.
- Type
- object
(static) defineProperties(obj, props) → {object}
Defines new or modifies existing properties directly on an object, returning the object. MDN
Example
var object1 = {};
Object.defineProperties(object1, {
property1: { value: 42, writable: true },
property2: {}
});
console.log(object1.property1); // Outputs: 42
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | The object on which to define or modify properties. |
props |
object | An object whose keys represent the names of properties to be defined or modified and whose values are objects describing those properties. |
Returns:
- The object that was passed to the function.
- Type
- object
(static) defineProperty(obj, prop, descriptor) → {object}
Defines a new property directly on an object, or modifies an existing property on an object, and returns the object. MDN
Example
var object1 = {};
Object.defineProperty(object1, 'property1', {
value: 42,
writable: false
});
object1.property1 = 77; // throws an error in strict mode
console.log(object1.property1); // Outputs: 42
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | The object on which to define the property. |
prop |
string | The name or Symbol of the property to be defined or modified. |
descriptor |
object | The descriptor for the property being defined or modified. |
Returns:
- The object that was passed to the function.
- Type
- object
(static) freeze(obj) → {object}
Freezes an object. A frozen object can no longer be changed. MDN
Example
var obj = { prop: 42 };
Object.freeze(obj);
obj.prop = 33; // Throws an error in strict mode
console.log(obj.prop); // Outputs: 42
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | The object to freeze. |
Returns:
- The object that was passed to the function.
- Type
- object
(static) getOwnPropertyDescriptor(obj, prop) → {*}
From ECMAScript 5.1 ECMA-262 MDN
Example
var object1 = { property1: 42 };
var descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
console.dir(descriptor1.configurable); // Outputs: true
console.log(descriptor1.value); // Outputs: 42
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | The object in which to look for the property. |
prop |
string | The name or Symbol of the property whose description is to be retrieved. |
Returns:
- A property descriptor of the given property if it exists on the object, undefined otherwise.
- Type
- *
(static) getOwnPropertyNames(obj) → {Array.<string>}
Returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object. MDN
Example
var object1 = { a: 1, b: 2, c: 3 };
console.dir(Object.getOwnPropertyNames(object1));
// expected output: Array ["a", "b", "c"]
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | The object whose enumerable and non-enumerable properties are to be returned. |
Returns:
- An array of strings that corresponds to the properties found directly in the given object.
- Type
- Array.<string>
(static) getPrototypeOf(obj) → {true|null}
From ECMA-262 5.1 edition MDN
Example
var prototype1 = {};
var object1 = Object.create(prototype1);
console.dir(Object.getPrototypeOf(object1) === prototype1); // Outputs: true
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | The object whose prototype is to be returned. |
Returns:
- The prototype of the given object. If there are no inherited properties, null is returned.
- Type
- true | null
(static) is(value1, value2) → {boolean}
Determines whether two values are the same value. MDN
Example
Object.is('foo', 'foo'); // true
Object.is('foo', 'bar'); // false
Object.is([], []); // false
var foo = { a: 1 };
var bar = { a: 1 };
Object.is(foo, foo); // true
Object.is(foo, bar); // false
Parameters:
Name | Type | Description |
---|---|---|
value1 |
* | The first value to compare. |
value2 |
* | The second value to compare. |
Returns:
- A Boolean indicating whether or not the two arguments are the same value.
- Type
- boolean
(static) keys(object) → {Array.<string>}
Returns an array of a given object's own enumerable property names, in the same order as we get with a normal loop. MDN
Example
var object1 = { a: 'somestring', b: 42, c: false };
Object.keys(object1);
// Returns: Array ['a', 'b', 'c']
Parameters:
Name | Type | Description |
---|---|---|
object |
object |
Returns:
- An array of strings
- Type
- Array.<string>
(static) values(obj) → {Array}
Returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well). MDN
Example
const object1 = { a: 'somestring', b: 42, c: false };
Object.values(object1);
// Returns: Array ['somestring', 42, false]
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | The object whose enumerable own property values are to be returned. |
Returns:
- An array containing the given object's own enumerable property values.
- Type
- Array