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

Reopening Classes and Instances

重新打开类和实例

You don’t need to define a class all at once. You can reopen a class and define new properties using the reopen method.

Ember.js的类不需要一次性完成所有的定义。可以通过reopen方法重新打开一个类来定义新的属性。

  1. Person.reopen({
  2. isPerson: true
  3. });
  4. Person.create().get('isPerson') // true

When using reopen, you can also override existing methods and call this._super.

当使用reopen时,还可以重写已有的方法。在重写的方法中可以通过this._super来调用被重写的方法。

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

reopen is used to add instance methods and properties that are shared across all instances of a class. It does not add methods and properties to a particular instance of a class as in vanilla JavaScript (without using prototype).

reopen用来添加被所有类的实例共享的实例方法和属性。它不能像Vanilla Javascript那样,用来为某一特定的实例来添加方法和属性(不使用prototype)。

But when you need to create class method or add properties to the class itself you can use reopenClass.

但是如果需要为类添加方法和属性,则可以使用reopenClass方法。

  1. Person.reopenClass({
  2. createMan: function() {
  3. return Person.create({isMan: true})
  4. }
  5. });
  6. Person.createMan().get('isMan') // true