【问题标题】:Generating a class from an object (JavaScript)从对象生成类 (JavaScript)
【发布时间】:2009-08-07 15:50:14
【问题描述】:

我正在尝试从 JavaScript 中的对象生成一个类。例如:

var Test = {
    constructor: function() { document.writeln('test 1'); },
    method: function() { document.writeln('test 2'); }
};

var TestImpl = function() { };
TestImpl.prototype.constructor = Test.constructor;
TestImpl.prototype.method = Test.method;

var x = new TestImpl();
x.method();

但这不起作用:它只会写'test 2'(无论出于何种原因,构造函数都没有正确定义)。为什么?

【问题讨论】:

    标签: javascript class object


    【解决方案1】:

    我认为你做错了。

    请记住,JavaScript 实际上根本没有类。它有原型。所以你真正想做的是创建一个原型对象,它的工作方式类似于你在另一个对象上构建的函数集合。我无法想象这有什么有用的目的——你能详细说明你想要做什么吗?

    虽然我认为您可以通过以下方式使其工作:

    var TestImpl = function() {
        Test.constructor.apply(this);
    };
    TestImpl.prototype.method = Test.method;
    

    【讨论】:

      【解决方案2】:

      您的 TestImpl 函数构造函数。通常你会这样做:

      var Test1 = function () {
        document.writeln('in constructor');
      };
      
      Test1.prototype = {
        x: 3,
        method1: function() { document.writeln('x='+this.x); }
      }
      
      var y1 = new Test1();
      y1.method1();
      y1.x = 37;
      y1.method1();
      
      var y2 = new Test1();
      y2.method1();
      y2.x = 64;
      y2.method1();
      

      我认为你的情况有点倒退。通常你会将原型分配给构造函数,而不是将构造函数分配给原型。

      将方法分配给构造函数的原型而不是构造函数内部的“this”对象的原因是,前一种方法只创建一个共享函数,而后一种方法创建一个函数的单独实例。如果您创建大量对象,每个对象都有很多方法,这一点很重要(将内存分配保持在合理的数量)。

      【讨论】:

        【解决方案3】:
        var Test = function () {
          document.writeln('test 1');
          this.method = function() { document.writeln('test 2'); }
        };
        
        var x = new Test();
        x.method();
        

        【讨论】:

        • 错字:名称不符。测试/TestImpl
        【解决方案4】:

        Javascript 没有“类”的概念,它完全是关于原型和你使用它们的方式[并且你可以用这个小巧的特性模拟任何类型的继承。 ] 在 javascript 中,“Function”扮演着 [Class, Method and Constructor] 的角色。

        所以为了在 Javascript 中创建“类”行为,您需要做的就是使用“函数”的力量。

        var A = function(){
            alert('A.Constructor');
        }
        
        A.prototype = {
            method : function(){
                alert('A.Method');
            }
        }
        
        var b = new A(); // alert('A.Constructor');
        b.method(); // alert('A.Method');
        

        现在关于 JS 的巧妙之处在于,您可以使用相同的方法轻松创建“继承”行为。你需要做的就是将第二类“原型链”连接到第一类,如何?

        B = function(){
         this.prototype = new A(); // Connect "B"'s protoype to A's
        }
        B.prototype.newMethod = function() { alert('testing'); }
        
        var b = new B();
        
        b.method();    // Doesn't find it in B's prototype, 
                       // goes up the chain to A's prototype
        
        b.newMethod(); // Cool already in B's prototype
        
        // Now when you change A, B's class would automatically change too
        A.prototype.method = function(){ alert('bleh'); }
        b.method();    // alert('bleh')
        

        如果您需要更多参考资料,我建议您查看Douglas Crockford's Site 快乐的JSing。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-07-07
          • 2010-09-17
          • 1970-01-01
          • 2014-09-15
          • 2011-04-12
          • 2022-01-21
          • 1970-01-01
          相关资源
          最近更新 更多