0

I've been following classical inheritance vs Prototypical inheritance but I've got a doubt.

When we create an object using

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

I read that new creates a 'copy' of an object (hence it's bad?) but at the same time, I read they both are the same thing here(What is the difference between `new Object()` and object literal notation?)

I'm kinda confused if someone can explain what exactly is going on.

Community
  • 1
  • 1
TechnoCorner
  • 4,879
  • 10
  • 43
  • 81
  • Both are same, there is no difference. new doesn't create copy of object it creates an instance of a class. – Sanchay Kumar Feb 03 '17 at 07:36
  • @SanchayKumar Now this is even more confusing, lets say if it's function instead of an object it will create a new function all together right? For example `var Employee = function (name, company, salary) { this.name = name || ""; this.company = company || ""; this.salary = salary || 5000; } var emp1 = new Employee("John","Pluto",3000); ` In this case new object emp1 is created everytime we call it with new. How come it's not the case with new Object()? – TechnoCorner Feb 03 '17 at 08:03
  • **Employee** here is a construction function, calling it with new will create a new object. Calling it with new everytime will create a new object. Same is true for Object, **new Object** will also create a separate plain object everytime. – Sanchay Kumar Feb 03 '17 at 08:15
  • @SanchayKumar then it's a bad practise to do 'new' everytime? instead use {}? is my question.. if that's true..then how are they the same? – TechnoCorner Feb 03 '17 at 23:55
  • Both are same because the output will be same. But, {} will definitely take less space and performance wise it might be evaluated faster. But, those differences are negligible. – Sanchay Kumar Feb 04 '17 at 11:53

0 Answers0