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

类与实例(Classes and Instances)

To define a new Ember class, call the extend() method on Ember.Object:

定义一个新的Ember,只需要调用Ember.Objectextend()方法即可:

  1. App.Person = Ember.Object.extend({
  2. say: function(thing) {
  3. alert(thing);
  4. }
  5. });

This defines a new App.Person class with a say() method.

这里定义了一个新的App.Person的类,并且为该类定义了一个say()方法。

You can also create a subclass from any existing class by calling its extend() method. For example, you might want to create a subclass of Ember’s built-in Ember.View class:

通过调用一个现有类的extend()方法,可以定义类的子类。例如可以通过定义一个Ember内建的Ember.View的子类来进行扩展:

  1. App.PersonView = Ember.View.extend({
  2. tagName: 'li',
  3. classNameBindings: ['isAdministrator']
  4. });

When defining a subclass, you can override methods but still access the implementation of your parent class by calling the special _super() method:

当定义一个子类的时候,可以重写父类已有的方法。尽管重写了父类的方法,依然可以在重写的方法中通过_super()来调用被重写的父类方法。例如:

  1. App.Person = Ember.Object.extend({
  2. say: function(thing) {
  3. var name = this.get('name');
  4. alert(name + " says: " + thing);
  5. }
  6. });
  7. App.Soldier = App.Person.extend({
  8. say: function(thing) {
  9. this._super(thing + ", sir!");
  10. }
  11. });
  12. var yehuda = App.Soldier.create({
  13. name: "Yehuda Katz"
  14. });
  15. yehuda.say("Yes"); // alerts "Yehuda Katz says: Yes, sir!"

创建实例(Creating Instances)

Once you have defined a class, you can create new instances of that class by calling its create() method. Any methods, properties and computed properties you defined on the class will be available to instances:

当定义了一个类之后,就可以通过调用create()方法来创建类的实例。所有定义在类中的方法、属性、计算属性,都可以通过创建的实例来访问或调用。例如:

  1. var person = App.Person.create();
  2. person.say("Hello"); // alerts " says: Hello"

When creating an instance, you can initialize the value of its properties by passing an optional hash to the create() method:

当创建一个类的实例时,可以将实例属性的初始值以hash形式的参数传给create()方法,从而实现对属性的初始化。例如:

  1. App.Person = Ember.Object.extend({
  2. helloWorld: function() {
  3. alert("Hi, my name is " + this.get('name'));
  4. }
  5. });
  6. var tom = App.Person.create({
  7. name: "Tom Dale"
  8. });
  9. tom.helloWorld(); // alerts "Hi, my name is Tom Dale"

For performance reasons, note that you cannot redefine an instance’s computed properties or methods when calling create(), nor can you define new ones. You should only set simple properties when calling create(). If you need to define or redefine methods or computed properties, create a new subclass and instantiate that.

考虑到性能问题,实例的计算属性或方法不能在调用create()方法的时候进行重定义。同样也不可以在此时定义新的计算属性或方法。因此,在调用create()方法时,只能设置基本属性。如果需要定义或者重新定义计算属性或方法,可以通过定义一个新的子类来实现。

By convention, properties or variables that hold classes are PascalCased, while instances are not. So, for example, the variable App.Person would contain a class, while person would contain an instance (usually of the App.Person class). You should stick to these naming conventions in your Ember applications.

按照惯例,用来保存类名的属性和变量名需首字母大写,而实例名首字母不大写。例如:变量App.Person代表一个类,而person则代表一个实例(通常是类App.Person的实例)。在Ember应用中,应该采用这样的命名惯例。

初始化实例(Initializing Instances)

When a new instance is created, its init method is invoked automatically. This is the ideal place to do setup required on new instances:

当一个实例被创建后,实例的init方法会被自动调用。可以通过自定义init方法,来对新实例进行初始化。

  1. App.Person = Ember.Object.extend({
  2. init: function() {
  3. var name = this.get('name');
  4. alert(name + ", reporting for duty!");
  5. }
  6. });
  7. App.Person.create({
  8. name: "Stefan Penner"
  9. });
  10. // alerts "Stefan Penner, reporting for duty!"

If you are subclassing a framework class, like Ember.View or Ember.ArrayController, and you override the init method, make sure you call this._super()! If you don’t, the system may not have an opportunity to do important setup work, and you’ll see strange behavior in your application.

假若继承了一个Ember内建的类,例如Ember.View或是Ember.ArrayController,并且重写了init方法,一定要在重写的init方法中调用this._super()!如果没有调用该方法,系统可能无法正常完成一些重要的初始化工作,从而导致应用出现一些怪异的行为。

When accessing the properties of an object, use the get and set accessor methods:

当访问一个对象的属性时,应该使用get方法,而设置属性值时则应该使用set方法。例如:

  1. var person = App.Person.create();
  2. var name = person.get('name');
  3. person.set('name', "Tobias Fünke");

Make sure to use these accessor methods; otherwise, computed properties won’t recalculate, observers won’t fire, and templates won’t update.

请记住使用这些属性访问方法,否则计算属性不会重新计算,观测也不会被触发,模板也无法得到正常的更新。