欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Prototype Enumerable對(duì)象 學(xué)習(xí)

 更新時(shí)間:2009年07月19日 01:18:33   作者:  
Enumerable是Prototype框架的基石,而Enumerable不單獨(dú)使用,在Prototype中其它對(duì)象mix了Enumerable里面的方法,這樣就可以在這些對(duì)象上應(yīng)用Enumerable的方法,這樣的對(duì)象有:Array,Hash,ObjectRange,還有一些和DOM,AJAX相關(guān)的對(duì)象。

any方法:

跟all方法差不多,就不詳細(xì)說(shuō)了,看一下示例
復(fù)制代碼 代碼如下:

[].any()
// -> false (empty arrays have no elements that could be true-equivalent)

$R(0, 2).any()
// -> true (on the second loop cycle, 1 is true-equivalent)

[2, 4, 6, 8, 10].any(function(n) { return 0 == n % 3; })
// -> true (the iterator will return true on 6: the array does have 1+ multiple of 3)

$H({ opt1: null, opt2: false, opt3: '', opt4: 'pfew!' }).any(function(pair) { return pair.value; })
// -> true (thanks to the opt4/'pfew!' pair, whose value is true-equivalent)

collect/map(collect方法別名)方法:

 

Returns the results of applying the iterator to each element. Aliased as map.

This is a sort of Swiss-Army knife for sequences. You can turn the original values into virtually anything!

這個(gè)方法被稱為”瑞士軍刀“,這個(gè)方法基本上可以對(duì)數(shù)據(jù)進(jìn)行任何操作,其中map方法是這個(gè)方法的別名,這個(gè)方法實(shí)現(xiàn)很簡(jiǎn)單,results.push(iterator.call(context, value, index));這句話是關(guān)鍵,就是對(duì)每個(gè)數(shù)據(jù)進(jìn)行iterator調(diào)用,并且把結(jié)果存儲(chǔ)到要返回的數(shù)組中,看一下示例:

復(fù)制代碼 代碼如下:

['Hitch', "Hiker's", 'Guide', 'To', 'The', 'Galaxy'].collect(function(s) { return s.charAt(0).toUpperCase(); }).join('')
// -> 'HHGTTG'

$R(1,5).collect(function(n) { return n * n; })
// -> [1, 4, 9, 16, 25]

注意幫助文檔上最后有這樣幾行提示:

First, the method-calling scenario: you want to invoke the same method on all elements, possibly with arguments, and use the result values. This can be achieved easily with invoke.

Second, the property-fetching scenario: you want to fetch the same property on all elements, and use those. This is a breeze with pluck.
detect方法:
找到第一個(gè)滿足某個(gè)條件的數(shù)據(jù),并返回,看一下示例,其實(shí)這個(gè)方法就是find的別名,所以調(diào)用detect和find是一樣的:
復(fù)制代碼 代碼如下:

// An optimal exact prime detection method, slightly compacted.
function isPrime(n) {
if (2 > n) return false;
if (0 == n % 2) return (2 == n);
for (var index = 3; n / index > index; index += 2)
if (0 == n % index) return false;
return true;
}
// isPrime
$R(10,15).find(isPrime) // -> 11

[ 'hello', 'world', 'this', 'is', 'nice'].find(function(s) { return s.length <= 3; })
// -> 'is'

each方法:

調(diào)用這個(gè)方法時(shí),其實(shí)就是對(duì)每個(gè)數(shù)據(jù)進(jìn)行iterator操作,傳入iterator函數(shù)的第一個(gè)參數(shù)為數(shù)據(jù),第二個(gè)參數(shù)為索引,在遍歷過(guò)程中可以拋出$continue和$break異常,注意:

The usage of $continue is deprecated. This feature will not be available in releases after Prototype 1.5 in favor of speed.

看一下示例:

復(fù)制代碼 代碼如下:

['one', 'two', 'three'].each(function(s) { alert(s); });

[ 'hello', 'world'].each(function(s, index) { alert(index + ': ' + s); });
// alerts -> '0: hello' then '1: world'

// This could be done better with an accumulator using inject, but humor me
// here...
var result = [];
$R(1,10).each(function(n) {
if (0 == n % 2) throw $continue;
if (n > 6) throw $break;
result.push(n);
});
// result -> [1, 3, 5]

eachSlice方法:

下面很多簡(jiǎn)單的方法就直接給出示例了,不在解釋了,算法也沒有什么難得,基本一看就懂:

復(fù)制代碼 代碼如下:

var students = [ { name: 'Sunny', age: 20 }, { name: 'Audrey', age: 21 }, { name: 'Matt', age: 20 }, { name: 'Élodie', age: 26 }, { name: 'Will', age: 21 }, { name: 'David', age: 23 }, { name: 'Julien', age: 22 }, { name: 'Thomas', age: 21 }, { name: 'Serpil', age: 22 } ];

students.eachSlice(4, function(toon) { return toon.pluck('name'); })
// -> [ ['Sunny', 'Audrey', 'Matt', 'Élodie'],
// ['Will', 'David', 'Julien', 'Thomas'],
// ['Serpil'] ]

//下面的first方法是Array對(duì)象提供的
students.eachSlice(2).first()
// -> [{ name: 'Sunny', age: 20 }, { name: 'Audrey', age: 21 }]

entries方法,就是toArray方法,toArray方法里面又調(diào)用map方法,map方法就相當(dāng)于collect方法:
復(fù)制代碼 代碼如下:

$R(1, 5).toArray() // -> [1, 2, 3, 4, 5]

find/findAll(select方法的別名)方法:
復(fù)制代碼 代碼如下:

//find方法
[ 'hello', 'world', 'this', 'is', 'nice'].find(function(s) { return s.length <= 3; })
// -> 'is'
//findAll方法
$R(1, 10).findAll(function(n) { return 0 == n % 2; })
// -> [2, 4, 6, 8, 10] [ 'hello', 'world', 'this', 'is', 'nice'].findAll(function(s) { return s.length >= 5; })
// -> ['hello', 'world']

grep方法:
這個(gè)方法需要注意的就是,參數(shù)filter在函數(shù)里面是要被統(tǒng)一成正則表達(dá)式的,然后調(diào)用正則表達(dá)式的match方法來(lái)進(jìn)行判斷
復(fù)制代碼 代碼如下:

// Get all strings with a repeated letter somewhere
['hello', 'world', 'this', 'is', 'cool'].grep(/(.)\1/)
// -> ['hello', 'cool']
// Get all numbers ending with 0 or 5
$R(1,30).grep(/[05]$/)
// -> [5, 10, 15, 20, 25, 30]
// Those, minus 1
$R(1,30).grep(/[05]$/, function(n) { return n - 1; })
// -> [4, 9, 14, 19, 24, 29]
// Get all strings with a repeated letter somewhere
['hello', 'world', 'this', 'is', 'cool'].grep(/(.)\1/)
// -> ['hello', 'cool']
// Get all numbers ending with 0 or 5
$R(1,30).grep(/[05]$/)
// -> [5, 10, 15, 20, 25, 30]
// Those, minus 1
$R(1,30).grep(/[05]$/, function(n) { return n - 1; })
// -> [4, 9, 14, 19, 24, 29]

inGroupsOf方法:
復(fù)制代碼 代碼如下:

var students = [ { name: 'Sunny', age: 20 }, { name: 'Audrey', age: 21 }, { name: 'Matt', age: 20 }, { name: 'Élodie', age: 26 }, { name: 'Will', age: 21 }, { name: 'David', age: 23 }, { name: 'Julien', age: 22 }, { name: 'Thomas', age: 21 }, { name: 'Serpil', age: 22 } ];
//pluck方法就是取得對(duì)象的某個(gè)屬性,這里取得的是name屬性
students.pluck('name').inGroupsOf(4)
// -> [ ['Sunny', 'Audrey', 'Matt', 'Élodie'],
// ['Will', 'David', 'Julien', 'Thomas'],
// ['Serpil', null, null, null] ]

include/member(include方法的別名)方法,這里先檢查了一下對(duì)象上是否有indexOf方法,如果有的話則直接調(diào)用這個(gè)方法:
復(fù)制代碼 代碼如下:

$R(1,15).include(10)
// -> true
['hello', 'world'].include('HELLO')
// -> false
[1, 2, '3', '4', '5'].include(3)
// -> true (== ignores actual type)


inject方法:
復(fù)制代碼 代碼如下:

$R(1,10).inject(0, function(acc, n) { return acc + n; })
// -> 55 (sum of 1 to 10)
$R(2,5).inject(1, function(acc, n) { return acc * n; })
// -> 120 (factorial 5)
['hello', 'world', 'this', 'is', 'nice'].inject([], function(array, value, index) {
if (0 == index % 2) array.push(value);
return array;
})
// -> ['hello', 'this', 'nice']


invoke方法:
復(fù)制代碼 代碼如下:

['hello', 'world', 'cool!'].invoke('toUpperCase')
// ['HELLO', 'WORLD', 'COOL!']
['hello', 'world', 'cool!'].invoke('substring', 0, 3)
// ['hel', 'wor', 'coo']

max/min方法:
復(fù)制代碼 代碼如下:

$R(1,10).max() // -> 10
['hello', 'world', 'gizmo'].max()
// -> 'world'
function Person(name, age) { this.name = name; this.age = age; }
var john = new Person('John', 20);
var mark = new Person('Mark', 35);
var daisy = new Person('Daisy', 22);
[john, mark, daisy].max(function(person) { return person.age; })
// -> 35


partition方法:
復(fù)制代碼 代碼如下:

['hello', null, 42, false, true, , 17].partition()
// -> [['hello', 42, true, 17], [null, false, undefined]]
$R(1, 10).partition(function(n) { return 0 == n % 2; })
// -> [[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]]
['hello', null, 42, false, true, , 17].partition()
// -> [['hello', 42, true, 17], [null, false, undefined]]
$R(1, 10).partition(function(n) { return 0 == n % 2; })
// -> [[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]]

pluck方法:
復(fù)制代碼 代碼如下:

['hello', 'world', 'this', 'is', 'nice'].pluck('length')
// -> [5, 5, 4, 3, 4]


reject方法:
復(fù)制代碼 代碼如下:

$R(1, 10).reject(function(n) { return 0 == n % 2; })
// -> [1, 3, 5, 7, 9]
[ 'hello', 'world', 'this', 'is', 'nice'].reject(function(s) { return s.length >= 5; })
// -> ['this', 'is', 'nice']
$R(1, 10).reject(function(n) { return 0 == n % 2; })
// -> [1, 3, 5, 7, 9]
[ 'hello', 'world', 'this', 'is', 'nice'].reject(function(s) { return s.length >= 5; })
// -> ['this', 'is', 'nice']

size方法省略。

sortBy方法:
這個(gè)方法首先通過(guò)map方法返回一個(gè)對(duì)象數(shù)組,然后調(diào)用數(shù)組的sort方法,最后在取出對(duì)象中的value屬性
復(fù)制代碼 代碼如下:

['hello', 'world', 'this', 'is', 'nice'].sortBy(function(s) { return s.length; })
// -> 'is', 'this', 'nice', 'hello', 'world']
['hello', 'world', 'this', 'is', 'cool'].sortBy(function(s) {
var md = s.match(/[aeiouy]/g);
return null == md ? 0 : md.length;
})
// -> [ 'world', 'this', 'is', 'hello', 'cool'] (sorted by vowel count)

zip方法:
復(fù)制代碼 代碼如下:

var firstNames = ['Justin', 'Mislav', 'Tobie', 'Christophe'];
var lastNames = ['Palmer', 'Marohnić', 'Langel', 'Porteneuve'];
firstNames.zip(lastNames)
// -> [['Justin', 'Palmer'], ['Mislav', 'Marohnić'], ['Tobie', 'Langel'], ['Christophe', 'Porteneuve']]
//通過(guò)這個(gè)例子我們可以看出參數(shù)a就是一個(gè)數(shù)組,表示兩個(gè)數(shù)組中的相對(duì)應(yīng)的項(xiàng)
firstNames.zip(lastNames, function(a) { return a.join(' '); })
// -> ['Justin Palmer', 'Mislav Marohnić', 'Tobie Langel', 'Christophe Porteneuve']
//通過(guò)這個(gè)例子我們可以看到傳入的可以是多個(gè)數(shù)組
var cities = ['Memphis', 'Zagreb', 'Montreal', 'Paris'];
firstNames.zip(lastNames, cities, function(p) { return p[0] + ' ' + p[1] + ', ' + p[2]; })
// -> ['Justin Palmer, Memphis', 'Mislav Marohnić, Zagreb', 'Tobie Langel, Montreal', 'Christophe Porteneuve, Paris']
firstNames.zip($R(1, 100), function(a) { return a.reverse().join('. '); })
// -> ['1. Justin', '2. Mislav', '3. Tobie', '4. Christophe']

相關(guān)文章

最新評(píng)論