2011-05-18 72 views
4
// Base class 
var Base = function() { 
    this._value = 'base'; 
}; 
Base.prototype = { 
    constructor: Base, 
    // By function 
    getValue: function() { 
     return this._value; 
    }, 
    // By getter 
    get value() { 
     return this._value; 
    } 
}; 

// Sub class extends Base 
var Sub = function() { 
    this._value = 'sub'; 
}; 
Sub.prototype = { 
    constructor: Sub 
}; 
// Pass over methods 
Sub.prototype.getValue = Base.prototype.getValue; 
Sub.prototype.value = Base.prototype.value; 

// --- 

var mySub = new Sub(); 
alert(mySub.getValue()); // Returns 'sub' 
alert(mySub.value);  // Returns 'undefined' 

乍一看似乎mySub.value应返回相同的mySub.getValue(),但你可以看到它,而不是返回undefined。显然,getter没有找到父范围作为子实例(mySub),而是一个不存在的Base实例。复制的JavaScript getter/setter方法到另一个原型对象

有没有解决这个其他任何方式比具有相同的干将分配到新的原型?

+0

我知道这并不能回答你的问题......但为什么你甚至需要吸气剂?这不是不必要的复杂性吗? – 2011-05-18 04:00:35

+0

@Micheal没有。 – Kayla 2011-05-18 04:12:18

+0

@Michael在这个例子中,显然这是没有必要的,我只是写了一个清楚的概念来说明我面临的问题。 在我的实际应用中,获取/设置特定的属性我需要触发等功能,并且代码时。 – 2011-05-18 04:20:56

回答

10
Sub.prototype.__defineGetter__('value', Base.prototype.__lookupGetter__('value')); 

尝试。

+0

完美!正是我之后,谢谢。 – 2011-05-18 04:23:02

+0

我的荣幸。 :) – Kayla 2011-05-18 04:30:47

+8

__defineGetter__是非标准的,不符合ECMA-262标准。有没有另外一种方法呢? – 2012-01-03 09:40:04

5

我认为,如果你指定

Sub.prototype = new Base() 

的问题是,当你直接从Base.prototype.value分配给它的构造函数永远不会运行它会工作。该值将不存在,直到你有基类的一个实例(通过new

这是我的延长Function实现继承典型方法:

Function.prototype.Extend = function(superClass) { 
    this.prototype = new superClass(); 

    this.prototype.getSuperClass = function() { 
     return superClass; 
    }; 
    this.getSuperClass = this.prototype.getSuperClass; 
    return this; 
}; 

这将正确地分配所有的父将方法和属性分类到子类'class'。

使用看起来像

var Sub = function() {} 
Sub.Extend(Base) 
+0

我从来没有想过要这样做继承。多么美丽,优雅的解决方案! – 2011-05-18 04:11:46

+0

谢谢:-)让我找到源(我最肯定没有写......) – 2011-05-18 04:14:39

+0

哇!这值得一些关注。好的解决方案 – deepelement 2015-01-12 14:46:54

2

除了Alex Mcp的回答,你可以使用扩展它之后添加新的getter/setter方法来分:

Function.prototype.addGetter = function(val,fn){ 
    this.prototype.__defineGetter__(val,fn); 
    return this;  
} 
Function.prototype.addSetter = function(val,fn){ 
    this.prototype.__defineSetter__(val,fn); 
    return this;  
} 
//example; 
Sub.Extend(Base); 
Sub.addGetter('date',function(){return +new Date;}); 

,并增加tylermwashburns答案:你可以扩展函数原型为:

Function.prototype.copyGetterFrom = function(val,fromConstructor){ 
    this.prototype.__defineGetter__(
     val 
     ,fromConstructor.prototype.__lookupGetter__(val)); 
    return this; 
} 
//usage example.: 
Sub.copyGetterFrom('value',Base); 
3

a更现代的解决方案是使用Object.defineProperty,因为它允许getter和setter在不破坏的情况下进行处理。

唯一的问题是,它需要一个描述符对象,而不是手工制作一个利用Object.getOwnPropertyDescriptor功能只是给你拿的。

var BazValue = Object.getOwnPropertyDescriptor(Base.prototype,'value'); 

Object.defineProperty(Sub.prototype,'value',BazValue); 
+1

帮我填充工具'MouseEvent.x':'Object.defineProperty(MouseEvent.prototype, “X”,对象。getOwnPropertyDescriptor(MouseEvent.prototype,“clientX”))'。 – 2017-03-19 16:29:21

相关问题