最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Reseting a Javascript Class to its Default values - Stack Overflow

programmeradmin2浏览0评论

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...

Share Improve this question asked Jan 5, 2013 at 22:21 Mark Pieszak - Trilon.ioMark Pieszak - Trilon.io 67.4k15 gold badges83 silver badges96 bronze badges 1
  • 1 What does Drug.call(drug) do? jsfiddle/dRBpm/1 – Rikonator Commented Jan 5, 2013 at 22:27
Add a ment  | 

3 Answers 3

Reset to default 5

You'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.

发布评论

评论列表(0)

  1. 暂无评论