Javascript继承模式。请反馈?

Javascript继承模式。请反馈?,javascript,prototypal-inheritance,Javascript,Prototypal Inheritance,我只是想知道我下面处理继承的方式是否有缺点? 是否有内存泄漏需要考虑,比其他继承模式使用更多内存吗? 我更喜欢用下面的“类”模式编写JavaScript(新…())。。。我发现其他的遗传模式很突出,我刚刚想出了这个 欢迎评论 // Class A function A() { var that = this; that.hello = function() { return "HELLO"; } } // Class B function B() { var

我只是想知道我下面处理继承的方式是否有缺点? 是否有内存泄漏需要考虑,比其他继承模式使用更多内存吗? 我更喜欢用下面的“类”模式编写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!    

我认为你应该考虑JavaScript中的原型。参见本文-。

我建议使用以下模式(在您的示例中):


如果您愿意,也可以覆盖
hello
,如
B.prototype.hello
,但它不会反映在父(对象A)实例上。通过这种方式,您实际上使用原型来保存函数定义的副本,并实际继承属性、函数等。

就像HungryMind解释的那样,您拥有的不是继承,而是委托<如果代码>instanceof是基类,则它不适用于测试。如果您更喜欢创建基于闭包的对象(用于私有变量),那么您将不得不使用不使用原型的内继承方案


请参阅我的文章,了解如何在JS中实现正确的继承。并不是说您不能使用任何其他方案,而是在您真正理解继承在JS中的作用之前,您不应该使用任何其他方案。

这不是继承。看,是的,我以前见过这种模式。不太喜欢。我喜欢在包装函数中包含所有功能的想法。在公共方法中使用的私有变量将不可访问。对吗?将this.variable转换为var variable=“variable”;您将无法返回变量。@Hamidam是的,该模式唯一的缺点是不支持私有变量。再说一次,这是在JS中进行继承的最自然(逻辑上和内存上)的方式。你真的需要私有变量吗。它们不能作为安全机制来防止未经授权的访问(毕竟是JS)。为了便于开发,大多数IDE无论如何都看不到对象属性。对于逻辑分离,您可以使用前导下划线等命名所有“私有”变量。是的,我确实需要私有变量,因为它更简洁,不同的编码人员可以理解该方法或变量的用途。如果我声明了一个私有方法,那么它将被理解为只在这个类中使用。如果您正在编写API,这将成为一个问题,除非您想花大量时间编写清晰的指导原则。我已经写了一个替代我最初的问题,不需要你初始化一个A,这是目前的情况,那就是我正在为一个创建两个对象。它没有那么干净,真的,如果区分清楚的话(一句话就足够了),在API中使用起来就像私有一样方便。此外,在修改原始库时,它还可以让您更好地了解哪些变量是私有的。在实现私有变量的语言中,使用它们是有意义的。在JS中,它们不是最好的想法,所以牺牲代码优化和可读性来换取不值得付出的努力。为什么你说JS中的私有变量毫无意义?有没有办法用我不知道的方式操纵它们?还有,为什么我需要所有上述逻辑(例如,inherit方法那篇文章不是很好。它将属性从父对象的原型复制到子对象的原型。这意味着
instanceof
将不起作用。另外,如果在设置继承后修改父对象的原型,子类将不会拾取它。这不是一直都在做的事情,但它不是。)我发布了一个正确的方法(设置继承链)来说明如何在JS中实现良好的继承。
// 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