DEV Community

[Comment from a deleted post]
Collapse
 
somedood profile image
Basti Ortiz • Edited

Well, the two examples don't actually make any objects. Rather, they create constructor functions. Technically they still do create objects since Function instances are also instances of Object, but that is beyond the scope of this comment. Using the new keyword with these constructor functions yields the expected object.

const dood = new person('Fluffykins', 100);
const dood1 = new person1('Foo Bar', 50);

console.log(dood);  // { name: 'Fluffykins', age: 100 }
console.log(dood1); // { name: 'Foo Bar', age: 50 }

Essentially, the two examples are not different. In the olden days of JavaScript, we didn't have the notion of classes, so we had to use functions as "classes" and "constructors".

Now that it's 2019 and we have classes in JavaScript, we don't need to use the function workaround. We can just use the class keyword as our blueprint for instantiating objects rather than using some weird constructor function syntax.

Classes also allow an easier way to inherit properties and methods from a base class. To emulate inheritance for the second example, one would require a bunch of boilerplate code that won't even make sense at first glance, especially for beginners.

Jargon aside, the difference between the two is that the first example is the more formal and correct way of creating blueprints for objects nowadays. In conclusion, prefer the class syntax over the second example.