构造其他Object构造函数的对象构造函数

时间:2012-11-12 07:32:09

标签: javascript node.js object

我目前正在将一个非常大的actionscript库转换为在我的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:请不要建议图书馆。我知道它们有时很有价值,但我不需要维护,依赖并包含整个项目库。


ANSWER : 我知道改变原生成员原型是不好的形式,但我认为这是值得的,因为缺乏可能的功能和它的大小。

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 个答案:

答案 0 :(得分:1)

非常可能。你可以用多种方式完成它,coffeescript中使用的越完整:

    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方法在这里得到了美化:

    __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;
    };

再一次,比以前更加可怕。您基本上检查父母拥有的属性并将其应用于孩子。可以在此处找到更多信息:http://www.jimmycuadra.com/posts/coffeescript-classes-under-the-hood