I am in a bit of a quandary, as I wish to create a function that will clear all the properties of an object and make it available to all instances of that object. To achieve this, I have added a prototype function called clear(). Here is the code snippet:
(function () {
Supplier.$inject = [];
angular.module('webclient').factory('Supplier', Supplier);
function Supplier() {
Supplier.prototype = {
clear: function () {
for (var key in this) {
//skip loop if the property is from prototype
if (this.hasOwnProperty(key))
continue;
console.log("key:" + key);
this[key] = undefined;
}
},
}
return Supplier;
};
})();
Therefore, my goal is to be able to clear all properties of the current supplier object. For instance, if the supplier object had the following properties:
SupplierID:21, Email:None
I would like to reset these properties to undefined. This can be achieved using the following steps:
var supplier = new Supplier();
supplier.SupplierID = 21; supplier.Email = "None";
To set each property to undefined, I would use the following code:
supplier.clear();
Any suggestions or ideas on how to improve this implementation?
Thank you!