If I have an object that I want to "inherit" methods from a "super object" to ensure consitency. They will be intermingling variables. REVISED
ParentObj = function()
{
var self = this;
this.interval = null
this.name = "";
this.timeout = 1000;
this.stop = function()
{
clearInterval(self.interval);
};
this.start = function()
{
self.log("Starting up");
setTimeout(function(){
self.getData(true);
self.interval = setInterval(function(){
self.getData();
}, self.timeout)
}, 1000);
};
this.log = function(msg)
{
require("sys").log(self.name + ": " + msg);
};
this.getData = function(override)
{
if(override || self.interval)
{
/**
* Allow
*/
self.log(new Date());
}
else
{
self.log("Unable to override and no interval");
}
}
}
ChildObj = function()
{
var self = this;
this.name = "Child";
this.timeout = 500;
this.start();
setTimeout(function(){
self.stop();
}, 2000);
}
ChildObj.prototype = new ParentObj();
var c = new ChildObj();
This doesn't seem to work correctly, specifically it not seeing the self.interval
and is unable to clear it.
I'm open to other JavaScript inheritance methods, if they exist, but I really need to start encapsulating stuff off into the parent. There are three or four functions that are identical, but being changed at times and this means I have to run through a dozen files to make the change, rather then simply altering the parent.
Working through some of the suggestions I've tried to more clearly define what sort of functionality I'm going for. Ideally all the "children" will have a couple of unique settings (name, interval, config settings) and a getData()
method while the parent manages starting, stopping, logging, and anything else.