如果我有一个对象,我想要“继承”方法从一个“超级对象”,以确保一致性。它们将是混合变量。修订的
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();这似乎不正确,特别是它没有看到self.interval,并且无法清除它。
如果存在其他JavaScript继承方法,我是开放的,但我确实需要开始将这些东西封装到父继承中。有三到四个函数是相同的,但有时会被更改,这意味着我必须运行十几个文件来进行更改,而不是简单地更改父函数。
研究一下我尝试过的一些建议,以便更清楚地定义我想要实现的功能。理想情况下,所有“子”都有两个唯一的设置(名称、间隔、配置设置)和一个getData()方法,而父方法则管理启动、停止、日志记录和其他任何东西。
发布于 2010-09-22 17:56:39
..。
/**
* Extend a constructor with a subtype
* @param {Function} superCtor Constructor of supertype
* @param {Function} subCtor Constructor of subtype
* @return {Function} Constructor of subtype
*/
var extend = (function(){
return function (superCtor, subCtor) {
var oldProto=subCtor.prototype;
subCtor.prototype=clone(superCtor.prototype);
return merge(subCtor.prototype, oldProto).constructor=subCtor;
}
function Clone(){}
/**
* Clone an object
* @param {Object} obj Object to clone
* @return {Object} Cloned object
*/
function clone (obj) { Clone.prototype=obj; return new Clone() }
/**
* Merge two objects
* @param {Object} dst Destination object
* @param {Object} src Source object
* @return {Object} Destination object
*/
function merge (dst, src) {
for (var p in src) if (src.hasOwnProperty(p)) dst[p]=src[p];
return dst;
}
}());发布于 2010-09-22 17:45:54
首先,我不认为var this.interval是好的。var关键字用于定义变量,但在您的示例中,您引用的是对象。
其次,如果您想将"interval“声明为cobj的一种方法,则必须将函数的主体包装在一个函数中。
所以,这样对我来说是可行的:
var sobj = function()
{
this.close = function()
{
clearInterval(this.interval);
}
}
var cobj = function()
{
this.initInterval = function(){ this.intervalid = setInterval(function(){ alert("test")}, 5000)};
this.intervalid = null;
}
cobj.prototype = new sobj();
var inst = new cobj();
inst.initInterval();在定义了构造函数之后,我创建了一个"cobj“对象的实际实例,然后调用"initInterval”来初始化"setInterval“。
UPD:根据@MooGoo的评论更新代码。
发布于 2010-09-22 17:55:31
使用此继承方法,只能混合“上游”变量。您的子对象将能够看到其原型的公共属性,但原型无法看到其子对象的属性。一定是自成一体的。
(编辑:我刚刚注意到你也在使用"self“,而没有在sobj中声明它。)
sobj = function()
{
var self = this;
self.close = function()
{
clearInterval(self.interval);
}
self.interval = null;
}
cobj = function()
{
var self = this;
self.interval = setInterval(function(){ /* Do something */}, 1000);
}
// set cobj.prototype - see explanation below关于如何正确设置原型(以及深入了解继承在JS中是如何工作的),请参阅Douglas的“JavaScript: Good Parts”一书。
他实际上已经在他的网站上发布了如何正确设置原型。一定要使用不接触Object.prototype的版本,因为如果您更改它,许多脚本(启动程序的jQuery)都会中断。
https://stackoverflow.com/questions/3771971
复制相似问题