欢迎转载opendevkit文章, 文章原始地址: http://www.opendevkit.com/?e=57

declare接口是dojo定义类系统的关键函数, 类系统就是抽象,封装,继承.dojo用javascript的prototype实现继承, 用mixin实现派生, javascript一切都是函数, 这本身就是高度抽象.巧妙的地方在于: 对于一个类, 有构造函数和类名字, 实际上构造函数是用户自定义的函数, 类名字也是用户自定的, 怎么实现用户使用new操作符的时候, 构造整个对象呢?

1. 对象从哪来

比如, Dialog.js里声明了一个dijit.Dialog, 这是一个var,还是一个函数?实际上, 在declare接口里:

817         // add name if specified
 818         if(className){
 819             proto.declaredClass = className;                                                                                               
 820             lang.setObject(className, ctor);
 821         }
红 字部分, 把dijit.Dialog声明成了一个对象, 并且赋值为ctor, 实际上ctor是一个函数, 马上就说会说到. 当new操作的时候, 实际上调用的是ctor这个函数, 这个函数并不是dijit.Dialog里constructor(继承_Widget.js).

2. 关联类名字的函数ctor

declare接口里:

768                 // chain in new constructor                                                                                                
 769                 ctor = new Function;
 770                 ctor.superclass = superclass;
 771                 ctor.prototype = proto;
 772                 superclass = proto.constructor = ctor;
ctor首先是一个匿名函数, 接下来的

799         bases[0] = ctor = (chains && chains.constructor === "manual") ? simpleConstructor(bases) :
 800             (bases.length == 1 ? singleConstructor(props.constructor, t) : chainedConstructor(bases, t));
实际上给ctor赋值了simpleConstructor或者chainedConstructor的返回值, 而比如

simpleConstructor的返回值是个函数:

441     // plain vanilla constructor (can use inherited() to call its base constructor)
 442     function simpleConstructor(bases){
 443         return function(){
 444             var a = arguments, i = 0, f, m;
 445
 446             if(!(this instanceof a.callee)){
 447                 // not called via new, so force it
 448                 return applyNew(a);
 449             }
 450
 451             //this._inherited = {};
 452             // perform the shaman's rituals of the original declare()
 453             // 1) do not call the preamble
 454             // 2) call the top constructor (it can use this.inherited())
 455             for(; f = bases[i]; ++i){ // intentional assignment
 456                 m = f._meta;
 457                 f = m ? m.ctor : f;
 458                 if(f){
 459                     f.apply(this, a);
 460                     break;
 461                 }
 462             }
 463             // 3) call the postscript
 464             f = this.postscript;
 465             if(f){
 466                 f.apply(this, a);
 467             }
 468         };
 469     }
也就是ctor =  441     // plain vanilla constructor (can use inherited() to call its base constructor)
 442     function simpleConstructor(bases){
 443         return function(){
 444             var a = arguments, i = 0, f, m;
 445
 446             if(!(this instanceof a.callee)){
 447                 // not called via new, so force it
 448                 return applyNew(a);
 449             }
 450
 451             //this._inherited = {};
 452             // perform the shaman's rituals of the original declare()
 453             // 1) do not call the preamble
 454             // 2) call the top constructor (it can use this.inherited())
 455             for(; f = bases[i]; ++i){ // intentional assignment
 456                 m = f._meta;
 457                 f = m ? m.ctor : f;
 458                 if(f){
 459                     f.apply(this, a);
 460                     break;
 461                 }
 462             }
 463             // 3) call the postscript
 464             f = this.postscript;
 465             if(f){
 466                 f.apply(this, a);
 467             }
 468         };
 469     }
也就是ctor是一个函数, 函数体里 :

455             for(; f = bases[i]; ++i){ // intentional assignment
 456                 m = f._meta;
 457                 f = m ? m.ctor : f;
 458                 if(f){
 459                     f.apply(this, a);
 460                     break;

也就是当new调用这个对应dijit.Dialog的ctor的时候, 会执行_meta.ctor, 实际就是声明里边的constructor, 之后又调用:

463             // 3) call the postscript
 464             f = this.postscript;
 465             if(f){
 466                 f.apply(this, a);
 467             }

3. 总结

declare接口, 声明一个类, 实际上就是定一个了一个xxxx.xxxx.xxx这样的名字的一个函数, 名字就是类名, 函数就是调用constructor和postscript两个回调的ctor.

当, new的时候, xxx.xxx.xxx被调用, 近而调用了constructor和postscript函数.


需要理解具体的构造过程的话, 需要关注 : simpleConstructor和chainedConstructor.

dojo分析之declare接口的更多相关文章

  1. Dojo的declare接口

    declare(classname,[],{}) declare的第一个参数是可选的,代表类的名称 declare的第二个参数代表类的继承关系,比如继承哪一个父类,可以看到:第二个参数是一个数组,所以 ...

  2. mybatis源码分析之04Mapper接口的动态代理

    在工作中,使用mybatis操作数据库,只需要提供一个接口类,定义一些方法,然后调用接口里面的方法就可以CRUD,感觉是牛了一逼! 该篇就是记录一下,mybatis是如何完成这波骚操作的,即分析我们测 ...

  3. MyBatis 源码分析——生成Statement接口实例

    JDBC的知识对于JAVA开发人员来讲在简单不过的知识了.PreparedStatement的作用更是胸有成竹.我们最常见用到有俩个方法:executeQuery方法和executeUpdate方法. ...

  4. 【spring源码分析】BeanDefinitionRegistryPostProcessor接口可自定义bean加入IOC

    自定义BeanDefinitionRegistryPostProcessor BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcesso ...

  5. 后端程序员之路 33、Index搜索引擎实现分析2-对外接口和大体流程

    # index_manager的单例是index server对外的唯一接口,part_indexer是index搜索的核心部分,index_manager持有了一组part_indexer. typ ...

  6. Dojo Data Store——统一数据访问接口

    原文地址:http://www.infoq.com/cn/articles/wq-dojo-data-store 无论在传统的桌面应用还是在主流的互联网应用中,数据始终占据着软件应用中的核心地位.当下 ...

  7. List 接口以及实现类和相关类源码分析

    List 接口以及实现类和相关类源码分析 List接口分析 接口描述 用户可以对列表进行随机的读取(get),插入(add),删除(remove),修改(set),也可批量增加(addAll),删除( ...

  8. Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6642463 在前面几篇文章中,我们详细介绍了A ...

  9. 【Spring源码分析系列】ApplicationContext 相关接口架构分析

    [原创文章,转载请注明出处][本文地址]http://www.cnblogs.com/zffenger/p/5813470.html 在使用Spring的时候,我们经常需要先得到一个Applicati ...

随机推荐

  1. java中switch、while、do...while、for

    一.Java条件语句之 switch 当需要对选项进行等值判断时,使用 switch 语句更加简洁明了.例如:根据考试的名次,给予前 4 名不同的奖品.第一名,奖励笔记本一台:第二名,奖励 IPAD  ...

  2. Mysql:Forcing close of thread xxx user: 'root' 的解决方法

    MySQL server在中午的时候忽然挂掉.重启mysql也尽是失败,只有重启电脑才能解决,然而重装了MySQL也是不行,晚上还是挂, 去看mysql的errorlog,只能看到类似如下的信息: F ...

  3. iOS实现类似于歌词进度效果

    先看效果 这里关键的地方在于镂空文字的实现,可以用UILabel的drawRect方法. .h文件 @interface HollowLabel : UILabel @end .m文件 @interf ...

  4. SQLMAP 中$与#的区别

    在sql配置中比如in(#rewr#) 与in ($rewr$) 在Ibatis中我们使用SqlMap进行Sql查询时需要引用参数,在参数引用中遇到的符号#和$之间的区分为,#可以进行与编译,进行类型 ...

  5. Oracle数据库导入导出命令总结 (详询请加qq:2085920154)

    分类: Linux Oracle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的 ...

  6. 利用带关联子查询Update语句更新数据

    Update是T-sql中再简单不过的语句了,update table set column=expression  [where condition],我们都会用到.但update的用法不仅于此,真 ...

  7. jdk1.8下载安装

    jdk8环境变量 jdk8图解安装 java8安装   1 2 3 4 5 6 7 分步阅读 JDK8 是JDK的最新版本,加入了很多新特性,如果我们要使用,需要下载安装: JDK8在windows ...

  8. android6.0锁屏界面接收新通知处理流程

    灭屏状态下,接收新信息,屏幕会半亮显示通知流程: 1,应用构造notification后,传给NotificationManager,而后进入NotificationManagerService处理. ...

  9. c++11 实现单例模式

    C++11出来后,里面新增加了好多好用的功能 下面的单例就是使用了C++11中的标准库中的mutex和unique_prt 进行内存管理的. 此单例模式不用担心内存的释放问题 #pragma once ...

  10. win10 EFI装ubuntu14.04双系统 及初始配置

    这次第二次装ubuntu系统了,第一次是在win7下安装的,到了win10,由于用了efi,跟win7的安装方法不太相同,相同点有: 1.仍然可以用u盘启动,我用的是UltroISO这个软件. 2.装 ...