最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Prototype的Class.create函數(shù)解析

 更新時(shí)間:2011年09月22日 23:30:28   作者:  
Prototype中的類的創(chuàng)建,一般使用Class.create方法來(lái)創(chuàng)建,例如PeriodicalExecuter類型。使用的時(shí)候通過(guò)調(diào)用new PeriodicalExecuter(xxx)來(lái)生成對(duì)象。
復(fù)制代碼 代碼如下:

/**
* 一個(gè)設(shè)計(jì)精巧的定時(shí)執(zhí)行器
* 首先由 Class.create() 創(chuàng)建一個(gè) PeriodicalExecuter 類型,
* 然后用對(duì)象直接量的語(yǔ)法形式設(shè)置原型。
*
* 需要特別說(shuō)明的是 rgisterCallback 方法,它調(diào)用上面定義的函數(shù)原型方法bind, 并傳遞自己為參數(shù)。
* 之所以這樣做,是因?yàn)?setTimeout 默認(rèn)總以 window 對(duì)象為當(dāng)前對(duì)象,也就是說(shuō),如果 registerCallback 方法定義如下的話:
* registerCallback: function() {
* setTimeout(this.onTimerEvent, this.frequency * 1000);
* }
* 那么,this.onTimeoutEvent 方法執(zhí)行失敗,因?yàn)樗鼰o(wú)法訪問(wèn) this.currentlyExecuting 屬性。
* 而使用了bind以后,該方法才能正確的找到this,也就是PeriodicalExecuter的當(dāng)前實(shí)例。
*/
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()背后做了什么,具體來(lái)看看Class的實(shí)現(xiàn)。
復(fù)制代碼 代碼如下:


/**
* 創(chuàng)建一種類型,注意其屬性 create 是一個(gè)方法,返回一個(gè)構(gòu)造函數(shù)。
* 一般使用如下
* var X = Class.create(); 返回一個(gè)類型,類似于 java 的一個(gè)Class實(shí)例。
* 要使用 X 類型,需繼續(xù)用 new X()來(lái)獲取一個(gè)實(shí)例,如同 java 的 Class.newInstance()方法。
*
* 返回的構(gòu)造函數(shù)會(huì)執(zhí)行名為 initialize 的方法, initialize 是 Ruby 對(duì)象的構(gòu)造器方法名字。
* 此時(shí)initialize方法還沒(méi)有定義,其后的代碼中創(chuàng)建新類型時(shí)會(huì)建立相應(yīng)的同名方法。
*/
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}

Class.create實(shí)際上是返回一個(gè)函數(shù),那么new的時(shí)候,做了些什么呢。參照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的時(shí)候會(huì)執(zhí)行該返回的函數(shù),即執(zhí)行this.initialize.apply(this, arguments); 此時(shí)的this就是新生成的對(duì)象,這也就是說(shuō)了所有對(duì)象的初始化工作全部委托給initialize函數(shù)了。

-------

這里為什么要把自己的initialize方法綁定到自己身上??

相關(guān)文章

最新評(píng)論

白沙| 济源市| 忻州市| 巍山| 汉中市| 高雄县| 克山县| 马鞍山市| 沁水县| 徐水县| 通道| 凭祥市| 阜宁县| 大连市| 哈巴河县| 临西县| 江永县| 保山市| 张家川| 鹤峰县| 新民市| 崇礼县| 山阴县| 民权县| 西藏| 五台县| 西华县| 卓尼县| 三亚市| 赤水市| 镇安县| 昌黎县| 英山县| 乌拉特中旗| 沙洋县| 镇安县| 策勒县| 五峰| 贡嘎县| 巩留县| 安康市|