Prototype的Class.create函數(shù)解析
更新時間:2011年09月22日 23:30:28 作者:
Prototype中的類的創(chuàng)建,一般使用Class.create方法來創(chuàng)建,例如PeriodicalExecuter類型。使用的時候通過調(diào)用new PeriodicalExecuter(xxx)來生成對象。
復(fù)制代碼 代碼如下:
/**
* 一個設(shè)計精巧的定時執(zhí)行器
* 首先由 Class.create() 創(chuàng)建一個 PeriodicalExecuter 類型,
* 然后用對象直接量的語法形式設(shè)置原型。
*
* 需要特別說明的是 rgisterCallback 方法,它調(diào)用上面定義的函數(shù)原型方法bind, 并傳遞自己為參數(shù)。
* 之所以這樣做,是因為 setTimeout 默認總以 window 對象為當前對象,也就是說,如果 registerCallback 方法定義如下的話:
* registerCallback: function() {
* setTimeout(this.onTimerEvent, this.frequency * 1000);
* }
* 那么,this.onTimeoutEvent 方法執(zhí)行失敗,因為它無法訪問 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的實現(xiàn)。
復(fù)制代碼 代碼如下:
/**
* 創(chuàng)建一種類型,注意其屬性 create 是一個方法,返回一個構(gòu)造函數(shù)。
* 一般使用如下
* var X = Class.create(); 返回一個類型,類似于 java 的一個Class實例。
* 要使用 X 類型,需繼續(xù)用 new X()來獲取一個實例,如同 java 的 Class.newInstance()方法。
*
* 返回的構(gòu)造函數(shù)會執(zhí)行名為 initialize 的方法, initialize 是 Ruby 對象的構(gòu)造器方法名字。
* 此時initialize方法還沒有定義,其后的代碼中創(chuàng)建新類型時會建立相應(yīng)的同名方法。
*/
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
Class.create實際上是返回一個函數(shù),那么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的時候會執(zhí)行該返回的函數(shù),即執(zhí)行this.initialize.apply(this, arguments); 此時的this就是新生成的對象,這也就是說了所有對象的初始化工作全部委托給initialize函數(shù)了。
-------
這里為什么要把自己的initialize方法綁定到自己身上??
相關(guān)文章
不錯的一篇關(guān)于javascript-prototype繼承
不錯的一篇關(guān)于javascript-prototype繼承...2007-08-08
prototype Element學(xué)習(xí)筆記(Element篇三)
上一篇把Element的所函數(shù)都梳理了一遍,下面總結(jié)一下這些函數(shù)的功能,畢竟函數(shù)太多,不分門別類一下還是沒有底。2008-10-10
Prototype Number對象 學(xué)習(xí)
這個對象提供一些操作數(shù)值類型的工具函數(shù)2009-07-07
Prototype源碼淺析 String部分(三)之HTML字符串處理
現(xiàn)在,String部分轉(zhuǎn)入具體的關(guān)聯(lián)應(yīng)用,分別對應(yīng)HTML字符串,JSON字符串和HTML中的腳本字符串2012-01-01
基礎(chǔ)的prototype.js常用函數(shù)及其用法
基礎(chǔ)的prototype.js常用函數(shù)及其用法...2007-03-03

