Prototype Enumerable对象 学习
any方法:
跟all方法差不多,就不详细说了,看一下示例
[].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!
这个方法被称为”瑞士军刀“,这个方法基本上可以对数据进行任何操作,其中map方法是这个方法的别名,这个方法实现很简单,results.push(iterator.call(context, value, index));这句话是关键,就是对每个数据进行iterator调用,并且把结果存储到要返回的数组中,看一下示例:
['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方法:
找到第一个满足某个条件的数据,并返回,看一下示例,其实这个方法就是find的别名,所以调用detect和find是一样的:
// 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方法:
调用这个方法时,其实就是对每个数据进行iterator操作,传入iterator函数的第一个参数为数据,第二个参数为索引,在遍历过程中可以抛出$continue和$break异常,注意:
The usage of $continue
is deprecated. This feature will not be available in releases after Prototype 1.5 in favor of speed.
看一下示例:
['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方法:
下面很多简单的方法就直接给出示例了,不在解释了,算法也没有什么难得,基本一看就懂:
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对象提供的
students.eachSlice(2).first()
// -> [{ name: 'Sunny', age: 20 }, { name: 'Audrey', age: 21 }]
entries方法,就是toArray方法,toArray方法里面又调用map方法,map方法就相当于collect方法:
$R(1, 5).toArray() // -> [1, 2, 3, 4, 5]
find/findAll(select方法的别名)方法:
//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方法:
这个方法需要注意的就是,参数filter在函数里面是要被统一成正则表达式的,然后调用正则表达式的match方法来进行判断
// 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方法:
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方法就是取得对象的某个属性,这里取得的是name属性
students.pluck('name').inGroupsOf(4)
// -> [ ['Sunny', 'Audrey', 'Matt', 'Élodie'],
// ['Will', 'David', 'Julien', 'Thomas'],
// ['Serpil', null, null, null] ]
include/member(include方法的别名)方法,这里先检查了一下对象上是否有indexOf方法,如果有的话则直接调用这个方法:
$R(1,15).include(10)
// -> true
['hello', 'world'].include('HELLO')
// -> false
[1, 2, '3', '4', '5'].include(3)
// -> true (== ignores actual type)
inject方法:
$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方法:
['hello', 'world', 'cool!'].invoke('toUpperCase')
// ['HELLO', 'WORLD', 'COOL!']
['hello', 'world', 'cool!'].invoke('substring', 0, 3)
// ['hel', 'wor', 'coo']
max/min方法:
$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方法:
['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方法:
['hello', 'world', 'this', 'is', 'nice'].pluck('length')
// -> [5, 5, 4, 3, 4]
reject方法:
$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方法:
这个方法首先通过map方法返回一个对象数组,然后调用数组的sort方法,最后在取出对象中的value属性
['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方法:
var firstNames = ['Justin', 'Mislav', 'Tobie', 'Christophe'];
var lastNames = ['Palmer', 'Marohnić', 'Langel', 'Porteneuve'];
firstNames.zip(lastNames)
// -> [['Justin', 'Palmer'], ['Mislav', 'Marohnić'], ['Tobie', 'Langel'], ['Christophe', 'Porteneuve']]
//通过这个例子我们可以看出参数a就是一个数组,表示两个数组中的相对应的项
firstNames.zip(lastNames, function(a) { return a.join(' '); })
// -> ['Justin Palmer', 'Mislav Marohnić', 'Tobie Langel', 'Christophe Porteneuve']
//通过这个例子我们可以看到传入的可以是多个数组
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']
相关文章
prototype Element学习笔记(Element篇三)
上一篇把Element的所函数都梳理了一遍,下面总结一下这些函数的功能,毕竟函数太多,不分门别类一下还是没有底。2008-10-10
最新评论