cocos2dx 3.0 js继承:John Resiq的继承写法解析

最近公司要开新项目,要用js来写。就好好看了下js中的继承。

比较懒,直接上代码加注释吧。 - -

[javascript]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //创建一个function  
  2. cc.Class = function(){};  
  3. cc.Class.extend = function (prop) {  
  4.  //this为父类的对象  
  5.     var _super = this.prototype;  
  6.     // Instantiate a base class (but only create the instance,  
  7.     // don't run the init constructor)  
  8.     initializing = true;  
  9.  //创建父类对象,这个prototype会被赋值给子类的原型  
  10.     var prototype = new this();  
  11.     initializing = false;  
  12.  //测试是否支持函数转字符串,正常情况 fnTest= /\b_super\b/  
  13.     fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;  
  14.  //prop子类创建对象的字面量  
  15.     // Copy the properties over onto the new prototype  
  16.     for (var name in prop) {  
  17.         // Check if we're overwriting an existing function  
  18.   //prop[name]就是名字为name的属性,或方法  
  19.         prototype[name] = typeof prop[name] == "function" &&  
  20.             typeof _super[name] == "function" && fnTest.test(prop[name]) ?  
  21.    //如果这个方法子类有,父类也有,而且子类在这个方法里面用了_super,就执行下面的语句  
  22.    /* 
  23.    首先执行一个匿名函数:传递方法名,(即:name),和子类的方法 
  24.    最后返回一个方法给原型中名字为name的方法(或者是说:给原型中添加一个名为name的方法)(语句0) 
  25.    */  
  26.    /* 
  27.    这个方法主要是进行了一层封装,当调用子类名为name的方法时,将调用这个返回的方法    这个方法主要是先给子类的_super方法指向为父类中名为name的方法(语句2    ),然后再真正的调用子类的方法(也就是这个fn)(语句3)    因为子类中的_super方法已经指向父类中名为name的方法,所以在调用fn时,如果这个fn里面用到   了_super,那么指向的就是父类的同名方法。 
  28.    然后在调用完之后,把子类中的_super重置(语句1 和 语句4) 
  29.             */  
  30.    (function (name, fn) {  
  31.                 return function () {    //语句0  
  32.                     var tmp = this._super;   //语句1  
  33.                     // Add a new ._super() method that is the same method  
  34.                     // but on the super-class  
  35.                     this._super = _super[name]; //语句2  
  36.                     // The method only need to be bound temporarily, so we  
  37.                     // remove it when we're done executing  
  38.                     var ret = fn.apply(this, arguments);    //语句3  
  39.                     this._super = tmp;   //语句4  
  40.                     return ret;  
  41.                 };  
  42.             })(name, prop[name]) :  
  43.             prop[name];  
  44.     }  
  45.  //创建子类,这个Class会覆盖外面的那个Class - -!  
  46.     // The dummy class constructor  
  47.     function Class() {  
  48.         // All construction is actually done in the init method  
  49.         if (!initializing) {  
  50.             if (!this.ctor)  
  51.                 cc.log("No ctor function found, please set `classes_need_extend` section at `ini` file as `tools/tojs/cocos2dx.ini`");  
  52.             else  
  53.                 this.ctor.apply(this, arguments);  
  54.         }  
  55.     }  
  56.  //给子类的原型赋值  
  57.     // Populate our constructed prototype object  
  58.     Class.prototype = prototype;  
  59.  //把子类原型中的构造函数指向自己  
  60.     // Enforce the constructor to be what we expect  
  61.     Class.prototype.constructor = Class;  
  62.  //让子类也有extend方法  
  63.     // And make this class extendable  
  64.     Class.extend = arguments.callee;  
  65.     return Class;  
  66. };  
就是这样。。。

看的懂,看不懂的,都将就看看吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值