Properties and methods of the Object() constructor

Introduction

Object() is a function that can be considered the “mother” of all objects. All objects in JavaScript are derived from this function. Creating objects is possible in several ways, but the following way best explains the connection between the constructor function and the object:

A constructor function has a number of methods and two properties: “Object.length” (a very strange property that always returns 1) and “Object.prototype”

EXPLANATION:
Functions in javascript are “First class citizen”, therefore they support all operations available to other types. Among other types of functions, they also support the characteristics of objects, so functions and objects have their own properties and methods. One of the most important features of the constructor function is the Object.prototype property, through which the constructor function passes basic properties and methods to all objects in JavaScript.

“Object.prototype” property of constructor f

function object

“Object.prototype” does not have a “smart” name, but a generic one, created on the basis of belonging to a function (property of the constructor function Object()). This property of the constructor function (of type “object”) is in charge of passing methods and properties to all newly generated objects. This object delegates all its properties and methods to the newly created objects, so they are available to them from the very start. Object.prototype is at the end of the inheritance chain and all the properties and methods it possesses are available to any javascript object.

  • PROPERTIES:
    1. .constructor
    2. __proto__
  • METHODS:
    1. hasOwnProperty()
    2. isPrototypeOf()
    3. propertyIsEnumerable()
    4. toString()
    5. valueOf()

The properties of this object are specific because their property descriptors have negative values (writable=no; enumerable=no; configurable=no), and since the properties are not enumerable, this means they are not accessible through a “for…in” loop. The characteristics of the properties of this object are as follows:

Property attributes of Object.prototype
Writable but
Enumerable but
Configurable but

These properties and methods of “Object.prototypealso delegates objects that are created by inheritance, i.e. to all objects along the inheritance chain. Object.prototype is always at the end of the prototype inheritance chain.

Properties of the “Object.prototype” object

Object.prototype.constructor

This property points to the constructor function from which the original object was created, and if the object does not have its own “personal” constructor function (eg object literal {}, created using the keyword new…), then the javascript engine searches for the .constructor property along the inheritance chain until it is found.

Object.prototype.__proto__

This property (called “dunder proto”, short for “double underscore proto”) points to the parent object from which our object inherits properties and methods. With this property we can even predefine the object from which we want to inherit, although in general we should avoid using this property, because it is very slow, and besides, it is not according to the ES standard, although it is accepted in all browsers (except IE<11). If we really want to use such a property, it is better to use the getPrototypeOf() property, which is accepted by the ES2015 standard, and has the same characteristics as __proto__.

This property can also be used chained, so it can reach any prototype object along the entire inheritance chain:

Using this non-standard property we can make another object our prototype object by simply assigning it another object.

Example

Methods of the Object.prototype object

Object.prototype.isPrototypeOf()

This method checks if the object exists in one’s inheritance chain.

Object.prototype.hasOwnProperty()

Method “hasOwnPropery()” checks whether a certain object (without objects in the protoytpe chain) has any property, returns a boolean value. This property will check even the non-enumerable private properties of the object.

NOTE:
In addition to the previous method, the following approaches to this problem can often be seen:

a) Operator “in”

The in operator is used to test all objects in the prototype chain. With this operator, it is checked whether the object itself has any property, but all objects that delegate properties in the prototype chain are also checked.

b) operator !==

This method returns the same result as the operator “in” (all properties in the prototype chain), however this approach is not recommended because it is the slowest.

Object.prototype.propertyIsEnumerable()

Using the “propertyIsEnumerable()” method, it checks whether the property descriptor enumerable is set to the value true, i.e. whether the property is iterable and visible through property iterations.

Example

Object.prototype.toString()

This method returns a string representation of the object.

Updating Native Objects Prototypes

Since they are also native objects (String, Array, Number…).objects, then the mechanism of delegated inheritance can be applied to them as well, and even adding new properties to the initial prototype object:

We can now use the trim() method on all strings:

The disadvantage of this approach is that newer versions of JavaScript may have new functionality called “trim” so our method would override the standardized method. This problem could be solved as follows:

Methods of a constructor function

Object.create()

With the Object.create() method, an object is generated, which is defined immediately upon creation, from which object the methods are delegated via the “prototype chain” (properties are not copied!). The syntax for creating an object is:

The Object.create() method performs the following tasks in the background:

  1. New object is being created
  2. The new object is linked to the object from which it was created and thus inserted into the inheritance chain, after which it can have access to its delegated methods
  3. Optionally, it can also define new object properties.

Read more about this in the article “1001 ways to create objects”.

Object.assign()

This property copies all properties of one or more objects where the enumerable property descriptor is set to true and then returns the object.

Cloning objects

NOTE:
This property only copies “personal object properties”, not properties along the prototype inheritance chain.

Merging multiple objects into one

This property can be used with multiple objects, so it should be noted that duplicate properties are not displayed as two but as one property.

Object.getPrototypeOf()

With this method we get the prototype object of a certain object.

This method did not work best during the ES5 standard, but it has been improved with the ES2015 standard.

Example

Object.setPrototypeOf()

Extending the constructor function is done using the Object.create() method, which defines a prototype object of one object with another (read a little more about this in the article 1001 ways to create objects in JS.

However, the object literal does not have its own constructor function, so it is necessary to use the Object.setPrototypeOf() method, which assigns a prototype object to another object.

NOTE:
This method is a replacement for the non-standard method Object.prototype.__proto__ (read more about it in the article “Prototype inheritance”), which was not recommended for use because it is very slow, the same goes for the method Object.setPrototypeOf(), so avoid it!

Example

Object.defineProperty()

The Object.defineProperty() method defines or modifies the properties of an object.

Example

This property can be used to create private properties using special methods called “get-era” and “set-era”.

With this mechanism, every time we want to interact with the “fullName property we do so with either the “get_fullName()” or the “set_fullName()” method.

Object.is()

JS is a “loosely typed” language, which in certain situations implicitly converts types. Converting when comparison with the “==” operator often gives unwanted results.

Therefore “===” is often used, although comparison with this operator can also give an illogical result:

Example

All these problems are solved by the Object.is() method, which checks whether two objects are equal, and it does so without fail.

Example

Example

It also resolves lesser-known JavaScript inconsistencies and returns the appropriate response, such as:

See a table of comparison results using the ==, === operators and the Object.is() method:

comparison table

Object.values

Creates an array of all the values of the object’s “enumerable” properties (no properties along the inheritance chain).

Example

NOTE:
The order of the members is random, just like in the for..in loop!

Object.keys

Creates an array of all keys of the object’s “enumerable” properties (no properties along the inheritance chain).

Example

NOTE:
The order of the members is random, just like in the for..in loop!

Object.entries

Creates an array of all [key, value] pairs of the object’s “enumerable” properties (no properties along the inheritance chain).

Example

NOTE:
The order of the members is random, just like in the for..in loop!

Object.getOwnPropertyNames()

This method returns an array of all object keys and even those properties where the descriptor is enumerable: false

NOTE:

The “for..in” loop can also be used to view the object’s properties, which goes through all the properties (which have enumerable: true set) of the object and the entire prototype chain.

Object.getOwnPropertyDescriptor()

Property descriptors of an object are obtained using the method “Object.getOwnPropertyDescriptor()”:

Example

Object.getOwnPropertyDescriptors()

With this method we get all “personal” (not inherited)properties and their property descriptors:

Used to clone objects

Object.PreventExtension()

If we want to prevent adding new properties to an object, use the “Object.PreventExtension()” method

Object.seal()

The “Object.seal()” method takes an object and calls the previous preventExtension() method for it, and also sets the configurable descriptor to false (=> configurable: false + prevented adding new properties). The result of this method is that no new properties can be added to the object, no existing properties can be deleted, and no descriptors other than the “value”

descriptor can be changed.

Object.freeze()

The “Object.freeze()” method takes over an existing object, and calls the “Object.seal()” method for it, and changes the value of the “writable” descriptor to false (=> writable: false, configurable: false + preventing the addition of new properties) for all properties of the object. This method provides the highest level of object immutability.