Skip to content Skip to sidebar Skip to footer

Object() Without The New Keyword Or .create Method

I was looking on MDN for a polyfill for Array.prototype.includes() and I came across the Object() syntax below: if (!Array.prototype.includes) { Array.prototype.includes = funct

Solution 1:

Object(...) converts the passed value to an object. It simply returns the value itself if it is already an object, otherwise wit will create a new object and return that.

From the spec:

When Object is called as a function rather than as a constructor, it performs a type conversion.

Example:

var obj = Object("foo");
// same as // var obj = new String("foo");

what is the purpose of this in this case?

It ensures that the value is an object, not a primitive. The implementation just follows the spec:

  1. Let O be ? ToObject(this value).

Post a Comment for "Object() Without The New Keyword Or .create Method"