BostonJohn
BostonJohn

Reputation: 2661

what does calling Object without using new do?

I've seen code that goes along the line of

Object( existingObject ).myMethod();

Is this different than calling existingObject.myMethod() directly? More generally, what does Object(x) do?

Upvotes: 0

Views: 51

Answers (1)

Hacknightly
Hacknightly

Reputation: 5144

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.

In your case, since the value is an object already, it will just return the value existingObject. So, no, it is not really different from calling existingObject.myMethod directly.

Documentation

Upvotes: 2

Related Questions