$ super和$ parent在这个JavaScript代码中意味着什么?

时间:2011-03-02 14:30:56

标签: javascript

我正在研究build-a-JavaScript-framework tutorial

以下代码为什么$super$parent以美元符号为前缀?

dp.Class = function() {
  return dp.oo.create.apply(this, arguments);
}

dp.oo = {
  create: function() {
    var methods = null,
        parent  = undefined,
        klass   = function() {
          this.$super = function(method, args) { return dp.oo.$super(this.$parent, this, method, args); };
          this.initialize.apply(this, arguments);
        };

    if (typeof arguments[0] === 'function') {
      parent = arguments[0];
      methods = arguments[1];
    } else {
      methods = arguments[0];
    }

    if (typeof parent !== 'undefined') {
      dp.oo.extend(klass.prototype, parent.prototype);
      klass.prototype.$parent = parent.prototype;
    }

    dp.oo.mixin(klass, methods);
    dp.oo.extend(klass.prototype, methods);
    klass.prototype.constructor = klass;

    if (!klass.prototype.initialize)
      klass.prototype.initialize = function(){};

    return klass;
  },

  mixin: function(klass, methods) {
    if (typeof methods.include !== 'undefined') {
      if (typeof methods.include === 'function') {
        dp.oo.extend(klass.prototype, methods.include.prototype);
      } else {
        for (var i = 0; i < methods.include.length; i++) {
          dp.oo.extend(klass.prototype, methods.include[i].prototype);
        }
      }
    }
  },

  extend: function(destination, source) {
    for (var property in source)
      destination[property] = source[property];
    return destination;
  },

  $super: function(parentClass, instance, method, args) {
    return parentClass[method].apply(instance, args);
  }
};

2 个答案:

答案 0 :(得分:3)

$在JavaScript标识符中没有特殊含义。它只表示您想要指示的内容。

答案 1 :(得分:2)

没理由。可能是因为编写该代码的人想要明确表示超级&amp; parent是新关键字。