【问题标题】:javascript inheritance frameworkjavascript继承框架
【发布时间】:2012-03-13 13:32:19
【问题描述】:

是否有一个小型、轻量级的 javascript 类继承解决方案可以在客户端和服务器端 (node.js) 上运行良好?我不想要一个大的库,只想要一个允许我声明构造函数和一些方法,然后有能力让一个类继承它的东西。

【问题讨论】:

  • 也许你喜欢 CoffeeScript 的 class 和 extends 关键字?
  • 我非常讨厌 coffescript。我喜欢我的代码有标点符号,并且可读。 Coffescript 太像那些可怕的语言,比如 Visual Basic 和脚本语言,比如 ruby​​。

标签: javascript inheritance node.js


【解决方案1】:

John Resig 用大约 25 行代码 here 概述了一个简单的继承框架。我看到它曾经效果很好。

你可以这样使用它:

var Vehicle = Class.extend({
  init: function(wheels) {
    this.wheels = wheels;
  }
});

var Truck = Vehicle.extend({
  init: function(hp, wheels) {
    this.horsepower = hp;
    this._super(wheels);
  },

  printInfo: function() {
    console.log('I am a truck and I have ' + this.horsepower + ' hp.');
  }
});

var t = new Truck(4, 350);
t.printInfo();

【讨论】:

    【解决方案2】:

    【讨论】:

      【解决方案3】:

      我创建了这个小库来使用 ExtJs 样式类管理器。它非常简单,但非常灵活。

      通过 node.js 安装

      npm install esf-core
      

      示例

      Esf.define('A', {
          a: null,
      
          constructor: function (a) {
              // Save var
              this.a = a;
      
              // Heyho
              console.log('A');
          },
          foo: function (b) {
              console.log('foo - ' + b);
          }
      });
      
      Esf.define('B', {
          b: null,
      
          constructor: function (a, b) {
              // Call super constructor
              this.callParent(a);
      
              // Save var
              this.b = b;
      
              // Heyho
              console.log('B');
          },
          foo: function () {
              this.callParent('bar');
          }
      }, {
          extend: 'A'
      });
      
      // Use
      var b = new B(1, 2);
      
      // or
      var b = Esf.create('B', 1, 2);
      
      /*
       * Output:
       * A
       * B
       * foo - bar
       */
      b.foo();
      

      存储库

      https://bitbucket.org/tehrengruber/esf-js-core

      【讨论】:

        【解决方案4】:

        我看到prototype库使用成功了。

        【讨论】:

          【解决方案5】:

          我认为这比简单继承fw中的init hax要好得多:

          (function() {
              var core = {
                  require : function(source) {
                      if ( typeof (source) != "object" || !source)
                          throw new TypeError("Object needed as source.");
                      for (var property in source)
                          if (source.hasOwnProperty(property) && !this.prototype.hasOwnProperty(property))
                              this.prototype[property] = source[property];
                  },
                  override : function(source) {
                      if ( typeof (source) != "object" || !source)
                          throw new TypeError("Object needed as source.");
                      for (var property in source)
                          if (source.hasOwnProperty(property))
                              this.prototype[property] = source[property];
                  },
                  extend : function(source) {
                      var superClass = this;
                      var newClass = source.hasOwnProperty("constructor") ? source.constructor : function() {
                          superClass.apply(this, arguments);
                      };
                      newClass.superClass = superClass;
          
                      var superClone = function() {
                      };
                      superClone.prototype = superClass.prototype;
                      newClass.prototype = new superClone();
                      newClass.prototype.constructor = newClass;
          
                      if (source)
                          newClass.override(source);
                      return newClass;
                  }
              };
          
              core.require.call(Function, core);
          
              Function.create = function (source){
                  var newClass = source.hasOwnProperty("constructor") ? source.constructor : function() {};
                  newClass.override(source);
                  return newClass;
              };
          })(); 
          

          车辆示例:

          var Vehicle = Function.create({
              constructor : function(wheels) {
                  this.wheels = wheels;
              }
          });
          
          var Truck = Vehicle.extend({
              constructor : function(hp, wheels) {
                  this.horsepower = hp;
                  Vehicle.call(this, wheels);
              },
              printInfo : function() {
                  console.log('I am a truck and I have ' + this.horsepower + ' hp.');
              }
          });
          
          var t = new Truck(4, 350);
          t.printInfo();
          

          【讨论】:

            【解决方案6】:

            我创建了一个非常轻量级的库,可以在浏览器和 node.js 中运行。它是一个超级易用、无臃肿的库:

            例子:

            var Person = proto(function() {       // prototype builder
                this.init = function(legs, arms) {      // constructor
                    this.legs = legs
                    this.arms = arms
                }
            
                this.getCaughtInBearTrap = function() { // instance method
                    this.legs -= 1
                }
                this.limbs = function() {
                    return this.arms + this.legs
                }
            })
            
            var Girl = proto(Person, function() { // inheritance
                this.haveBaby = function() {
                    return Person(2,2)
                }
            })
            
            var g = Girl(2,2)        // instantiation
            g.getCaughtInBearTrap()
            console.log("Girl has "+g.limbs()+" limbs")
            console.log(": (")
            

            【讨论】:

              猜你喜欢
              • 2017-06-07
              • 2011-05-19
              • 2010-10-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-09-22
              相关资源
              最近更新 更多