Prototype ObjectRange對象學(xué)習(xí)
更新時間:2009年07月19日 01:08:34 作者:
ObjectRange對象基本就是實現(xiàn)了連續(xù)的數(shù)字或者字符串,其中只包含一個方法,include,判斷某個數(shù)字或者字符串是否在ObjectRange里。并且ObjectRange對象還混入了Enumerable的方法,所以可以直接在ObjectRange對象上調(diào)用Enumerable對象里面的方法。
Ranges represent an interval of values. The value type just needs to be “compatible,” that is, to implement a succ method letting us step from one value to the next (its successor).
Prototype provides such a method for Number and String, but you are of course welcome to implement useful semantics in your own objects, in order to enable ranges based on them.
ObjectRange對象基本就是實現(xiàn)了連續(xù)的數(shù)字或者字符串,其中只包含一個方法,include,判斷某個數(shù)字或者字符串是否在ObjectRange里。并且ObjectRange對象還混入了Enumerable的方法,所以可以直接在ObjectRange對象上調(diào)用Enumerable對象里面的方法。
//創(chuàng)建ObjectRange的便捷方法
function $R(start, end, exclusive) {
return new ObjectRange(start, end, exclusive);
}
//創(chuàng)建ObjectRange對象并且繼承自Enumerable
var ObjectRange = Class.create(Enumerable, (function() {
//初始化方法,exclusive為true時,不包含end數(shù)值,默認(rèn)為undefined也就相當(dāng)于false
function initialize(start, end, exclusive) {
this.start = start;
this.end = end;
this.exclusive = exclusive;
}
//覆蓋Enumerable里面的_each方法,在遍歷ObjectRange對象時需要用到此方法
function _each(iterator) {
var value = this.start;
while (this.include(value)) {
iterator(value);
value = value.succ();
}
}
//判斷某個數(shù)值或者字符串是否包含在ObjectRange對象里
function include(value) {
if (value < this.start)
return false;
if (this.exclusive)
return value < this.end;
return value <= this.end;
}
return {
initialize: initialize,
_each: _each,
include: include
};
})());
看一下示例,然后在詳細(xì)解釋一些細(xì)節(jié):
$A($R('a', 'e'))
// -> ['a', 'b', 'c', 'd', 'e'], no surprise there
//千萬不要嘗試輸出下面返回的結(jié)果,否者將會造成瀏覽器直接死掉
$A($R('ax', 'ba'))
// -> Ouch! Humongous array, starting as ['ax', 'ay', 'az', 'a{', 'a|', 'a}', 'a~'...]
這里說一下$A($R('a', 'e')),如何返回值。首先看$A方法,前面的文章【Prototype 學(xué)習(xí)——工具函數(shù)學(xué)習(xí)($A方法)】中已經(jīng)詳細(xì)講解了$A方法,不知道請自行參考。在$A方法里面有這樣一句:if ('toArray' in Object(iterable)) return iterable.toArray();我們知道,ObjectRange里面混入了Enumerable里面的方法,也就是說間接實現(xiàn)了toArray方法,那么看一下Enumerable里面的toArray方法:
function toArray() {
return this.map();
}
//======> this.map()
//我們注意到在返回的時候map方法被映射到了collect方法
return {
//...
collect: collect,
map: collect,
//...
}
//======> collect()
//在本例中這個方法其實就相當(dāng)于返回一個數(shù)組,因為傳進(jìn)來的參數(shù)都是undefined。這里面有一個this.each方法,繼續(xù)查看
function collect(iterator, context) {
iterator = iterator || Prototype.K;
var results = [];
this.each(function(value, index) {
results.push(iterator.call(context, value, index));
});
return results;
}
//======> this.each()
//終于看到this._each了,現(xiàn)在明白為什么ObjectRange里面會重寫了_each方法了吧。在遍歷的時候要用到這個方法
function each(iterator, context) {
var index = 0;
try {
this._each(function(value) {
iterator.call(context, value, index++);
});
} catch (e) {
if (e != $break) throw e;
}
return this;
}
//======> this._each()
//詳細(xì)說明一下this._each()
//關(guān)鍵就是succ()這個方法,因為_each里面使用這個方法產(chǎn)生下一個數(shù)值。
//這個succ()在哪里呢?在Number.prototype和String.prototype里面定義了這個方法
function _each(iterator) {
var value = this.start;
while (this.include(value)) {
iterator(value);
value = value.succ();
}
}
//下面兩個方法我就不講了吧。
//======> String.prototype.succ()
function succ() {
return this.slice(0, this.length - 1) +
String.fromCharCode(this.charCodeAt(this.length - 1) + 1);
}
//======> Number.prototype.succ()
function succ() {
return this + 1;
}
//綜上所述,如果你自己想定義其它類型的ObjectRange對象,譬如Date類型,那么你就要自己實現(xiàn)succ()方法,用來生成連續(xù)的對象
上面的流程將清楚了,但一些函數(shù)沒有仔細(xì)講,等講到這些對象的時候在仔細(xì)說明里面的函數(shù)。下面看幾個include的示例吧:
$R(1, 10).include(5)
// -> true
$R('a', 'h').include('x')
// -> false
$R(1, 10).include(10)
// -> true
$R(1, 10, true).include(10)
// -> false
Prototype provides such a method for Number and String, but you are of course welcome to implement useful semantics in your own objects, in order to enable ranges based on them.
ObjectRange對象基本就是實現(xiàn)了連續(xù)的數(shù)字或者字符串,其中只包含一個方法,include,判斷某個數(shù)字或者字符串是否在ObjectRange里。并且ObjectRange對象還混入了Enumerable的方法,所以可以直接在ObjectRange對象上調(diào)用Enumerable對象里面的方法。
復(fù)制代碼 代碼如下:
//創(chuàng)建ObjectRange的便捷方法
function $R(start, end, exclusive) {
return new ObjectRange(start, end, exclusive);
}
//創(chuàng)建ObjectRange對象并且繼承自Enumerable
var ObjectRange = Class.create(Enumerable, (function() {
//初始化方法,exclusive為true時,不包含end數(shù)值,默認(rèn)為undefined也就相當(dāng)于false
function initialize(start, end, exclusive) {
this.start = start;
this.end = end;
this.exclusive = exclusive;
}
//覆蓋Enumerable里面的_each方法,在遍歷ObjectRange對象時需要用到此方法
function _each(iterator) {
var value = this.start;
while (this.include(value)) {
iterator(value);
value = value.succ();
}
}
//判斷某個數(shù)值或者字符串是否包含在ObjectRange對象里
function include(value) {
if (value < this.start)
return false;
if (this.exclusive)
return value < this.end;
return value <= this.end;
}
return {
initialize: initialize,
_each: _each,
include: include
};
})());
看一下示例,然后在詳細(xì)解釋一些細(xì)節(jié):
復(fù)制代碼 代碼如下:
$A($R('a', 'e'))
// -> ['a', 'b', 'c', 'd', 'e'], no surprise there
//千萬不要嘗試輸出下面返回的結(jié)果,否者將會造成瀏覽器直接死掉
$A($R('ax', 'ba'))
// -> Ouch! Humongous array, starting as ['ax', 'ay', 'az', 'a{', 'a|', 'a}', 'a~'...]
這里說一下$A($R('a', 'e')),如何返回值。首先看$A方法,前面的文章【Prototype 學(xué)習(xí)——工具函數(shù)學(xué)習(xí)($A方法)】中已經(jīng)詳細(xì)講解了$A方法,不知道請自行參考。在$A方法里面有這樣一句:if ('toArray' in Object(iterable)) return iterable.toArray();我們知道,ObjectRange里面混入了Enumerable里面的方法,也就是說間接實現(xiàn)了toArray方法,那么看一下Enumerable里面的toArray方法:
復(fù)制代碼 代碼如下:
function toArray() {
return this.map();
}
//======> this.map()
//我們注意到在返回的時候map方法被映射到了collect方法
return {
//...
collect: collect,
map: collect,
//...
}
//======> collect()
//在本例中這個方法其實就相當(dāng)于返回一個數(shù)組,因為傳進(jìn)來的參數(shù)都是undefined。這里面有一個this.each方法,繼續(xù)查看
function collect(iterator, context) {
iterator = iterator || Prototype.K;
var results = [];
this.each(function(value, index) {
results.push(iterator.call(context, value, index));
});
return results;
}
//======> this.each()
//終于看到this._each了,現(xiàn)在明白為什么ObjectRange里面會重寫了_each方法了吧。在遍歷的時候要用到這個方法
function each(iterator, context) {
var index = 0;
try {
this._each(function(value) {
iterator.call(context, value, index++);
});
} catch (e) {
if (e != $break) throw e;
}
return this;
}
//======> this._each()
//詳細(xì)說明一下this._each()
//關(guān)鍵就是succ()這個方法,因為_each里面使用這個方法產(chǎn)生下一個數(shù)值。
//這個succ()在哪里呢?在Number.prototype和String.prototype里面定義了這個方法
function _each(iterator) {
var value = this.start;
while (this.include(value)) {
iterator(value);
value = value.succ();
}
}
//下面兩個方法我就不講了吧。
//======> String.prototype.succ()
function succ() {
return this.slice(0, this.length - 1) +
String.fromCharCode(this.charCodeAt(this.length - 1) + 1);
}
//======> Number.prototype.succ()
function succ() {
return this + 1;
}
//綜上所述,如果你自己想定義其它類型的ObjectRange對象,譬如Date類型,那么你就要自己實現(xiàn)succ()方法,用來生成連續(xù)的對象
上面的流程將清楚了,但一些函數(shù)沒有仔細(xì)講,等講到這些對象的時候在仔細(xì)說明里面的函數(shù)。下面看幾個include的示例吧:
復(fù)制代碼 代碼如下:
$R(1, 10).include(5)
// -> true
$R('a', 'h').include('x')
// -> false
$R(1, 10).include(10)
// -> true
$R(1, 10, true).include(10)
// -> false
相關(guān)文章
Prototype源碼淺析 String部分(四)之補(bǔ)充
Prototype源碼淺析 String部分(四)之補(bǔ)充,需要的朋友可以參考下。2012-01-01Prototype 學(xué)習(xí) 工具函數(shù)學(xué)習(xí)($A方法)
Prototype 學(xué)習(xí) 工具函數(shù)學(xué)習(xí)($A使用方法)2009-07-07初學(xué)prototype,發(fā)個JS接受URL參數(shù)的代碼
初學(xué)prototype,發(fā)個JS接受URL參數(shù)的代碼...2007-02-02Prototype 學(xué)習(xí) 工具函數(shù)學(xué)習(xí)($w,$F方法)
Prototype $w $F使用方法2009-07-07滾動經(jīng)典最新話題[prototype框架]下編寫
滾動經(jīng)典最新話題[prototype框架]下編寫...2006-10-10