javascript继承

TypeScript编译生成的继承:

var extends = this.extends || function (d, b) {
    function () { this.constructor = d; }
    
.prototype = b.prototype;
    d.prototype = new ();
};
var Greeter = (function () {
    function Greeter(message) {
        this.greeting = message;
    }
    Greeter.prototype.greet = function () {
        return "Hello, " + this.greeting;
    };
    return Greeter;
})();
var B = (function (_super) {
    
extends(B, _super);
    function B() {
        _super.apply(this, arguments);
  
    }
    return B;
})(Greeter);

see http://www.typescriptlang.org/Playground/

这里有个问题是,var b = new B()时,b的proto显示成了,调试时辨别该对象比较麻烦。

一个解决办法是定义function () { this.constructor = d; }时,将替换成函数名字,然后eval该表达式




从一段js代码中摘取出的:

function() {
  var e = function(e, t) {
    return function() {
      return e.apply(t, arguments)
    }
  },
  t = {}.hasOwnProperty,
  n = function(e, n) {
    function i() {
      this.constructor = e
    }
    for (var r in n) t.call(n, r) && (e[r] = n[r]);
    return i.prototype = n.prototype,
    e.prototype = new i,
    e.super = n.prototype,
    e
  };
    
  Bobcat.Text = function(e) {
    function t(e) {
      var n, r = this;
      n = {
        style: {
          create: function(e) {
            return new Bobcat.TextStyle(e.data)
          }
        }
      },
      t.
super.constructor.call(this, e, n),
      this.oldValue = ko.observable()
    }
    return n(t, e),
    t.prototype.edit = function() {
      t.
super.edit.call(this);
      if (this"default") return this.oldValue(this.value()),
      this.value(" ")
    },
    t.prototype.deselect = function() {
      t.
super__.deselect.call(this);
      if (this"default") return this.value() === " " ? this.value(this.oldValue()) : this"default"
    },
    t
  } (Bobcat.Component),
    
} (window);