What is the difference between assigning a variable as an object and assigning a variable as an object notation / constructor designator?

I just noticed that when assigning a variable as Object type of the variable is a β€œfunction”, whereas if I assign it as an empty object using the object notation of the literal {} or create an instance as new Object , the typeof variable is an object. What is the difference here?

Note that I do not set the difference between writing an object literal and constructor notation.

enter image description here

+5
source share
4 answers

The global symbol Object refers to the function of the constructor of objects. Assigning an Object variable to a variable simply copies this link and is completely different from assigning a link to a new empty object ( {} ).

Perhaps you are thinking:

 var a = new Object(); var b = {}; 

These two statements do the same.

+7
source

When you assign a variable as an object, it refers to the Object object on it, whereas when you make a new Object or {}, it simply creates a simple object with its constructor method parent Object

You can learn more about screenshots.

enter image description here

enter image description here enter image description here

+4
source

a = Object; does not create a new object. It assigns a constructor function to your variable a . To create a new object, use this code: a = new Object();

+2
source

let a = new Object();

Creates a new object printed as {} .

let a = {};

Creates a new object printed as {} . Not completely different from the approach above.

let a = Object;

Object is the constructor that calls it will return a new object, but it's better to use the new keyword for code conventions. I personally prefer to use let a = {}; in javascript. A constructor is a function, so the console told you that it just created a function.

+1
source

Source: https://habr.com/ru/post/1271942/


All Articles