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 开头的 apply 调用
// 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!
I am just wondering if there is any drawbacks with the way I am tackling inheritance below ?
Is there any memory leaks to consider, any more memory use than other inheritance patterns ?
I prefer to code JavaScript with the "class" pattern below ( new ...() ) ... I find other inheritance patterns obtrusive, and just came up with this one...
Comments are appreciated!
// 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"
================================ EDIT =================================
I have revised my original way, and came up with this. Does this suffer from the same problem as the one before ( Two objects created when creating a B, ( A and B total) )
See apply call in beginning of 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你应该考虑一下 javascript 中的原型。请参阅这篇文章 - http://www.sitepoint.com/javascript-inheritance/。
I think you should consider about prototypes in javascript. See this article - http://www.sitepoint.com/javascript-inheritance/.
我建议使用以下模式(在您的示例中):
如果您愿意,您也可以重写
hello
,如B.prototype.hello
并且它不会反映父(对象 A)实例。这样,您实际上可以使用原型来保存函数定义的重复项,并实际上继承属性、函数等。I'd suggest using the following pattern (in your example):
You could also override
hello
, asB.prototype.hello
if you wished and it wouldn't reflect on the parent (object A) instances. This way you actually use prototypes to save duplicates of function definitions and ACTUALLY inherit properties, functions etc.就像 HungryMind 解释的那样,你所拥有的不是继承,它更像是委托。如果它是基类,则
instanceof
将无法用于测试。如果您更喜欢创建基于闭包的对象(对于私有变量),那么您将陷入不使用原型的继承方案中。请参阅我的文章,了解如何在 JS 中实现正确继承。 http://js-bits.blogspot.com/2010 /08/javascript-inheritance-done-right.html 并不是说您不能使用任何其他方案,但除非您真正了解继承在 JS 中的工作原理,否则您不应该这样做。
Like HungryMind explained what you have is not inheritance, its more like delegation.
instanceof
won't work for testing if it's a base class. If you prefer to create closure based objects (for private variables) instead, you're stuck with an in inheritance scheme that does not use the prototype.See my post for what makes for correct inheritance in JS. http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html Not that you can't use any other scheme, but you shouldn't until you really understand how inheritance is meant to work in JS.