2011-11-15 36 views
0

我只是想知道在处理下面的继承方式时是否有任何缺点? 是否有任何内存泄漏要考虑,比其他继承模式更多的内存使用? 我更喜欢使用下面的“class”模式编码JavaScript(new ...())...我发现其他的继承模式很突兀,只是想出了这个...Javascript继承模式。请反馈?

评论感谢!

// Class A 
function A() { 

    var that = this; 

    that.hello = function() { 

    return "HELLO"; 

    } 

} 

// Class B 
function B() { 
    var zuper = new A(); 
    var that = this; 

    that.variable = "VARIABLE"; 

    zuper.bye = function() { 

    return "BYE"; 
    } 

    zuper.getVariable = function() { 
    return that.variable 
    } 

    return zuper; 
} 

var b = new B(); 
alert (b.hello()) // "HELLO" 
alert (b.bye()) // "BYE" 
alert (b.getVariable()) // "VARIABLE" 

================================编辑========= ======================== 我修改了原来的方式,并提出了这个问题。这是否来自同一个问题,因为前一个挨(创建一个B,(A和B计)时创建的两个对象) 见B中

// Class A 
function A() { 

    var that = this; 
    that.publicProperty = "PUBLIC_PROPERTY"; 
    var privateProperty = "PRIVATE_PROPERTY"; 

    that.hello = function() { 
    return "HELLO"; 
    } 

    that.getPrivateProperty = function() { 
    return privateProperty; 
    } 

    that.overrideThis = function() { 
    return "NO_PLEASE_NO"; 
    } 

} 

// Class B 
function B(a, b, c) { 
    A.apply(this, arguments); 

    this.variable = "VARIABLE"; 
    var privateVariable = "PRIVATE_VARIABLE"; 

    this.bye = function() { 

    return "BYE"; 
    } 

    this.getVariable = function() { 
    return this.variable 
    } 

    this.getPrivateVariable = function() { 
    return privateVariable; 
    } 

    this.getAandB = function() { 
    return a + b; 
    } 

    this.getFromSuperPublicPropery = function() { 
    return this.publicProperty; 
    } 

    this.overrideThis = function() { 
    return "MUHAHAHA"; 
    } 

} 

var b = new B("aaa", "bbb"); 

alert (b.hello())  // "HELLO" 
alert (b.bye())   // "BYE" 
alert (b.getVariable()) // "VARIABLE" 
alert (b.getPrivateVariable()) // "VARIABLE"' 
alert (b.getAandB())   // "aaabbb" 
alert (b.getFromSuperPublicPropery()) // "PUBLIC_PROPERTY" 
alert (b.getPrivateProperty()) // "PRIVATE_PROPERTY" 
alert (b.overrideThis()) // MUAHAHAA 


function C() { 
    A.apply(this, arguments); 
} 

var c = new C(); 
alert (c.overrideThis()) // "NO_PLEASE_NO" 
alert (c.bye()) // Expecting an exception here! Correct!  
+3

这不是继承。看看如何http://stackoverflow.com/questions/7486825/javascript-inheritance – hungryMind

回答

1

我想你应该考虑有关的原型开始套用来电JavaScript的。看到这篇文章 - http://www.sitepoint.com/javascript-inheritance/

+1

那篇文章不是很好。它将属性从父级的原型复制到子级的原型。这意味着'instanceof'将不起作用。另外,如果您在设置继承后修改父级的原型,那么子级不会选择它。不是总是这样做,但它是正确的方式(设置继承链)我发布了什么使得在JS中良好的继承。 http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html –

0

我建议使用下面的模式(在你的例子):

// A function to implement basic inheritance 
function inherit(child, parent) { 
    function F() {}; 
    F.prototype = parent.prototype; 
    child.prototype = new F(); 
    // Reassign the original constructor, explained below 
    child.prototype.constructor = child; 
    // Maybe have a reference to parent prototype 
    // child.superClass = parent.prototype; 
} 

// Class A 
function A() { 
} 

A.prototype.hello = function() { 
    return "HELLO"; 
} 

// Class B 
function B() { 
    this.variable = "VARIABLE";  
} 

inherit(B, A); 

B.prototype.bye = function() { 
    return "BYE"; 
} 

B.prototype.getVariable = function() { 
    return this.variable; 
}; 


var b = new B(); 
alert (b.hello()) // "HELLO" 
alert (b.bye()) // "BYE" 
alert (b.getVariable()) // "VARIABLE" 

//If you reassigned the original constructor to child, you can do the following 
alert (b instanceof B); //true 
alert (b instanceof A); //true 

您也可以覆盖hello,因为B.prototype.hello如果你希望它不会对父(对象A)反映情况。这样,你实际上使用原型来保存函数定义的副本,并且实际上继承了属性,函数等。

+0

是的,我以前见过这种模式。不是一个粉丝。我喜欢将所有功能都包含在包装函数中的想法。在公共方法中使用的私有变量将不可访问。对?把this.variable变成var variable =“VARIABLE”;你将无法返回变量。 – momomo

+0

@Hamidam是的,这种模式唯一的缺点是它不支持私有变量。然后再一次,这是在JS中进行继承的最自然的(逻辑和内存明智的)方法。你真的需要私有变量吗?它们不能作为安全措施来防止未经授权的访问(毕竟它是JS)。为了开发舒适,大多数IDE无论如何都看不到对象属性。对于逻辑分隔,你可以用前面的下划线来命名所有“私有”变量等。 – zatatatata

+0

是的,我确实需要私有变量,因为它更清晰,不同的编码器可以理解该方法或变量的目的。如果我声明一个私有方法,那么可以理解为只能在这个类中使用。如果你正在编写一个API,这将成为一个问题,除非你想花很多时间写出明确的指导方针。我写了一个替代我最初的问题,不需要你初始化一个当前是这种情况的A,那就是我为一个创建两个对象。 – momomo

0

像HungryMind一样解释你拥有的不是继承,它更像是委派。 instanceof不适用于测试它是否为基类。如果您更喜欢创建基于闭包的对象(对于私有变量),那么您就会陷入一种不使用原型的继承方案。

看到我的帖子是什么使正确在JS继承。 http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html不是你不能使用任何其他的方案,但你不应该直到你真正了解继承是如何在JS中工作的。