0

来自Dojo,我真的很怀念Dojo的很多declare()功能。我正在开发一个复杂的应用程序,并且我从 Node 中破解了生活地狱,lang.inherits()以使其更......好吧,更强大。

这是一个示例,向您展示它的实际作用:

var First = declare( null, {
  one: function(p){ 
    console.log("one in First");
    console.log(p); 
    return 1000; 
  },
  two: function(p){ 
    console.log("two in First");
    console.log(p);
    return 1001; 
  },
  constructor: function(a){ 
    this.a = a; 
    console.log("Constructor of First called");
  },
})

var Second = declare( First, {
  two: function( p ){
    console.log("two in Second"); 
    console.log( p );
    a = this.inherited(arguments);
    console.log("Inherited function returned: " + a );
  },
  constructor: function(a){ 
    console.log("Constructor of Second called, and this.a is...");
    console.log( this.a );
  },
})

console.log("Creating first...");
first = new First(10);
console.log("Creating second...");
second = new Second( 20 );

console.log( "first.a:")
console.log( first.a );
console.log( "second.a:")
console.log( second.a );

console.log( "first.one(1):")
first.one(1);
console.log( "first.two(2):")
first.two(2);

console.log( "second.one(3):")
second.one(3);
console.log( "second.two(4):")
second.two(4);

将显示:

Creating first...
Constructor of First called
Creating second...
Constructor of First called
Constructor of Second called, and this.a is...
20
first.a:
10
second.a:
20
first.one(1):
one in First
1
first.two(2):
two in First
2
second.one(3):
one in First
3
second.two(4):
two in Second
4
two in First
4
Inherited function returned: 1001

我知道这个函数lang.inherits()之所以简约是有原因的:nodejs 不想强加特定的方式来处理 Javascript 中的“类”、原型和对象。

但是,很多代码都充满了:

function SomeClass( options ){
  this.options = options;
}

SomeClass.prototype.functionOne = function(something){
  //...
}
SomeClass.prototype.functionTwo = function(something){
  //...
}

哪个可以(并且......嗯,应该?)写成:

SomeClass = declare( null, {
  constructor: function(options){
    this.options = options;
  },
  functionOne: function(something){
    // ...
  },
  functionTwo: function(something){
    // ...
  },
})

能够做到的好处是:

SomeOtherClass = declare( SomeClass, {
  constructor: function(){
    this.options['manipulate'] ++;
  },
  functionOne: function(something){
    this.inherited(arguments); // Call the superclass method
    // ...
  },
})

它将自动调用父级的构造函数等(为了实现this.inherited(),我实际上最终创建了函数的哈希映射,因为它们实际上是无名的);

这个和 Dojo 的主要区别是这个版本没有实现多重继承和混合。然而,虽然多重继承/混合在客户端环境中是有意义的,但我觉得它们在服务器端程序中将是一个主要的过度杀伤力。好的......这是代码。你能发现这段代码有什么问题吗?

我发明了一些已经存在的东西吗?

开始了...

var 
  dummy
;

var declare = exports.declare = function(superCtor, protoMixin) {

  // Kidnap the `constructor` element from protoMixin, as this
  // it mustn't get copied over into the prototype
  var constructor = protoMixin.constructor;
  delete protoMixin.constructor;

  // The function that will work as the effective constructor. This
  // will be returned
  var ctor = function(){

    // Call the superclass constructor automatically
    if( typeof( superCtor.prototype.constructor === 'function' ) ){
       superCtor.prototype.constructor.apply( this, arguments );
    }

    // Call its own constuctor (kidnapped a second ago)
    if( typeof( constructor ) === 'function' ){
      constructor.apply( this, arguments );
    }
  };

  // The superclass can be either an empty one, or the one passed
  // as a parameter
  superCtor = superCtor === null ? function(){} : superCtor;

  // Create the new class' prototype. It's a new object, which happen to
  // have its own prototype (__proto__) set as the superclass' and the
  // `constructor` attribute set as ctor (the one we are about to return)
  ctor.super_ = superCtor;
  ctor.prototype = Object.create(superCtor.prototype, {
    constructor: {
      value: ctor,
      enumerable: false,
      writable: true,
      configurable: true
    }
  });

  // Implement inherited() so that classes can run this.inherited(arguments)
  // This will only work for sub-classes created using declare() as they are
  // the ones with the _inheritMap in their prototype
  protoMixin.inherited = function(args){
    var name, fn;

    // Look for the name in the _inheritMap
    name = this._inheritMap[ args.callee ];
    if( name ){
      fn = superCtor.prototype[name];
      if( fn ){
        return fn.apply( this, args );
      } else {
        throw( new Error("Method " + name + "() not inherited!") );
      }
    }
  }

  // Copy every element in protoMixin into the prototype.
  ctor.prototype._inheritMap = {}
  for( var k in protoMixin ){
    ctor.prototype[ k ] = protoMixin[ k ];
    ctor.prototype._inheritMap[ protoMixin[ k ] ] = k;
  }

  return ctor;
};
exports = module.exports = declare;
4

2 回答 2

0

我会看一下npm install declarejs,这基本上是 Dojo 声明的一个被撕掉的版本。

你可以在这里找到更多信息

就我个人而言,我更喜欢 Backbone's 之类的东西.extend(),它很容易被撕掉。

于 2013-02-22T15:47:21.997 回答
0

好吧,我想的答案是“如果它有效,那就太好了!”。它有效……所以:太好了!

供将来参考,“声明”在 GitHub 上:

https://github.com/mercmobily/JsonRestStores/blob/master/declare.js

我更新了代码,以便 this.inherited(arguments) 在没有哈希图的情况下工作。

目前,它是以下内容的一部分:

https://github.com/mercmobily/JsonRestStores

尽管我不妨创建一个单独的存储库,因为它本身就是一个方便的功能!

默克。

于 2013-02-25T14:04:19.067 回答