Is there a way to access the parent object when calling a function contained inside that object as a constructor without explicitly referring to it? Take a look at this scenario:
var customers = {
// Number of customers
count: 0,
// Naturally _this is undefined
_this: this,
// Constructor function
CreateCustomer: function (name, email) {
this.name = name;
this.email = email;
// How can I access the containing object without explicitly referencing it?
customers.count++;
},
getCount: function () {
return this.count;
},
};
If I attempt to call the CreateCustomer
function as a constructor, then this
will point to the empty object created by the new
operator. For example:
var customer = new customers.CreateCustomer('Alice', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="14667d777f54716c75796478713a777b79">[email protected]</a>')
In such cases, how can I access the parent object customers
without directly mentioning it?