Prototype ObjectRange對象學習_prototype

來源:互聯網
上載者:User
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對象基本就是實現了連續的數字或者字串,其中只包含一個方法,include,判斷某個數字或者字串是否在ObjectRange裡。並且ObjectRange對象還混入了Enumerable的方法,所以可以直接在ObjectRange對象上調用Enumerable對象裡面的方法。
複製代碼 代碼如下:

//建立ObjectRange的便捷方法
function $R(start, end, exclusive) {
return new ObjectRange(start, end, exclusive);
}

//建立ObjectRange對象並且繼承自Enumerable
var ObjectRange = Class.create(Enumerable, (function() {
    //初始化方法,exclusive為true時,不包含end數值,預設為undefined也就相當於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();
}
}

//判斷某個數值或者字串是否包含在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
};
})());

看一下樣本,然後在詳細解釋一些細節:
複製代碼 代碼如下:

$A($R('a', 'e'))
// -> ['a', 'b', 'c', 'd', 'e'], no surprise there

//千萬不要嘗試輸出下面返回的結果,否者將會造成瀏覽器直接死掉
$A($R('ax', 'ba'))
// -> Ouch! Humongous array, starting as ['ax', 'ay', 'az', 'a{', 'a|', 'a}', 'a~'...]

這裡說一下$A($R('a', 'e')),如何傳回值。首先看$A方法,前面的文章【Prototype 學習——工具函數學習($A方法)】中已經詳細講解了$A方法,不知道請自行參考。在$A方法裡面有這樣一句:if ('toArray' in Object(iterable)) return iterable.toArray();我們知道,ObjectRange裡面混入了Enumerable裡面的方法,也就是說間接實現了toArray方法,那麼看一下Enumerable裡面的toArray方法:
複製代碼 代碼如下:

function toArray() {
return this.map();
}

//======> this.map()

//我們注意到在返回的時候map方法被映射到了collect方法
return {
//...
collect: collect,
map: collect,
//...
}

//======> collect()

//在本例中這個方法其實就相當於返回一個數組,因為傳進來的參數都是undefined。這裡面有一個this.each方法,繼續查看
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了,現在明白為什麼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()

//詳細說明一下this._each()
//關鍵就是succ()這個方法,因為_each裡面使用這個方法產生下一個數值。
//這個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類型,那麼你就要自己實現succ()方法,用來產生連續的對象

上面的流程將清楚了,但一些函數沒有仔細講,等講到這些對象的時候在仔細說明裡面的函數。下面看幾個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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.