0

What is the difference between these two methods of declaring an object, from which you can make an instance? Are there pros and cons to each?

//Method 1:
var myThing1 = function(){
  this.a= 2,
  this.m = function() {
    return this.a + 1;
  }
}   
var myThing1Instance = new myThing1();

//Method 2:
var myThing2 = {
  a: 2,
  m: function() {
    return this.a + 1;
  }
};
var myThing2Instance = Object.create(myThing2)
dt1000
  • 3,684
  • 6
  • 43
  • 65
  • If you feel the duplicates aren't appropriate, please edit your question and explain why you think so. I will then gladly re-open your question – Phil Oct 02 '18 at 04:57
  • Not a duplicate. I'm not asking about prototype vs this. I'm asking about using a function and 'new' vs using an object literal and Object.create. What is the difference? – dt1000 Oct 03 '18 at 13:30
  • Keep in mind that Object.create assigns a prototype to an object so what I'm seeing is explained in those other posts – Phil Oct 03 '18 at 14:48
  • Is there any difference between Thing1Instance, and Thing2Instance? – dt1000 Oct 04 '18 at 14:13
  • Yes. I'm not entirely sure I understand your question; you can inspect them in your console to see the differences. – Phil Oct 08 '18 at 04:49

0 Answers0