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.
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Well, the two examples don't actually make any objects. Rather, they create constructor functions.
Technically they still do create objects sinceUsing theFunction
instances are also instances ofObject
, but that is beyond the scope of this comment.new
keyword with these constructor functions yields the expected object.Essentially, the two examples are not different. In the olden days of JavaScript, we didn't have the notion of
class
es, so we had to use functions as "classes" and "constructors".Now that it's 2019 and we have
class
es in JavaScript, we don't need to use the function workaround. We can just use theclass
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.