6. Over the Moon, how to avoid new?


Are we able to get the same result, but at the same time avoid "new" in this piece?

function StaffMember (name, discountPercent) {
    this.name = name,
    this.discountPercent = discountPercent
var sally = new StaffMember("Sally",5);
var bob = new StaffMember("Bob",10);
var me = new StaffMember("Nal", 20);


Hi edwinnaelbert,

In Javascript (and many other languages), user-defined Objects in general are only initialised with the new keyword. However, it is technically possible to circumvent this in JS, albeit with some fancy functions.

A comment on this discussion sums up the role of new pretty succinctly:

new exists so that you don't have to write factory methods to construct/copy functions/objects. It means, "Copy this, making it just like its parent 'class'; do so efficiently and correctly; and store inheritance info that is accessible only to me, JS, internally".

Hope this helps!


Thank you a lot! From now I won't be afraid of using new keyword so much)