2012-11-12 212 views
0

我目前正在将一个相当大的动作库转换为在我的nodejs项目中工作的过程。在这样做的时候,我偶然发现了一些可能成为问题的东西:从类构建类。有没有办法将一个对象作为另一个对象的基础(IE:继承基础对象的所有成员,然后从扩展对象中覆盖相同的名称成员)?构建其他对象构造函数的对象构造函数

现在这是我在做什么,但它变得有点困难,现在有3+类建立一个在顶部的另一个管理:

// The base object which others may extend 
function A() { 
    this.a = "pie"; 
} 
A.prototype.yum = function() { 
    return this.a + " is AWESOME!"; 
} 

// The "extends A" object. 
// Instead of creating an instance of "B", I current just create an instance of "A", 
// then adding the members from "B" to it at which point I return the "A" instance. 
function B() { 
    var a = new A(); 
    a.b = "pie"; 
    // Notice how I have to declare the overwriting function here instead of being able 
    // to drop it into B's prototype. The reason this bothers me is instead of just 
    // having one copy of the function(s) stored, each time a "new B" is created the 
    // function is duplicated... for 100s of "B" objects created, that seems like poor 
    // memory management 
    a.yum = function() { 
     return "I like " + this.a + " and " + this.b; 
    }; 
    return a; 
} 
console.log((B()).yum()); 




是否有可能在以下方面做些什么?
我知道这是无效的,但它提供了这个想法。

function A(){ 
    this.a = "pie" 
} 
A.prototype.yum = function() { 
    return this.a + " is AWESOME!"; 
} 
function B(){ 
    // Throws an "illegal left hand assignment" Exception due to overwriting `this`; 
    this = new A(); 
    this.b = "cake" 
} 
B.prototype.yum = function() { 
    return "I like "+this.a+" and "+this.b; 
} 
console.log((new B()).yum()); 

注:
1:我知道的JavaScript没有类;它使用对象和原型。否则我不会问。
2:这不是实际的代码im(试图)转换;这是一个普遍的例子
3:请不要建议图书馆。我知道它们有时是有价值的,但我宁愿不必维护,依靠并包含整个项目的图书馆。



答案: 我知道这是不好的形式,以改变本地成员的原型,但我认为这是值得的,因为缺乏的可能功能,以及它的大小。

Object.prototype.extendsUpon = function (p) { 
    var h = Object.prototype.hasOwnProperty; 
    for(var k in p)if(h.call(p,k))this[k]=p[k]; 

    function c(c){this.constructor=c;} 
    c.prototype = p.prototype; 
    this.prototype = new c(this); 
    this.__base__ = p.prototype; 
} 


function object_Constructor_built_ontop_of_another_constructor() { 
    this.extendsUpon(base_Object_to_built_atop_off); 
    this.__base__.constructor.apply(this, arguments); 

    // From here proceed as usual 

    /* To access members from the base object that have been over written, 
    * use "this.__base__.MEMBER.apply(this, arguments)" */ 
} 

回答

1

非常有可能。你可以用多种方式做到这一点,在咖啡脚本中使用得越完整:

var ClassBase, ClassTop, 
     __hasProp = {}.hasOwnProperty, 
     __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; 

    ClassBase = (function() { 

     function ClassBase() {} 

     return ClassBase; 

    })(); 

    ClassTop = (function(_super) { 

     __extends(ClassTop, _super); 

     function ClassTop() { 
     return ClassTop.__super__.constructor.apply(this, arguments); 
     } 

     return ClassTop; 

    })(ClassBase); 

会出现一些样板代码。 ClassTop继承了ClassBase。除了__extend(function(_super...和一些构造函数样板外,这些类没有多少内容,但它非常简单。

继承主要是由__extends模板,管理一些神奇。在此之前

__extends = function (child, parent) { 
     for (var key in parent) { 
      if (__hasProp.call(parent, key)) child[key] = parent[key]; 
     } 
     function ctor() { 
      this.constructor = child; 
     } 
     ctor.prototype = parent.prototype; 
     child.prototype = new ctor(); 
     child.__super__ = parent.prototype; 
     return child; 
    }; 

再次,更可怕的:全__extends方法在这里美化。你基本上检查父母的属性并将它们应用于孩子。更多信息可以在这里找到:http://www.jimmycuadra.com/posts/coffeescript-classes-under-the-hood

+0

Yelp;你现在让我感到困惑。你能给我一个使用蛋糕和派的简化版吗? (lol) – SReject

+0

nvm,经过几分钟的修补之后,我明白了。 ty – SReject

+0

不用担心。如果您需要进一步解释,请告诉我。把所有这些“东西”扔在你身上可能有点令人生畏。 – Daniel