【问题标题】:Native Javascript Class implementation本机 Javascript 类实现
【发布时间】:2010-08-05 18:20:23
【问题描述】:

我目前正在参与一个 Web 应用程序项目,我们不会使用框架。

我正在寻找“最好的”javascript 继承实现。我使用原型样式类如下:

function animal(options) {
    self = this;

    //properties    
    this.name = options.name;

    //events

    this.onXYZ = options.onXYZ;

    //event handlers
    this.handlerOfEvent = function() {
        //do something using 'self' as the 'this' variable
    }
}

animal.prototype.aFunction = function() 
{
     //do something 
}

等等

我没有在没有框架的情况下使用继承(通常使用 Mootools),但我确实了解它在 Javascript 中的工作原理,并且已经看到了一些实现。

我想获得一些反馈,告诉我在哪里可以找到最好的实现,它不涉及任何原生类型,并允许我完全访问上升类的属性和函数。

任何指针将不胜感激。

非常感谢您的宝贵时间。

【问题讨论】:

标签: javascript class inheritance


【解决方案1】:

道格拉斯·克罗克福德(Douglas Crockford)描述了一种我最近偏爱的方法:

var rectangle = function(width, height)
{
    var h = height, w = width;
    var scale = function(s)
    {
        h = h * s;
        w = w * s;
    }

    return { scale: scale };
}

var square = function(width)
{
    var o = rectangle(width, width)
    // Add things to o, if needed
    return o;
}

这不是一个非常好的例子,因为实际上并没有扩展任何东西,但它应该可以让这个想法得到理解。为了实例化这些对象,只需使用这个:

var newRectangle = rectangle(3, 4);  // 3 by 4 rectangle
var newSquare = square(6);  // 6 by 6 square

【讨论】:

    【解决方案2】:

    我过去尝试过很多方法。我最喜欢 John Resig 的实现方式。这很简单。您可以从http://ejohn.org/blog/simple-javascript-inheritance/ 看到完整的示例和 javascript 文件(只有大约 25 行代码)

    只是为了完成答案,你可以在包含他的代码后实现一个这样的类..

    var Person = Class.extend({
        name : '',
        init : function (name) {
            this.name = name;
        },
        say : function () {
            alert("I'm " + this.name);
        }
    });
    
    var mick = new Person("Mick");
    mick.say();
    

    【讨论】:

      【解决方案3】:

      看看Simple Javascript Class Project(常量、属性、受保护、静态、实用程序等)、Simple JavaScript InheritanceInheritance Patterns in JavaScript

      简单的 Javascript 类 (sjsClass) 示例

      Class.extend('Foo', {
          __protected : {
              privV : 123,
              privF : function () {
                  return this.privV + this.priv3;
              }
          },
          'protected priv3' : 'Protected Value',
          setX : function (x) {
              this.privV = x;
          },
          test : function () { return this.privF(); }
      });
      var f = new Foo;
      f.setX(456);
      f.test(); // -> 'Protected Value456'
      f.privF(); // -> Error
      f.privV; // -> undefined
      f.priv3; // -> undefined
      

      【讨论】:

        【解决方案4】:

        您应该查看来自 Douglas Crockford 的 videos 关于“寄生继承”的信息。

        这是一个基本的例子

            var pkg={};//simulating a package
            pkg.ObjA=function (){
            var privateField;//every inner function will have a closure to this field, this is the way to simulate private fields
            var privatefunc=function(){
            //same for private functions
            };
            //use object augmentation to add different fields to the "that" reference
            that.publicMethod=function(){
            //do something
            };
            return that;
            }
        
            //create two different instance of the object A
            var instance1=pkg.ObjA();
            var instance2=pkg.ObjA();
        
        pkg.ObjB=function(){
        
        //the that of this object is based on the ObjA
        var that=pkg.ObjA();
        
        //specialize the that to simulate inheritance
        that.newMethod=function(){
        
        }
        return that;
        }
        
        var child=pkg.ObjB();
        

        【讨论】:

          猜你喜欢
          • 2016-03-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-16
          • 1970-01-01
          • 1970-01-01
          • 2016-04-01
          • 2018-04-05
          相关资源
          最近更新 更多