16
2015
04

(十)createjs-继承

js本身是没有继承的(实现继承,其他语言只要一个关键字就够了),但是牛人很多,大神们想到了很多方法来实现“继承”,要深入学习js,学习一下继承还是很有必要的,好在这方面资料很多,随便百度一下就有了。

这里,来学习一下如何实现继承createjs中的类。

先看代码:

  1.      function MyLayer(){

  2.            this.Container_constructor();

  3.       }

  4.       var p=createjs.extend(MyLayer, createjs.Container);

  5.       p.a=function(){

  6.           console.log("调用了函数a");

  7.       }

  8.       createjs.promote(MyLayer,"Container");

  9.        

  10.     window.onload=function(){

  11.         //创建stage,参数为canvas的id

  12.         var stage=new createjs.Stage("gameMain");

  13.         var layer=new Hanyeah.MyLayer(); 

  14.         layer.a();

  15.         console.log(layer); 

  16.         console.log(new createjs.Container());  

  17.         //stage.addChild(layer);

  18.         stage.update();

  19.     }

首先我们定义了一个类(其实就是一个function),注意this.Container_constructor()这句,一会儿再说这句的作用。

function MyLayer(){

       this.Container_constructor();

}

然后调用createjs提供的方法createjs.extend(MyLayer, createjs.Container);从名字来看,这句是实现继承的,第一个参数是子类,第二个参数是父类,这个函数返回了子类的prototype(不知道prototype的话,自己去百度),所以我们定义了一个变量p来接收,p.a就相当于MyLayer.prototype.a了。

一开始以为createjs.extend之后就实现继承了,其实还不够,还要有后面这句createjs.promote(MyLayer,"Container");第一个参数是子类,第二个参数一般是父类的名字(官方文档写的挺清楚,只是英文的,没看懂)。

这样我们就实现了继承createjs中的类,MyLayer继承自createjs.Container,并且为MyLayer添加了一个新方法a。


完整的继承应该是这样。我们知道(function a())()这句的作用是是立即执行, "use strict"是严格模式的意思。为什么要这么写?百度一下“use strict“就知道了,(function a())()就是为了控制"use strict"的作用域。createjs中都是用的严格模式,我们自己写代码的时候,可以不用,但是还是建议使用,据说严格模式是js未来的趋势。

  1.  var Hanyeah={};

  2. (function(){

  3.             "use strict";

  4.             function MyLayer(){

  5.                 this.Container_constructor();

  6.             }

  7.             var p=createjs.extend(MyLayer, createjs.Container);

  8.             p.a=function(){

  9.                 console.log("调用了函数a");

  10.             }

  11.             Hanyeah.MyLayer=createjs.promote(MyLayer,"Container");

  12.         })()

关于this.Container_constructor();应该是调用父类的构造函数,相当于as中的super();这个方法需要调用createjs.promote之后才会有,函数名Container_constructor前边的Container就是createjs.promote函数的第二个参数。

具体自己看一下源码和注释吧,作者英文太渣,只能看到会用的程度。


createjs.promote方法的定义及注释。

  1. /**

  2.  * Promotes any methods on the super class that were overridden, by creating an alias in the format `prefix_methodName`.

  3.  * It is recommended to use the super class's name as the prefix.

  4.  * An alias to the super class's constructor is always added in the format `prefix_constructor`.

  5.  * This allows the subclass to call super class methods without using `function.call`, providing better performance.

  6.  *

  7.  * For example, if `MySubClass` extends `MySuperClass`, and both define a `draw` method, then calling `promote(MySubClass, "MySuperClass")`

  8.  * would add a `MySuperClass_constructor` method to MySubClass and promote the `draw` method on `MySuperClass` to the

  9.  * prototype of `MySubClass` as `MySuperClass_draw`.

  10.  *

  11.  * This should be called after the class's prototype is fully defined.

  12.  *

  13.  * function ClassA(name) {

  14.  * this.name = name;

  15.  * }

  16.  * ClassA.prototype.greet = function() {

  17.  * return "Hello "+this.name;

  18.  * }

  19.  *

  20.  * function ClassB(name, punctuation) {

  21.  * this.ClassA_constructor(name);

  22.  * this.punctuation = punctuation;

  23.  * }

  24.  * createjs.extend(ClassB, ClassA);

  25.  * ClassB.prototype.greet = function() {

  26.  * return this.ClassA_greet()+this.punctuation;

  27.  * }

  28.  * createjs.promote(ClassB, "ClassA");

  29.  *

  30.  * var foo = new ClassB("World", "!?!");

  31.  * console.log(foo.greet()); // Hello World!?!

  32.  *

  33.  * @method promote

  34.  * @param {Function} subclass The class to promote super class methods on.

  35.  * @param {String} prefix The prefix to add to the promoted method names. Usually the name of the superclass.

  36.  * @return {Function} Returns the subclass.

  37.  */

  38. createjs.promote = function(subclass, prefix) {

  39. "use strict";


  40. var subP = subclass.prototype, supP = (Object.getPrototypeOf&&Object.getPrototypeOf(subP))||subP.__proto__;

  41. if (supP) {

  42. subP[(prefix+="_") + "constructor"] = supP.constructor; // constructor is not always innumerable

  43. for (var n in supP) {

  44. if (subP.hasOwnProperty(n) && (typeof supP[n] == "function")) { subP[prefix + n] = supP[n]; }

  45. }

  46. }

  47. return subclass;

  48. };

createjs.extend的定义及注释。

  1. /**

  2.  * Sets up the prototype chain and constructor property for a new class.

  3.  *

  4.  * This should be called right after creating the class constructor.

  5.  *

  6.  * function MySubClass() {}

  7.  * createjs.extend(MySubClass, MySuperClass);

  8.  * ClassB.prototype.doSomething = function() { }

  9.  *

  10.  * var foo = new MySubClass();

  11.  * console.log(foo instanceof MySuperClass); // true

  12.  * console.log(foo.prototype.constructor === MySubClass); // true

  13.  *

  14.  * @method extends

  15.  * @param {Function} subclass The subclass.

  16.  * @param {Function} superclass The superclass to extend.

  17.  * @return {Function} Returns the subclass's new prototype.

  18.  */

  19. createjs.extend = function(subclass, superclass) {

  20. "use strict";


  21. function o() { this.constructor = subclass; }

  22. o.prototype = superclass.prototype;

  23. return (subclass.prototype = new o());

  24. };


点击查看实例

« 上一篇下一篇 »

相关文章:

使用Typescript开发基于createjs的项目  (2016-8-25 9:54:40)

createjs中文API  (2016-8-18 8:45:24)

pixi和createjs效率对比  (2016-8-2 9:45:21)

createjs中stage.mouseX的bug  (2016-7-12 9:14:44)

createjs启用touch事件  (2016-7-12 9:7:5)

createjs指定文本最大高度  (2016-6-14 10:0:38)

如何使用Createjs来编写HTML5游戏(转)  (2016-2-3 13:57:38)

createjs中Text中文不换行的问题(2)  (2015-11-25 10:58:11)

(十二)createjs游戏-围住神经猫  (2015-11-18 14:7:20)

(十一)createjs游戏-游戏框架  (2015-11-18 11:16:31)

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。