Object methods in javascript
The object prototype in javascript comes with many built in methods that are very useful when working with this type of data. Let’s take a look at the main ones and learn a bit more about them.
assign( )
The Object.assign()
method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.
create( )
The Object.create()
method creates a new object, using an existing object as the prototype of the newly created object.
defineProperties( )
The Object.defineProperties()
method defines new or modifies existing properties directly on an object, returning the object.
defineProperty( )
The static method Object.defineProperty()
defines a new property directly on an object, or modifies an existing property on an object, and returns the object.
freeze( )
The Object.freeze()
method freezes an object. A frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed. In addition, freezing an object also prevents its prototype from being changed. freeze()
returns the same object that was passed in.
fromEntries( )
The Object.fromEntries()
method transforms a list of key-value pairs into an object.
getOwnPropertyDescriptor( )
The Object.getOwnPropertyDescriptor()
method returns an object describing the configuration of a specific property on a given object (that is, one directly present on an object and not in the object's prototype chain). The object returned is mutable but mutating it has no effect on the original property's configuration.
getOwnPropertyDescriptors( )
The Object.getOwnPropertyDescriptors()
method returns all own property descriptors of a given object.
getOwnPropertyNames( )
The Object.getOwnPropertyNames()
method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object.
isPrototypeOf( )
The isPrototypeOf()
method checks if an object exists in another object's prototype chain.
keys( )
The Object.keys()
method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would.
preventExtensions( )
The Object.preventExtensions()
method prevents new properties from ever being added to an object (i.e. prevents future extensions to the object).
seal( )
The Object.seal()
method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.
values( )
The Object.values()
method 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 only difference is that a for...in
loop enumerates properties in the prototype chain as well.)