您的位置:首页 > Web前端 > JavaScript

prototype.js1.5.0开发者手册 译文+添加注释(三)

2007-09-21 00:20 681 查看
本人辛苦翻译的,转载或引用请短信通知下,谢谢!

prototype.js1.5.0开发者手册 2007-09-21 by lin49940本人为学习的目的而翻译的

目录

10. 在 prototype.js中定义的新对象和类

PeriodicalExecuter 对象

Prototype 对象

Enumerable 对象

Hash 对象

ObjectRange 类

Class 对象

top
在 prototype.js中定义的新对象和类

这个程序包另外能帮助你的地方就是提供既支持面向对象设计理念又有共通功能的许多对象。

top
PeriodicalExecuter 对象

这个对象提供使用一个设定时间的定时器重复调用指定方法的逻辑。

MethodKindArgumentsDescription
[ctor](callback, interval)constructor

构造函数

callback: a function that will be passed the PeriodcalExecuter object itself as the only argument(只能传入唯一参数 PeriodcalExecuter对象的方法), interval: 秒数

创建一个PeriodicalExecuter对象实例,这个实例将重复调用callback方法

registerCallback()instance(none)重置定时器.

stop()instance(none)取消定时器,避免callback方法的执行.

onTimerEvent()instance(none)This method is what will be called by the timer. It, in turn, will invoke the callback method passing the object itself.(貌似灰色字体的方法都是私有的,不用管)

PropertyTypeDescription
callbackFunction(objExecuter)被调用的方法. objExecuter:调用方法的PeriodcalExecuter对象.(注意:在前面的版本中,callback函数是不传入参数的)

timerTimer基于timer对象的一个句柄,负责重复调用callback方法.

frequencyNumber以秒为单位的间隔。

currentlyExecutingBoolean标识callback方法是否正在执行中。

top
Prototype 对象
Prototype 没有太重要的作用,只是声明了使用的prototype.js库的版本 。

属性类型描述
VersionString库的版本
emptyFunctionFunction()一个空方法对象.
KFunction(obj)一个只是回传指定参数的方法对象.

ScriptFragmentString识别scripts的正则表达式

top
Enumerable 对象 ( 好多 - -#!!)
Enumerable 对象允许你写更优雅的代码去迭代列表样式的结构中的项目.

很多其它的对象通过继承自 Enumberable 对象来得到它有用的接口。

方法类别参数描述
each(iterator)instanceiterator: a function object conforming to Function(value, index)(方法对象,遵照Function(value,index)这样的格式.)

调用指定的迭次(iterator)函数,这个函数在第一个参数中传入列表的每个元素,在第二个参数中传入元素的索引(index).

all([iterator])instanceiterator: a function object conforming to Function(value, index), optional.(方法对象,遵照Function(value,index)这样的格式.这个参数的可选的)这个方法是通过指定的iterator函数测试整个集合(collection)的值的一种方式. 如果所有的元素在iterator函数中返回的值true, all方法将返回true, 否则将返回false. 假设没有iterator函数被指定,这个测试返回true如果这个元素本身解析为true.你能简单地把这个方法理解为" 检测所有的元素是否都通过测试".(不能为true的基本都是null或undefined,这里的集合包括Array,Hash等,下面的方法也一样)

any([iterator])instanceiterator: a function object conforming to Function(value, index), optional.(同上)

这个方法是通过指定的iterator函数测试整个集合(collection)的值的一种方式. 如果在iterator函数中返回的值中至少一个值为true, any方法将返回true, 否则将返回false. 假设没有iterator函数被指定,这个测试返回true如果这个元素本身解析为true.你能简单地把这个方法理解为"检测是否有元素通过测试".

collect(iterator)instanceiterator: a function object conforming to Function(value, index)对集合(collection)中的每个元素调用iterator函数,返回每个结果到一个数组中,一个结果对应集合一个元素,这些结果在数组中的保持原先集合中的顺序.

detect(iterator)instanceiterator: a function object conforming to Function(value, index)对集合(collection)中的每个元素调用iterator函数, 返回第一个iterator函数返回值为true. (或者,更正确地,not-false)的元素. 如果没有元素返回true, detect()返回null.

entries()instance(none)Same as toArray(). 和toArray()一样
find(iterator)instanceiterator: a function object conforming to Function(value, index)Same as detect(). 和detect()一样
findAll(iterator)instanceiterator: a function object conforming to Function(value, index)对集合(coolection) 中的每个元素调用iterator函数,把所有在iterator函数中返回值为true的元素放到数组中, 返回这个数组. 这个方法跟reject()作用相反.

grep(pattern [, iterator])instancepattern: a RegExp object used to match the elements,用于匹配元素的正则表达式对象, iterator: a function object conforming to Function(value, index)针对pattern正则表达式,检测集合(collection)中每个元素的字符串值. 这个方法将返回一个包含所有匹配pattern的元素的数组.如果iterator函数被指定, 返回的数组将包含调用iterator函数返回的每个匹配的元素.

include(obj)instanceobj:任何对象尝试在集合(collection)中查找指定对象. 如果找到,返回true, 否则返回false.

inGroupsOf(number, fillWith)instancenumber: number of items per group,每个组项目的数量,fillWith: value used to fill empty spots,用来填充空点的值.返回由第一个参数number指定项目的数量, 进行分组的集合(collection).如果源集合中项目的数量不能被第一个参数的数字整除,最后一个分组中空的项目将用null填充,如果第二个参数有提供值,那将用这个值填充. (简单例子: ['a','b','c','d'].inGroupsOf(3,'?') creates [ ['a','b','c'] , ['d','?','?'] ],有时间测试下是不是改变源集合 )

inject(initialValue, iterator)instanceinitialValue: any object to be used as the initial value,任何对象,被用来作为的初始值, iterator: a function object conforming to Function(accumulator, value, index)使用iterator函数联合集合(collection)的所有元素. Iterator在被调用时,把上一次迭代的结果传入到参数accumulator中.第一次迭代时,从accurmelator中取得initialValue.最终结果是最后返回的值. (这个方法是不改变源集合的, iterator(accumulator,value,index),index是索引,从0开始, value是集合中第index个元素的值, accumulator开始的时候是取initiaValue的值,后面是return什么值,就取什么值,最后的迭次后返回的值才是最终结果,而不是accurmelator. 可以拿来求数组元素的总值等等.)

invoke(methodName [, arg1 [, arg2 [...]]])instancemethodName: name of the method that will be called in each element,将在每个元素中被调用的方法的名称, arg1..argN: arguments that will be passed in the method invocation.在集合(collection)中的每个元素通过方法名称调用指定的方法,传入任何指定的参数(arg1 to argN),并返回结果到数组对象中.

map(iterator)instanceiterator: a function object conforming to Function(value, index)Same as collect(). 和collect()一样.
max([iterator])instanceiterator: a function object conforming to Function(value, index)返回集合(collection)中最大值的元素, 如果指定iterator函数,在集合中的每个元素调用iterator函数, 返回最大的结果值.(没有指定iterator函数,返回的是最大值的元素,而不是元素的最大值,可以用Hash试试就知道了,如果是普通的数组,根本没区别,大小的区别应该是按照ASCII吗!)

member(obj)instanceobj: 任何对象Same as include(). 和include()一样.
min([iterator])instanceiterator: a function object conforming to Function(value, index)返回集合(collection)中最小值的元素, 如果指定iterator函数,在集合中的每个元素调用iterator函数, 返回最小的结果值.(其他的看上面的max())

partition([iterator])instanceiterator: a function object conforming to Function(value, index)返回包含2个不同数组的数组. 第一个数组将包含所有调用iterator函数返回true的元素, 第二个数组将包含剩余的元素.如果没指定iterator函数,第一个数组将包含本身解析为true的元素,另一个数组将包含剩余的元素.

pluck(propertyName)instancepropertyName name of the property that will be read from each element. This can also contain the index of the element在集合(collection)中根据指定属性名查找该属性的值,返回结果到数组对象中.(propertyName除了是属性名之外,也可以是元素的索引index.)

reject(iterator)instanceiterator: a function object conforming to Function(value, index)对集合(coolection) 中的每个元素调用iterator函数,把所有在iterator函数中返回值为false的元素放到数组中,返回这个数组.这个方法跟findAll()作用相反.

select(iterator)instanceiterator: a function object conforming to Function(value, index)Same as findAll(). 和findAll()一样.
sortBy(iterator)instanceiterator: a function object conforming to Function(value, index)返回一个所有元素根据iterator函数调用结果排序的数组.

toArray()instance(none)返回一个由集合中所有元素组成的数组.

zip(collection1[, collection2 [, ... collectionN [,transform]]])instancecollection1 .. collectionN: enumerations that will be merged, transform: a function object conforming to Function(value, index)用当前集合(collection)合并每个给定的集合.合并操作返回一个新的数组,这个数组的元素数量跟当前集合一样,每个元素都是一个数组(这里称它们为子数组), 子数组是由每个合并的集合相同索引(index)的元素组成. 如果指定了transform函数, 在被返回前,每个子数组将被transform函数转换. (简单例子:[1,2,3].zip([4,5,6], [7,8,9]).inspect() returns "[ [1,4,7],[2,5,8],[3,6,9] ]" )

top
Hash 对象
Hash 对象实现了一种 hash 结构,也就是一个 Key:Value 对的集合.
Hash 对象中的每一项都是由2个元素组成的数组: 第一个元素是Key, 然后是Value. 每一项也有两个属性: keyvalue ,这两个属性不用说明了吧.

方法类别参数描述
keys()instance(none)返回一个由所有项(items)的key组成的数组.

values()instance(none)返回一个由所有项(items)的value组成的数组.

merge(otherHash)instanceotherHash: Hash对象用被传入的otherHash合并这个hash,生成新的hash.

toQueryString()instance(none)用像查询字符串格式一样的字符串返回hash的所有项, e.g.: ''key1=value1&key2=value2&key3=value3''

inspect()instance(none)覆盖Object类的inspect(), 返回一个反映hash及其key:value对精细格式的字符串表现.

top
ObjectRange 类
继承自 Enumerable
用上下边界描绘 values 的一个区域.

属性类型类别描述
start(any)instancerange的下边界
end(any)instancerange的下边界
exclusiveBooleaninstance确定上下边界本身是否成为range的一部分.

方法类别参数描述
[ctor](start, end, exclusive)constructor

构造函数

start: the lower bound, end: the upper bound, exclusive: include the bounds in the range?创建一个range对象, 跨度从start到end, 这里要重点注意, start和end必须是相同类型的对象, 并且它们必须有succ()方法。

include(searchedValue)instancesearchedValue: value that we are looking for检查指定值是否range的一部分.返回true或false.

top
Class 对象

在这个库中 class 对象在声明其他的类时候被用到. 用这个对象声明类使得新类支持 initialize() 方法, 他起构造函数的作用.

看下面的例子.


//declaring the class


var MySampleClass = Class.create();




//defining the rest of the class implementation




MySampleClass.prototype = ...{






initialize: function(message) ...{


this.message = message;


},






showMessage: function(ajaxResponse) ...{


alert(this.message);


}


};




//now, let's instantiate and use one object


var myTalker = new MySampleClass('hi there.');


myTalker.showMessage(); //displays alert

方法类别参数描述
create(*)instance(any)定义新类的构造方法。
上一部分: prototype.js1.5.0开发者手册 译文+添加注释 (二)

查考文章: https://compdoc2cn.dev.java.net/pro totype/html/prototype.js.cn.html

英文原版: http://www.sergiopereira.com/articles/prototype.js.html#Reference.NewObjects
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: