Prototype的Class.create函数解析
函数 解析 create Class prototype
2023-06-13 09:14:30 时间
/**
*一个设计精巧的定时执行器
*首先由Class.create()创建一个PeriodicalExecuter类型,
*然后用对象直接量的语法形式设置原型。
*
*需要特别说明的是rgisterCallback方法,它调用上面定义的函数原型方法bind,并传递自己为参数。
*之所以这样做,是因为setTimeout默认总以window对象为当前对象,也就是说,如果registerCallback方法定义如下的话:
*registerCallback:function(){
*setTimeout(this.onTimerEvent,this.frequency*1000);
*}
*那么,this.onTimeoutEvent方法执行失败,因为它无法访问this.currentlyExecuting属性。
*而使用了bind以后,该方法才能正确的找到this,也就是PeriodicalExecuter的当前实例。
*/
varPeriodicalExecuter=Class.create();
PeriodicalExecuter.prototype={
initialize:function(callback,frequency){
this.callback=callback;
this.frequency=frequency;
this.currentlyExecuting=false;
this.registerCallback();
},
registerCallback:function(){
setTimeout(this.onTimerEvent.bind(this),this.frequency*1000);
},
onTimerEvent:function(){
if(!this.currentlyExecuting){
try{
this.currentlyExecuting=true;
this.callback();
}finally{
this.currentlyExecuting=false;
}
}
this.registerCallback();
}
}
具体Class.create()背后做了什么,具体来看看Class的实现。
/**
*创建一种类型,注意其属性create是一个方法,返回一个构造函数。
*一般使用如下
*varX=Class.create();返回一个类型,类似于java的一个Class实例。
*要使用X类型,需继续用newX()来获取一个实例,如同java的Class.newInstance()方法。
*
*返回的构造函数会执行名为initialize的方法,initialize是Ruby对象的构造器方法名字。
*此时initialize方法还没有定义,其后的代码中创建新类型时会建立相应的同名方法。
*/
varClass={
create:function(){
returnfunction(){
this.initialize.apply(this,arguments);
}
}
}
Class.create实际上是返回一个函数,那么new的时候,做了些什么呢。参照MDN
Whenthecodenewfoo(...)isexecuted,thefollowingthingshappen:
Anewobjectiscreated,inheritingfromfoo.prototype.
Theconstructorfunctionfooiscalledwiththespecifiedargumentsandthisboundtothenewlycreatedobject.newfooisequivalenttonewfoo(),i.e.ifnoargumentlistisspecified,fooiscalledwithoutarguments.
Theobjectreturnedbytheconstructorfunctionbecomestheresultofthewholenewexpression.Iftheconstructorfunctiondoesn"texplicitlyreturnanobject,theobjectcreatedinstep1isusedinstead.(Normallyconstructorsdon"treturnavalue,buttheycanchoosetodosoiftheywanttooverridethenormalobjectcreationprocess.)
new的时候会执行该返回的函数,即执行this.initialize.apply(this,arguments);此时的this就是新生成的对象,这也就是说了所有对象的初始化工作全部委托给initialize函数了。
-------
这里为什么要把自己的initialize方法绑定到自己身上??
相关文章
- 一文彻底搞懂迭代器与生成器函数
- R中的sweep函数
- assert函数解析[通俗易懂]
- 【C++ 语言】面向对象 ( 函数重载 | 运算符重载 | 运算符重载两种定义方式 | 拷贝构造方法 | RVO 优化 | NRVO 优化 )
- Oracle开发之分析函数(Top/Bottom N、First/Last、NTile)
- Oracle 函数:轻松解析函数引用(oracle函数引用)
- 掌握Oracle循环函数精要(oracle循环函数)
- linux下atoi函数功能探究(linuxatoi)
- 函数解析Linux中atoi函数的功能(linuxatoi)
- Linux:理解atoi函数(linuxatoi)
- 函数解析Linux上atoi函数的奥秘(linuxatoi)
- 函数解析Linux 中atoi函数的作用(linuxatoi)
- 函数解析Linux atoi函数(linuxatoi)
- 函数解析字符串为整型:Linux atoi函数(linuxatoi)
- 函数解析Linux中atoi函数的作用(linuxatoi)
- 函数解析Linux atoi函数的秘密(linuxatoi)
- Linux系统下内存管理函数的使用(linux内存函数)
- 深入解析Oracle函数:从CHM手册中学习(oracle函数chm)
- Oracle中使用颜色函数的实践应用(oracle中颜色函数)
- MySQL与日期掌握日期函数让你轻松处理数据(mysql yu日期)
- 一个高ai的分页函数和一个url函数
- asp中去除内容HTML标签的三个function函数
- sql自定义百分比转换小数函数代码
- 解析如何屏蔽php中的phpinfo()函数
- PHParray_multisort()函数的深入解析
- 解析strtr函数的效率问题
- 深入解析C++中的虚函数与多态
- C++初始化函数列表详细解析
- javascript获取函数形参个数