Ember学习(8):REOPENING CLASSES AND INSTANCES

英文原址:http://emberjs.com/guides/object-model/reopening-classes-and-instances/


你不须要一次就完毕类所有内容的定义,通过reopen方法,你能够又一次打开一个类定义而且加入新的属性。

1
2
3
4
5

Person.reopen({
 
isPerson: true
});

Person.create().get(
'isPerson') // true


当使用reopen时,你也能够重写已有的方法,通过调用this._super调用之前的版本号

1
2
3
4
5
6

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

reopen方法是用于给类加入新的实例方法和属性的,这些新加入的属性和方法会被全部该类的实例所共享,它不是只只给该类的某个特定的实例加入方法和属性(好比普通的Javascript下不使用prototype的情况)

 

假设你须要给类本身加入新的类属性或者类方法,你能够使用reopenClass

1
2
3
4
5
6
7

Person.reopenClass({
 
createMan: function() {
   
return Person.create({isMan: true})
  }
});

Person.createMan().get(
'isMan') // true


原文地址:https://www.cnblogs.com/mengfanrong/p/3763311.html