• 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类的一个实例
  • 相关阅读:
    个人总结
    第二次冲刺 10
    第二次冲刺 09
    第二次冲刺 08
    第二次冲刺 07
    团队冲刺第七天
    团队绩效评估
    团队冲刺第六天
    团队冲刺第五天
    团队冲刺第四天
  • 原文地址:https://www.cnblogs.com/toward-the-sun/p/4095462.html
Copyright © 2020-2023  润新知