emberjs重写补充类之reopen方法和reopenClass方法

无需一次性将类定义完全,你可以使用reopen方法来重新打开(reopen)一个类并为其定义新的属性。

Person.reopen({
  isPerson: true
});
Person.create().get('isPerson') // true

当使用reopen时,你也同样可以覆写已经存在的方法并调用this._super

Person.reopen({
  // override `say` to add an ! at the end
  say: function(thing) {
    this._super(thing + "!");
  }
});

正如你所见,reopen是用来为实例添加属性和方法的。而当你需要创建类的方法或为类本身添加属性时,则可使用reopenClass

Person.reopenClass({
  createMan: function() {
    return Person.create({isMan: true})
  }
});
Person.createMan().get('isMan') // true Person类的createMan方法用来创建Person类的一个实例
原文地址:https://www.cnblogs.com/toward-the-sun/p/4095462.html