我想将constructor.name添加到自定义的javascript类项目中

时间:2014-05-26 20:48:04

标签: javascript oop inheritance jquery-plugins

我为我的javascript代码使用this小类函数;实际上它以某种方式改变了它实现'其他类的方式,因此它' inherets '只有函数而不是属性(参见:https://github.com/centurianii/g3OO

问题在于:

1)没有给出<object>.constructor.name,在http://jsfiddle.net/A8SkN/3/看到No.3和4(最终我想要的是以某种方式获得存储在变量中的新类的名称 - 我知道与适当的OO语言相比,这是完全疯狂的)和

2)没有传递名称为name的任何特定静态属性(但只要它具有不同的名称,它就会看到其他所有内容!),请参阅上面jsfiddle的No.6。

以下是Class代码:

(function(window, document, undefined){
   // Namespace object
   var g3;
   // Return as AMD module or attach to head object
   if (typeof define !== 'undefined')
      define([], function () {
         g3 = g3 || {};
         return g3;
      });
   else if (typeof window !== 'undefined')
      g3 = window.g3 = window.g3 || {};
   else{
      g3 = g3 || {};
      module.exports = g3;
   }

   g3.Class = function () {
      var len = arguments.length;
      if(len === 0 || (len === 1 && arguments[0] === null))
         return function() {};
      var body = arguments[len - 1],
          SuperClass = len > 1 ? arguments[0] : null,
          implementClasses = len > 2,
          Class,
          SuperClassEmpty,
          i;

      //we expect last object to override 'constructor' otherwise the new is empty!
      if (body.constructor === Object) {
         Class = function() {};
      } else {
         Class = body.constructor;
         delete body.constructor;
      }

      //'Class.Super' is a reserved word for g3.Class!
      if (SuperClass) {
         SuperClassEmpty = function() {};
         SuperClassEmpty.prototype = SuperClass.prototype;
         Class.prototype = new SuperClassEmpty();
         Class.prototype.constructor = Class;
         //doesn't work!
         //Class.prototype.constructor.name = Class.prototype.toString();
         Class.Super = SuperClass;
         extend(Class, SuperClass, false); //works for static members!
      }

      if (implementClasses)
         for (i = 1; i < len - 1; i++)
            if(typeof arguments[i] === 'object')
               extend(Class.prototype, arguments[i], false, 'function');
            else
               extend(Class.prototype, arguments[i].prototype, false);

      extendClass(Class, body);

      return Class;
   };

   function extendClass(Class, extension, override) {
      //'STATIC' is a reserved word from last argument of g3.Class!
      if (extension.STATIC) {
         extend(Class, extension.STATIC, override); //overwrites previous parent's static members
         delete extension.STATIC;
      }
      extend(Class.prototype, extension, override);
   };

   var extend = g3.Class.extend = function (obj, extension, override, type) {
      var prop;
      if (override === false) {
         for (prop in extension)
            if (!(prop in obj))
               if(!type)
                  obj[prop] = extension[prop];
               else if(typeof extension[prop] === type)
                  obj[prop] = extension[prop];
      } else {
         for (prop in extension)
            if(!type)
               obj[prop] = extension[prop];
            else if(typeof extension[prop] === type)
               obj[prop] = extension[prop];
         if (extension.toString !== Object.prototype.toString)
            obj.toString = extension.toString;
      }
   };
}(window, document));

让我们测试Class

///////TESTS///////
function Person(name){
   this.name = name;
}

//Class Dreamer extends Person
var Dreamer = g3.Class(Person, {
   STATIC: {
       dreams: [],
       name: 'Dreamer'
   },
   constructor: function(name, dream) {
      //Explicit call to Dreamer.Super === Person
      Dreamer.Super.call(this, name);
      this.dream = dream;
   },
   add: function() {
      Dreamer.dreams.push(this.dream);
   },
    toString: function(){
        return 'Dreamer';
    }
});
var poet = new Dreamer('Hary', 'I\'ll write a poem today!');
poet.add();

/////PRINT/////
var tests =  '<ol><li>poet.toString() = ' +  poet.toString() + '</li>';
tests += '<li>poet.constructor = ' + poet.constructor + '</li>';
tests += '<li>poet.constructor.name = ' + poet.constructor.name + '</li>';
tests += '<li>poet.constructor.name == \'\' : ' + (poet.constructor.name === '')+ '</li>';
tests += '<li>poet.prototype = ' + poet.prototype + '</li>';
if(poet.prototype)
tests += '<li>poet.prototype.constructor = ' + poet.prototype.constructor + '</li>';
tests += '<li>Dreamer.name = ' + Dreamer.name + '</li>';
tests += '<li>Dreamer.dreams = ' + Dreamer.dreams + '</li>';

document.getElementById('class').innerHTML = tests + '</ol>';

结果:

1. poet.toString() = Dreamer
2. poet.constructor = function (name, dream)...
3. poet.constructor.name =
4. poet.constructor.name == '' : true   //empty string!
5. poet.prototype = undefined
6. Dreamer.name =                       //empty string!
7. Dreamer.dreams = I'll write a poem today!

有什么想法吗?

感谢。

0 个答案:

没有答案