Prototype的Class.create函数解析_prototype

复制代码 代码如下:

/**
* 一个设计精巧的定时执行器
* 首先由 Class.create() 创建一个 PeriodicalExecuter 类型,
* 然后用对象直接量的语法形式设置原型。
*
* 需要特别说明的是 rgisterCallback 方法,它调用上面定义的函数原型方法bind, 并传递自己为参数。
* 之所以这样做,是因为 setTimeout 默认总以 window 对象为当前对象,也就是说,如果 registerCallback 方法定义如下的话:
* registerCallback: function() {
* setTimeout(this.onTimerEvent, this.frequency * 1000);
* }
* 那么,this.onTimeoutEvent 方法执行失败,因为它无法访问 this.currentlyExecuting 属性。
* 而使用了bind以后,该方法才能正确的找到this,也就是PeriodicalExecuter的当前实例。
*/
var PeriodicalExecuter = 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 是一个方法,返回一个构造函数。
* 一般使用如下
* var X = Class.create(); 返回一个类型,类似于 java 的一个Class实例。
* 要使用 X 类型,需继续用 new X()来获取一个实例,如同 java 的 Class.newInstance()方法。
*
* 返回的构造函数会执行名为 initialize 的方法, initialize 是 Ruby 对象的构造器方法名字。
* 此时initialize方法还没有定义,其后的代码中创建新类型时会建立相应的同名方法。
*/
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}

Class.create实际上是返回一个函数,那么new的时候,做了些什么呢。参照MDN

When the code new foo(...) is executed, the following things happen:

A new object is created, inheriting from foo.prototype.
The constructor function foo is called with the specified arguments and this bound to the newly created object. new foo is equivalent to new foo(), i.e. if no argument list is specified, foo is called without arguments.
The object returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)
new的时候会执行该返回的函数,即执行this.initialize.apply(this, arguments); 此时的this就是新生成的对象,这也就是说了所有对象的初始化工作全部委托给initialize函数了。

-------

这里为什么要把自己的initialize方法绑定到自己身上??

时间: 2024-08-02 05:37:14

Prototype的Class.create函数解析_prototype的相关文章

CImageList类Create函数参数解析

前面提到了CImageList类的Create(...)函数,虽然MSDN上已经有所解释,但仍有网友问到参数的具体含义,下面就我的理解,对参数进行一次轻量级的剖析   函数原型(其他重载函数请参看msdn):   BOOL Create( int cx, int cy, UINT nFlags, int nInitial, int nGrow ); cx ,cy: 图片的实际像素宽与高,没有问题 nFlags:创建图像列表的类型,包括4/8/16/24/32/位色, nInitial : 创建I

c语言-C语言 PCRE正则表达式 函数解析

问题描述 C语言 PCRE正则表达式 函数解析 求指教:PCRE库函数中,pcre_exec()的返回值是什么意思? *ovector数组的元素又是代表什么意 解决方案 int pcre_exec(const pcre *code, const pcre_extra *extra, const char *subject, int length, int startoffset, int options, int *ovector, int ovecsize) pcre_exec()返回匹配串的

关于线程内创建socket create函数返回空的情况。。。。

问题描述 关于线程内创建socket create函数返回空的情况.... 解决方案 你只是new了一个CSocket,m_hSocket有值了,但是没有看到你代码里有create的调用,create创建的代码写在哪里?

为什么 eclipse 里面打开的函数 解析 跟官网上找到的解析不一样呢

问题描述 为什么 eclipse 里面打开的函数 解析 跟官网上找到的解析不一样呢 rt: 望各位大神 解释一下 这个问题 有点坑 我找到的 函数 跟同事找到的函数解析不一样 老板问的时候 悲剧了 可是我也是看的 源码呀

iOS 基础函数解析 - Foundation Functions Reference

iOS 基础函数解析 - Foundation Functions Reference 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. Foundation Functions Referenc

Matlab中bsxfun和unique函数解析

一.问题来源 来自于一份LSH代码,记录下来. 二.函数解析 2.1 bsxfun bsxfun是一个matlab自版本R2007a来就提供的一个函数,作用是"applies an element-by-element binary operation to arrays a and b, with singleton expansion enabled. 函数用在两个数组间元素逐个计算.比如当我们想对一个矩阵A的每一列或者每一行与同一个长度相等的向量a进行某些操作(比较大小,乘除等)时,我们只

Prototype 工具函数 学习_prototype

$H就是建立Hash对象的便捷方法,关于Hash对象具体参考[Prototype 学习--Hash对象 ] $R就是简历ObjectRange对象的便捷方法,关于ObjectRange对象具体参考[Prototype 学习--ObjectRange对象 ] Try.these: Accepts an arbitrary number of functions and returns the result of the first one that doesn't throw an error.

php中array_slice和array_splice函数解析_php技巧

本文主要介绍了php中array_slice和array_splice函数,感兴趣的可以围观一下, array_slice和array_splice函数是用在取出数组的一段切片,array_splice还有用新的切片替换原删除切片位置的功能.类似javascript中的Array.prototype.splice和Array.prototype.slice方法. array_slice array array_slice ( array $array , int $offset [, int $

discuz中用到的javascript函数解析[原创]第1/2页_javascript技巧

var lang = new Array(); var userAgent = navigator.userAgent.toLowerCase(); var is_opera = userAgent.indexOf('opera') != -1 && opera.version(); var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8