首页 > Web开发 > 详细

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

时间:2014-11-13 18:22:07      阅读:291      评论:0      收藏:0      [点我收藏+]

无需一次性将类定义完全,你可以使用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类的一个实例

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

原文:http://www.cnblogs.com/toward-the-sun/p/4095462.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!