Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between assigning a variable as Object and assigning a variable as Object Literal Notation/Object Constructor Notation? [duplicate]

Tags:

javascript

I just happened to notice that when assigning a variable as an Object, the typeof the variable is a 'function' whereas if I assign it as an empty object using object literal notation {} or instantiate as a new Object, the typeof variable is an object. What's the difference here?

Please note, I'm not asking the difference between Object literal notation and constructor notation.

enter image description here

like image 486
Ramesh N Avatar asked Sep 19 '17 12:09

Ramesh N


People also ask

Whats the difference between an object and a object literal?

Objects created using object literal are singletons, this means when a change is made to the object, it affects the object entire the script. Whereas if an object is created using constructor function and a change is made to it, that change won't affect the object throughout the script.

What is the difference between creating an object using literal notation and creating an object using a constructor?

The main difference here is what you can do with it. With the constructor function notation you create an object that can be instantiated into multiple instances (with the new keyword), while the literal notation delivers a single object, like a singleton.

What is object and object literal in JavaScript?

In plain English, an object literal is a comma-separated list of name-value pairs inside of curly braces. Those values can be properties and functions. Here's a snippet of an object literal with one property and one function. var greeting = {

When would you create an object using literal notation vs constructor notation?

Now the question is when should we be using Literal notation and constructor notation. The point is when we need only one instance with the same values then we can go with the literal notation else if we may need multiple instances, like the instance of a class, we can go for the constructor notation.


3 Answers

The global symbol Object refers to the Object constructor function. Assigning Object to a variable just makes a copy of that reference, and is completely different from assigning a reference to a new empty object ({}).

Perhaps you're thinking of:

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

Those two statements do the same thing.

like image 190
Pointy Avatar answered Oct 16 '22 07:10

Pointy


When you assign a variable as an Object it references the Object function onto it whereas when you do new Object or {} it just creates a plain object with the constructor method of it's parent Object

You can understand more with screenshots

enter image description here

enter image description here enter image description here

like image 4
Manzur Khan Avatar answered Oct 16 '22 08:10

Manzur Khan


a = Object; doesn't create a new object. It assigns the constructor function to your variable a. To create a new object, use this code: a = new Object();

like image 2
JSON Derulo Avatar answered Oct 16 '22 08:10

JSON Derulo