admin管理员组文章数量:1429629
Pretty silly question, but I was thinking about this randomly earlier and I've never tried doing it... let's say I have a class like so:
function Drug () {
this.name = '';
this.type = '';
}
Later on in the page I set some values to this Drug:
var drug = new Drug();
drug.name = 'Testing';
drug.type = 'TestType';
Is there a simple way to let's say restart this instance of Drug to its defaults without just doing something like:
var drug2 = new Drug();
Like some sort of reset function I can create inside of Drug
to just do drug.reset();
?
I tried: this.clear = function () { return new Drug(); }
but that doesn't do the trick...
Pretty silly question, but I was thinking about this randomly earlier and I've never tried doing it... let's say I have a class like so:
function Drug () {
this.name = '';
this.type = '';
}
Later on in the page I set some values to this Drug:
var drug = new Drug();
drug.name = 'Testing';
drug.type = 'TestType';
Is there a simple way to let's say restart this instance of Drug to its defaults without just doing something like:
var drug2 = new Drug();
Like some sort of reset function I can create inside of Drug
to just do drug.reset();
?
I tried: this.clear = function () { return new Drug(); }
but that doesn't do the trick...
-
1
What does
Drug.call(drug)
do? jsfiddle/dRBpm/1 – Rikonator Commented Jan 5, 2013 at 22:27
3 Answers
Reset to default 5You're overthinking it just a little. Instead of directly initializing those properties in the constructor, create an init()
method which initializes them, called by the constructor. You can then call it to reinitialize the object's properties.
function Drug () {
this.init();
}
Drug.prototype.init = function() {
this.name = '';
this.type = '';
}
// Instantiate:
var d = new Drug();
d.name = 'Testing';
console.log(d);
// Drug {name: "Testing", type: ""}
// Re-initialize it
d.init();
console.log(d);
// Drug {name: "", type: ""}
This certainly isn't the only way you could handle it, but the first that es to mind.
With this way you can redifine later your default values or add/remove them
function Drug() {
this.init();
}
// initializer
(function(obj) {
obj.prototype.init = function() {
for ( var k in obj.prototype ) {
this[ k ] = obj.prototype[ k ];
}
}
}(Drug));
// define here all default properties
Drug.prototype.name = '';
Drug.prototype.type = '';
var drug = new Drug();
drug.name = 'Testing';
drug.type = 'TestType';
console.log( drug );
drug.init();
console.log( drug );
try it yourself here: http://jsfiddle/bukfixart/vzsFw/
A rather general form of providing publicly-accessible properties in a class.
From here, you could build in a config object (in the constructor), to run overtop of the defaults.
var Drug = function (cfg) {
var defaults = {
key : "val"
};
this.init = function () {
var key, val;
for (key in defaults) {
if (defaults.hasOwnProperty (key)) {
val = defaults[key];
this[key] = val;
}
}
if (cfg) {
for (key in cfg) {
if (cfg.hasOwnProperty(key)) {
val = cfg[key];
this[key] = val;
}
}
}
};
this.reset = this.init;
this.init();
};
Here .reset()
is just sugar on top of .init()
.
If you wanted, though, you could have the application of defaults and the application of config separated, so that .reset()
ignored the config and just used defaults...
There are a billion ways of doing this, but here's one that would let you keep private state of this particular created object, while resetting the publicly-exposed properties.
本文标签: Reseting a Javascript Class to its Default valuesStack Overflow
版权声明:本文标题:Reseting a Javascript Class to its Default values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745449173a2658804.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论