enyo官方开发入门教程翻译一Key Concepts之Creating Kinds

Creating Kinds

enyo.kind

     enyo.kindenyo框架生成kind的方法。一个kind是一个拥有高级特性(如原型链)的原型构造函数。

     Kind生成器的扩展能力包括插件系统,构造函数允许执行自定义的子类操作。

     在本片文章中,我们将会看到当enyo.kind方法被调用时会发生的几件事。要学习调用过程中发生什么你需要一个实例化的object,参考Object Lieftcycle

Special Property Names

    通常,定义在 inProps对象的属性都是直接拷贝到生成原型中,但是一些属性名会进行特殊的处理。一些特殊属性中例子:

Name:在全局命名空间中定义的构造函数名称(中间对象将自动创建)。名称不是直接拷贝到原型中的,而是存储在kindName中。

1 // Create a function MyNamespace.MyKind with a prototype.
2 // MyNamespace.MyKind.prototype.kindName is set to "MyNamespace.MyKind".
3 // MyNamespace.MyKind.prototype.plainProperty is set to "foo".
4 enyo.kind({
5     name: "MyNamespace.MyKind"
6     plainProperty: "foo"
7 });
8 // Make an instance of the new kind.
9 var myk = new MyNamespace.MyKind();

 

Kind:一个从父类继承而来的kind的名称或引用。新的构造函数原型链接到kind指定的原型链中,在新的原型中基础属性(即由父类继承而来的属性)由kind构造函数赋值。

1  // Create a function MyKind with a prototype, derived from enyo.Object.
2     // MyKind.prototype.kindName is set to "MyKind".
3     // MyKind.prototype.base is set to enyo.Object.
4     enyo.kind({
5         name: "MyKind",
6         kind: enyo.Object
7     });

 

Constructor:创建新实例的方法,它在原型中通常是 _constructor

 1 // Create a function MyKind with a prototype, derived from enyo.Object.
 2 // _constructor_ is called when an instance is created. 
 3 enyo.kind({
 4     name: "MyKind",
 5     kind: enyo.Object,
 6     constructor: function() {
 7         this.instanceArray = [];
 8         // Call the constructor inherited from Object
 9         this.inherited(arguments);
10     }
11 });

 

Statics:静态对象的属性都是直接从构造函数拷贝而来,而不是从原型中得来。

 1 // Create a kind with a static method.
 2 enyo.kind({
 3     name: "MyKind",
 4     statics: {
 5         info: function() {
 6             return "MyKind is a kind with statics.";
 7         }
 8     }
 9 });
10 // Invoke the static info() method of MyKind.
11 console.log(MyKind.info());

 

框架中的一些kind定义了自身特有的属性,例如enyo.Object的公共属性。

this.inherited

This.inherited使你可以轻松的调用父kind被重写过的方法。

 1    enyo.kind({
 2         name: "MyKind",
 3         doWork: function() {
 4             this.work++;
 5         }
 6     });
 7 
 8     enyo.kind({
 9         name: "MyDerivedKind",
10         kind: "MyKind",
11         doWork: function() {
12             if (this.shouldDoWork) {
13                 this.inherited(arguments);
14             }
15         }
16     });

 

This.inherited的第一个参数必须是"arguments",它是一个包含执行函数信息的js变量。

关于this.inherited的详细信息请参考Object Lifecycle

posted @ 2013-01-09 20:02  外卖  阅读(967)  评论(0编辑  收藏  举报