Javascript继承模式。反馈好吗?

时间:2011-11-15 11:00:17

标签: javascript prototypal-inheritance

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

赞赏评论!

// 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 个答案:

答案 0 :(得分:1)

我认为你应该考虑javascript中的原型。请参阅此文章 - http://www.sitepoint.com/javascript-inheritance/

答案 1 :(得分: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)实例上。这样,您实际上使用原型来保存函数定义的副本,并且实际上继承了属性,函数等。

答案 2 :(得分:0)

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

请参阅我的帖子,了解JS中正确继承的原因。 http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html并不是说你不能使用任何其他方案,但是在你真正理解继承在JS中是如何工作之前你不应该这样做。