简体   繁体   English

Javascript继承模式。 反馈好吗?

[英]Javascript Inheritance pattern. Feedback please?

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... 我更喜欢使用下面的“类”模式(新...())来编写JavaScript代码。我发现其他继承模式是令人讨厌的,于是提出了这个模式。

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 这是否遇到与之前相同的问题(创建B时创建了两个对象,(总共A和B))请参阅B开头的apply call

// 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 think you should consider about prototypes in javascript. 我认为您应该考虑javascript中的原型。 See this article - http://www.sitepoint.com/javascript-inheritance/ . 请参阅这篇文章-http: //www.sitepoint.com/javascript-inheritance/

Like HungryMind explained what you have is not inheritance, its more like delegation. 就像HungryMind解释的那样,您拥有的不是继承,而是继承。 instanceof won't work for testing if it's a base class. instanceof是基类,则无法进行测试。 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. 如果您更喜欢创建基于闭包的对象(用于私有变量),则将陷入一个不使用原型的in继承方案中。

See my post for what makes for correct inheritance in JS. 请参阅我的文章,了解如何在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. http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html并不是您不能使用任何其他方案,但是除非您真正了解继承的含义,否则您不应该这样做。在JS中工作。

I'd suggest using the following pattern (in your example): 我建议使用以下模式(在您的示例中):

// 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

You could also override hello , as B.prototype.hello if you wished and it wouldn't reflect on the parent (object A) instances. 如果愿意,还可以覆盖hello ,作为B.prototype.hello ,它不会反映在父(对象A)实例上。 This way you actually use prototypes to save duplicates of function definitions and ACTUALLY inherit properties, functions etc. 这样,您实际上就可以使用原型来保存函数定义的副本,并实际上继承属性,函数等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript原型模式-反馈 - Javascript Prototype Pattern - Feedback 排除特定模式后,在特定模式后匹配“,”。 (在JavaScript中落后) - Match a “,” after a specific pattern while excluding this pattern. (lookbehind in JavaScript) Javascript:显示模块模式。 访问私人会员 - Javascript: Revealing Module Pattern. Access private members 我已经开始使用这种JavaScript模式了。它有什么问题吗? - I've started using this JavaScript pattern. Is there anything wrong with it? " Eslint "没有找到与模式 "lint" 匹配的文件。请检查模式中的输入错误。 错误 - Eslint "No files matching the pattern "lint" were found. Please check for typing mistakes in the pattern." error JavaScript装饰模式。 错误:超出了最大调用堆栈大小 - JavaScript Decorator pattern. Error: Maximum call stack size exceeded Javascript模块模式。 如何访问模块内的全局变量 - Javascript Module pattern. How to access globals inside the module Javascript 单个 var 模式。 我超载了吗? - Javascript single var pattern. Am I overloading it? javascript中继承的模式 - a pattern for inheritance in javascript Javascript多重继承模式 - Javascript multiple inheritance pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM