??xml version="1.0" encoding="utf-8" standalone="yes"?>国产亚洲美日韩AV中文字幕无码成人,亚洲av无码不卡私人影院,国产亚洲玖玖玖在线观看 http://www.tkk7.com/hummer008/category/31799.html不走d?要走p无间?/description>zh-cnFri, 30 May 2008 15:53:19 GMTFri, 30 May 2008 15:53:19 GMT60prototype源码分析(?) http://www.tkk7.com/hummer008/articles/204074.htmlhummerhummerFri, 30 May 2008 05:56:00 GMThttp://www.tkk7.com/hummer008/articles/204074.htmlhttp://www.tkk7.com/hummer008/comments/204074.htmlhttp://www.tkk7.com/hummer008/articles/204074.html#Feedback0http://www.tkk7.com/hummer008/comments/commentRss/204074.htmlhttp://www.tkk7.com/hummer008/services/trackbacks/204074.html

Function - 对Functioncȝ扩展

Prototype?span class="object">Function对象的扩展比较简? 但却是非帔R要且非常有用的扩展。扩展主要包?个方? 他们被加CFunction.prototype? q样׃得Q意的函数对象都具备了q两个方法?q两个方法的主要用途在于将某个函数l定到特定的函数之上L行?

Knowledge Prepare - 知识准备

Source View - 源码解析

Function.prototype.bind = function() {
var __method = this;  // q里的this表示bindҎ的调用? 是一个函数对?/span>
var args = $A(arguments);  // q里?A(arguments)表示传入?span class="method">bindҎ的参? 不要与下面的$A(arguments)h
var object = args.shift(); // 调用args.shift()Ҏq回W一个参? 即目标对? 此时args为除ȝ一个参数的一个参数数l?/span>
return function() {  // 不带有Q何的参数的函? 注意函数内部?A(arguments)的含?/span>
// 调用函数的applyҎ执行函数, 其中的object为目标对? args?span class="method">bindҎ中的参数列表(除了W一个参C外的参数构成的数l?
return __method.apply(object, args.concat($A(arguments)));// 事实? q里?A(arguments)一定是一个空数组
}
}
Function.prototype.bindAsEventListener = function(object) {
var __method = this;  // q里的this表示bindҎ的调用? 是一个函数对?/span>
return function(event) {  // 带有一个全局event参数的函?/span>
// 调用函数的callҎ执行函数, 其中的object为目标对? 全局的event对象作ؓ参数
return __method.call(object, event || window.event);
}
}

Field & Function Reference - 属性方法一?/h4>

Function ( 实例 ) - 扩展
Method / Property Kind Arguments Description
bind(object[, arg1, arg2...]) Ҏ objectQ目标对? 函数绑定到该对象上L?br /> argument list: 以参数列表的方式传入, 函数执行时所带的参数 一个实例方? 其调用者是一个函数对? 表示某个对象绑定到该函CL? 其中的第一个参数表C目标对? 其他参数作为函数执行时的参C?/td>
bindAsEventListener(object) Ҏ objectQ目标对?/td> 一个实例方? 其调用者是一个函数对? 表示某个对象绑定到该函CL? 参数为目标对? 在函数执行时全局的Event对象作ؓ参数传入

Analysis & Usage - 分析与?/h4>

hummer 2008-05-30 13:56 发表评论
]]>prototype源码分析(?) http://www.tkk7.com/hummer008/articles/204073.htmlhummerhummerFri, 30 May 2008 05:53:00 GMThttp://www.tkk7.com/hummer008/articles/204073.htmlhttp://www.tkk7.com/hummer008/comments/204073.htmlhttp://www.tkk7.com/hummer008/articles/204073.html#Feedback0http://www.tkk7.com/hummer008/comments/commentRss/204073.htmlhttp://www.tkk7.com/hummer008/services/trackbacks/204073.html

Object - 对Objectcȝ扩展

Prototype?span class="object">Objectc进行的扩展主要通过一个静态函?span class="method">Object.extend(destination, source)实现了JavaScript中的l承?从语义的角度, Object.extend(destination, source)Ҏ有些不和逻辑, 因ؓ它事实上仅仅实现了从源对象到目标对象的全息拷贝。不q你也可以这栯为:׃目标对象拥有了所有源对象所拥有的特? 所以看上去像目标对象l承了源对象(q加以扩?一栗另? Prototype?span class="object">Object扩展了几个比较有用的静态方? 所有其他的cd以通过调用q些静态方法获取支持?

Source View - 源码解析

Object.extend = function(destination, source) {  // 一个静态方法表C? 目标对象拥有源对象的所有属性和Ҏ
for (var property in source) {
destination[property] = source[property];   // 利用动态语a的特? 通过赋值动态添加属性与Ҏ
}
return destination;   // q回扩展后的对象
}
Object.extend(Object, {
inspect: function(object) {   // 一个静态方? 传入一个对? q回对象的字W串表示
try {
if (object == undefined) return 'undefined';  // 处理undefined情况
if (object == null) return 'null';     // 处理null情况
// 如果对象定义了inspectҎ, 则调用该Ҏq回, 否则q回对象的toString()?/span>
return object.inspect ? object.inspect() : object.toString();
} catch (e) {
if (e instanceof RangeError) return '...';  // 处理异常情况
throw e;
}
},
keys: function(object) {     // 一个静态方? 传入一个对? q回该对象中所有的属? 构成数组q回
var keys = [];
for (var property in object)
keys.push(property);     // 每个属性压入到一个数l中
return keys;
},
values: function(object) {   // 一个静态方? 传入一个对? q回该对象中所有属性所对应的? 构成数组q回
var values = [];
for (var property in object)
values.push(object[property]);   // 每个属性的值压入到一个数l中
return values;
},
clone: function(object) {    // 一个静态方? 传入一个对? 克隆一个新对象q返?/span>
return Object.extend({}, object);
}
});

Field & Function Reference - 属性方法一?/h4>

Object ( 静?) - 扩展
Method / Property Kind Arguments Description
extend(destination, source) 静态方?/td> L对象  
inspect(object) 静态方?/td> L对象  
keys(object) 静态方?/td> L对象  
values(object) 静态方?/td> L对象  
clone(object) 静态方?/td> L对象  

Analysis & Usage - 分析与?/h4>

Object.extend(destination, source)是Prototype实现的一个静态方? 在JavaScript中模拟了l承?事实? q个Ҏ的语义更加們֐于:动态地为某个对象添加属性或Ҏ。这个函数诏I了整个Prototype的框? 虽然核心思想是一致的, 但是在用中q是能够看到一些不同之? q些不同之处很微但是却值得一提?

1. 扩展现有对象的功?- Z们添加新的函?

Object.extend(Number.prototype, {
toColorPart: function() {
var digits = this.toString(16);
if (this < 16) return '0' + digits;
return digits;
},
......
});

q是非常典型的对Numbercȝ扩展, ?span class="object">Number.prototype加入了一些额外的函数, 从而每个Number对象的实例都拥有q些Ҏ?

2. 定义q实现抽象类

var Enumerable = {
each: function(iterator) {
var index = 0;
try {
this._each(function(value) {
try {
iterator(value, index++);
} catch (e) {
if (e != $continue) throw e;
}
});
} catch (e) {
if (e != $break) throw e;
}
},
......
}
Object.extend(Array.prototype, Enumerable);
Object.extend(Array.prototype, {
_each: function(iterator) {
for (var i = 0; i < this.length; i++)
iterator(this[i]);
},
......
}

q是Prototype中的代码D? 它向我们展示了定义ƈ实现抽象cȝ完整q程。这里首先定义了一?span class="object">Enumerablec? q是一个抽象类。这个抽象类的内? 有一?span class="method">_eachҎ被调用来完成一些逻辑。但是在整个Enumerable的范围内, 你无法找?span class="method">_eachҎ的具体实现。也是? Enumerable是一个抽象类, h一?span class="method">_each的抽象函数?所? 如果你还没有实现_eachq个抽象函数, 你是无法直接使用Enumerablecȝ。之后的代码比较明朗了, 使用Object.extendҎ, 所?span class="object">Enumerable的函数复制给Array.prototype, 然后?span class="object">Array.prototype中实?span class="method">_eachҎ, q样׃?span class="object">Array对象具备?span class="object">Enumerableq个抽象cȝ所有特性?



hummer 2008-05-30 13:53 发表评论
]]>prototype源码分析(?)http://www.tkk7.com/hummer008/articles/204072.htmlhummerhummerFri, 30 May 2008 05:52:00 GMThttp://www.tkk7.com/hummer008/articles/204072.htmlhttp://www.tkk7.com/hummer008/comments/204072.htmlhttp://www.tkk7.com/hummer008/articles/204072.html#Feedback0http://www.tkk7.com/hummer008/comments/commentRss/204072.htmlhttp://www.tkk7.com/hummer008/services/trackbacks/204072.html

Class - cd?/h3>

ClasscdC在JavaScript中声明一个新的类, q过构造函数实例化q个cȝ机制。通过使用Class.create()Ҏ, 你实际上声明了一个新的类, q定义了一?span class="method">initialize()Ҏ作ؓ构造函? 一旦你在这个声明的cȝprototype中实C改该Ҏ, 你就可以使用new操作W来创徏q实例化一个类?

Knowledge Prepare - 知识准备

在JavaScript? 当你定义了一个新的函? 你实际上声明了一个新的类, 而这个函数本w就相当于类的构造函数?下面的代码向你展CZ两种不同的方式来创徏一个新?span class="object">Personc? ?span class="method">Person.prototype的定义也紧跟在函数定义之后?

var Person = function(name) {  // 一个匿名函? q将q个函数赋值给一?span class="object">Person变量, 此时Person成ؓ一个类
this.name = name;
}
function Person(name) {  // 直接定义一个叫?span class="object">Person的函数表C?span class="object">Personc?/span>
this.name = name;
}
Person.prototype = {  // 定义Person的prototype?/span>
printName: function() {   // 定义一个print函数
alert(this.name);
}
}

当你通过函数的方式声明了一个类之后, 你就可以通过new操作W来实例化这个类。这? 你就可以调用cȝ成员函数来完成你的逻辑?

var person = new Person("Joe Smith"); // 使用new操作W来新徏一?span class="Object">Person的实? q赋l变?span class="method">person
person.printName(); // person可以看作是一个实例的引用(reference), 所以可以通过q个引用来调?span class="Object">PersoncM的成员函?/span>

我们来ȝ一下创Z个新的类的实例的整个程和步骤:

1. 通过定义一个函数的方式(匿名或者实?来声明一个新的类.
2. 如果有必? 定义q个新的cȝprototype?
3. 使用new操作W紧跟你所定义的函数来创徏一个新的类的实? 一旦JavaScript~译器碰Cnew操作W? 它实际上创徏了一个空的类实例变量.
4. 所有这个类的prototype域中的属性与Ҏ复制到这个新的实例中, q将其成员函C所有的this指针指向q个新创建的实例.
5. 接下? 执行紧跟?span class="method">new操作W后面的那个函数.
6. 当你执行q个函数? 如果你试囑֯一个不存在的属性进行赋? JavaScript~译器将自动Z在这个实例范围内新创个属?
7. 函数执行完毕? 这个初始化完成的实例返?

在Prototype? 使用Class对象, 你可以以一个比较简单的方式来声明一个新的对象。通过使用Class.create(), prototypeZ创徏了一个默认的构造函数initialize(), 一旦你实现q一函数, 可以以一个类似Java中构造函数的方式来创Z个新的类的实例?

Source View - 源码解析

var Class = {  // 全局静态类, 用于声明一个新的类q提供构造函数支?/span>
create: function() {
return function() { // q回一个函? 代表着q个新声明的cȝ构造函?/span>
// 一个命名ؓinitialize的函数将被这个类实现作ؓcȝ构造函?/span>
this.initialize.apply(this, arguments);// initialize函数在你实例化一个变量的时候被调用执行(即上?个步骤中的第5?
}
}
}

Field & Function Reference - 属性方法一?/h4>

Class ( 静?)
Method / Property Kind Arguments Description
create() 静态方?/td>  / 用于声明一个新的类q提供了一个名为initialize构造函数支?/td>

Analysis & Usage - 分析与?/h4>

通过Classc? 你可以很ҎC用构造函数的方式创徏一个新的类, q对于JavaE序员来说或许更加容易被接受。下面我们列ZJava和JavaScript各自声明和创Z个新的类的代码对? 我们可以看到, 他们是如此相|

var Person = Class.create(); // cȝ声明              |public class Person { // cȝ声明
Person.prototype = {                                 |    private String name;
initialize: function(name) {  // 构造函?/span>          |    public Person(String name){ // 构造函?/span>
this.name = name;                               |       this.name = name;
}                                                 |    }
printName: function() {  // 成员函数               |    public void printName(){  // 成员函数
alert(this.name);                               |       System.out.println(name);
}                                                 |    }
}                                                    |}
var person = new Person("Joe Smith");// 创徏实例      |Person person = new Person("Joe Smith");// 创徏实例
person.printName(); // 函数调用                       |person.printName(); // 函数调用


hummer 2008-05-30 13:52 发表评论
]]>prototype源码分析(?)http://www.tkk7.com/hummer008/articles/204071.htmlhummerhummerFri, 30 May 2008 05:51:00 GMThttp://www.tkk7.com/hummer008/articles/204071.htmlhttp://www.tkk7.com/hummer008/comments/204071.htmlhttp://www.tkk7.com/hummer008/articles/204071.html#Feedback0http://www.tkk7.com/hummer008/comments/commentRss/204071.htmlhttp://www.tkk7.com/hummer008/services/trackbacks/204071.html

Basic - 基础代码

在开始所有的Prototype学习之旅之前Q我们首先对Prototype中最常用的一些公用函数做一些研IӞq些JavaScript函数诏I整?span class="Object">Prototype框架Q熟l用它们不仅有助于我们学习Prototype框架Q同时也为我们自q写JavaScript应用提供了独特的思\?

Source View - 源码解析

var Prototype = {     // 定义一?span class="Object">Prototype对象Q封装了一些简单的静态函数和变量
Version: '1.5.0_rc1',     // 定义当前Prototype框架的版本号
ScriptFragment: '(?:
)((\n|\r|.)*?)(?:<\/script>)',   // q回一个正则表辑ּQ判断是否ؓScript?/span>
emptyFunction: function() {},    // q回一个空的函? 该函C么都不做, 在编写一些复杂流E中会用此功能
K: function(x) {return x}     // q回参数本n, 在编写一些复杂流E中会用此功能
}
/*****************************************************************************************************************/
function $() {   // 一个根据idd对象的快捷函? Ҏidq回所对应的对?/span>
var results = [], element;
for (var i = 0; i < arguments.length; i++) { // 可以接受多个参数, q回由每个id所对应的对象构成的数组
element = arguments[i];
if (typeof element == 'string')    // 可以接受非string参数Q直接返回对象本w?/span>
element = document.getElementById(element);  // 通过DOM操作d对象
results.push(Element.extend(element)); // 参见Element.extend的解? 回的对象d了一些额外特性,q压入到l果数组?/span>
}
return results.reduce(); // Ҏ参数的个数返回对象或者对象数l?/span>
}
/*****************************************************************************************************************/
var Abstract = new Object();  // 定义了一个全局的对? 可以理解Z个命名空? 成ؓ一个抽象基c?/span>
/*****************************************************************************************************************/
var Try = {
these: function() {  // 一个静态的函数, 接受多个函数作ؓ参数, 依次执行q返回第一个成功执行的函数的结?/span>
var returnValue;
for (var i = 0; i < arguments.length; i++) {
var lambda = arguments[i];   // 每个参数都是一个将被执行的函数
try {
returnValue = lambda();    // 执行函数q记录返回?/span>
break;                     // W一ơ成功执行后, 即退?/span>
} catch (e) {}   // 捕获每个函数执行q程中的异常
}
return returnValue;
}
}
/*****************************************************************************************************************/
var PeriodicalExecuter = Class.create();  // 定义一个定时器, 重复执行某一函数
PeriodicalExecuter.prototype = {
initialize: function(callback, frequency) {  // 构造函? 定义需要重复执行的函数和执行周?/span>
this.callback = callback;      // 需要重复执行的函数
this.frequency = frequency;    // 重复执行的周? 单位是秒
this.currentlyExecuting = false;   // 是否当前执行标志
this.registerCallback();    // 调用重复执行的函?/span>
},
registerCallback: function() { // 调用window.setInterval函数完成重复执行, q回一个标志留作被stop函数调用, 以停止定时器
// W一个参数y妙地使用?span class="method">bindҎ, ?span class="Object">this作ؓbind的参? q样onTimeEventҎ内部可以通过this获取当前对象的引?/span>
this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
},
stop: function() {
if (!this.timer) return;   // 通过setInterval函数的返回值来判断是否当前执行
clearInterval(this.timer);   // 当前执行? 调用clearIntervalҎ来中止当前的执行
this.timer = null;    // 当前执行的标志讄?span class="Object">null
},
onTimerEvent: function() {
if (!this.currentlyExecuting) {  // 判断是否当前执行, 否则跌, 相当于一个同步信号量
try {
this.currentlyExecuting = true;  // 讄是否当前执行的标?/span>
this.callback(this);   // 调用callback完成真正的函数执?/span>
} finally {
this.currentlyExecuting = false;  // 无论是否成功执行, 最l设|当前执行标志ؓfalse
}
}
}
}

Field & Function Reference - 属性方法一?/h4>

Prototype ( 静?)
Method / Property Kind Arguments Description
Version 静态属?/td>  / 表示Prototype框架的版本号
ScriptFragment 静态属?/td>  / 表示script代码D늚正则表达?/td>
emptyFunction 静态方?/td>  / q回一个什么都不做的空函数
K 静态方?/td> L对象 q回传入的参?/td>
$() ( 静?)
Method / Property Kind Arguments Description
$() 静态方?/td> string或者Q意对? 参数数量不限 Ҏ参数的类型和数量q回DOM操作的结? 传入的参CؓstringӞq回document.getElementById所d的对? 传入的参Cؓ非stringcd的对象时, 直接q回参数本n; 当参C数是1个时, 直接q回上述操作所得的l果; 当参C数大?个时, 每个参数操作的l果l成一个对象数l返?/td>
Abstract ( 静?)
Method / Property Kind Arguments Description
Abstract c?/td>  / 一个全局的对? 可以理解Z个命名空? 成ؓ一个抽象基c?/td>
Try ( 静?)
Method / Property Kind Arguments Description
these 静态方?/td> 多个有待依次执行的函?/td> q回W一个成功执行的函数的执行结?/td>
PeriodicalExecuter ( 实例 )
Method / Property Kind Arguments Description
callback 属?/td>  / 构造函数实例化时初始化的属? 定义定时器重复执行时真正需要调用的函数
frequency 属?Number)  / 构造函数实例化时初始化的属? 定义定时器重复执行的旉间隔
currentlyExecuting 属?Boolean)  / 在整个定时器执行q程中表C是否当前执行的标志, 可以作ؓ执行q程中的同步信号?/td>
timer 属?/td>  / 一个内部变? 在registerCallbackҎ调用window.setInterval时记录该函数的返回? 用以lstopҎ使用
registerCallback Ҏ  / 在构造函数初始化完成时调用执? 也可以直接调用该函数启动定时? 在函数内部通过调用window.setIntervalҎ完成定时器功?/td>
stop Ҏ this.timer, 即重复执行时调用setInterval的返回?/td> 用于暂停定时? 通过实例变量调用完成
onTimerEvent Ҏ  / 一个内部方? 真正调用callback完成函数执行

Analysis & Usage - 分析与?/h4>

Extension - 扩展

$()为我们提供了一个快L方式Q通过id来读取一个对象,从而避免了我们在编写JavaScript代码时的重复而又冗长的代码段。然而,在某些情况下Q我们需要通过对象的name属性来d对象Qƈ对对象进行一定的操作Q此Ӟ$()Ҏ显得有些力不从心了。因而在q里Q我针对q种需求,q模?()ҎQ实C一个通过name来读取对象的函数?


事实上,getElementsByName()已经帮我们解决了大多数的问题。由于name在HTML的document中可以不唯一Q因而我们需要对此进行简单的处理Q当name唯一Ӟ直接q回单一对象Q当name不唯一Ӟq回一个数l?q已l与getElementsByName()本n的含义不同了Q当然这仅仅是ؓ了方便编E而已)?



hummer 2008-05-30 13:51 发表评论
]]>
prototype源码分析q接http://www.tkk7.com/hummer008/articles/204070.htmlhummerhummerFri, 30 May 2008 05:48:00 GMThttp://www.tkk7.com/hummer008/articles/204070.htmlhttp://www.tkk7.com/hummer008/comments/204070.htmlhttp://www.tkk7.com/hummer008/articles/204070.html#Feedback0http://www.tkk7.com/hummer008/comments/commentRss/204070.htmlhttp://www.tkk7.com/hummer008/services/trackbacks/204070.html
http://p-x1984.javaeye.com/blog/196906

hummer 2008-05-30 13:48 发表评论
]]>
prototype.js开发笔??http://www.tkk7.com/hummer008/articles/203722.htmlhummerhummerThu, 29 May 2008 01:55:00 GMThttp://www.tkk7.com/hummer008/articles/203722.htmlhttp://www.tkk7.com/hummer008/comments/203722.htmlhttp://www.tkk7.com/hummer008/articles/203722.html#Feedback0http://www.tkk7.com/hummer008/comments/commentRss/203722.htmlhttp://www.tkk7.com/hummer008/services/trackbacks/203722.html阅读全文

hummer 2008-05-29 09:55 发表评论
]]>
prototype.js的学习资??http://www.tkk7.com/hummer008/articles/203716.htmlhummerhummerThu, 29 May 2008 01:48:00 GMThttp://www.tkk7.com/hummer008/articles/203716.htmlhttp://www.tkk7.com/hummer008/comments/203716.htmlhttp://www.tkk7.com/hummer008/articles/203716.html#Feedback0http://www.tkk7.com/hummer008/comments/commentRss/203716.htmlhttp://www.tkk7.com/hummer008/services/trackbacks/203716.html1.prototype.js的官方网?br /> http://www.prototypejs.org/

2.prototype.js 官方api文档
http://prototypejs.org/api/

3.开始学习prototype.js
http://www.prototypejs.org/learn

4.prototype.js sample
http://prototype-window.xilinus.com/samples.html

5.prototype.js讨论?br /> http://groups.google.com/group/Prototypejs/topics

6.precipitant的学习笔讎ͼ貌似看到一Qƈ没有扑ֈ二?^_^
http://blog.csdn.net/precipitant/archive/2007/10/13/1823220.aspx

7.phphot写的prototype.js教程
http://blog.csdn.net/phphot/archive/2007/10/10/1818938.aspx

8.一个ajax的讨?br /> http://bbs.okajax.com/forum-13-1.html

9.prototype.js开发应用手?br /> http://bbs.blueidea.com/thread-2812502-1-1.html

10.一个ajax框架介绍的网?br /> http://ajaxian.com/resources/

11.以下几个|站也是学习prototype.js的不错的|站,含有prototype.js中文教程
https://compdoc2cn.dev.java.net/prototype/html/prototype.js.cn.html
http://www.lvjiyong.com/item/jquery-with-prototype-using-ajax
http://591wap.cn/works/prototype/using_ajax_request.html

12.JAVA.NET上的prototype.js学习W记
https://compdoc2cn.dev.java.net/prototype/html/prototype.js.cn.html

13.prototype.js源文件以及中英文手册下蝲,L步这?br /> prototype.js教程及prototype中文手册



hummer 2008-05-29 09:48 发表评论
]]>
PrototypeQ?.5 rc2)使用指南Q五Q??http://www.tkk7.com/hummer008/articles/203713.htmlhummerhummerThu, 29 May 2008 01:42:00 GMThttp://www.tkk7.com/hummer008/articles/203713.htmlhttp://www.tkk7.com/hummer008/comments/203713.htmlhttp://www.tkk7.com/hummer008/articles/203713.html#Feedback0http://www.tkk7.com/hummer008/comments/commentRss/203713.htmlhttp://www.tkk7.com/hummer008/services/trackbacks/203713.html

(十三).Prototype1.5 rc2Q?/span>1.5 rc2)版指南最后一之Position

Position?/span>prototype中定义的一个对象,提供了操?/span>DOM中与位置相关的方法,要很好的理解元素在页面中的位|,可以参考这文章:Relatively Absolute

具体代码如下Q按照代码说_其中英文是作者的注释Q中文红色的才是偶的说明或翻译英文的注释Q采用顶式注释法(注释在要说明的代码的上面)说明

  // set to true if needed, warning: firefox performance problems
  // NOT neeeded for page scrolling, only if draggable contained in
  // scrollable elements
  //
只有在用拖动的时候元素包含在有滚动条的元素中才需要设|ؓ
true
  includeScrollOffsets: false,

  // must be called before calling withinIncludingScrolloffset, every time the
  // page is scrolled
  //
当页面被scrolled后,使用withinIncludingScrolloffset的时候需要先调用q个Ҏ

  prepare: function() {
    //
横向滚动条滚动的距离
    this.deltaX =  window.pageXOffset
                || document.documentElement.scrollLeft
                || document.body.scrollLeft
                || 0;
    //
U向滚动条滚动的距离
    this.deltaY =  window.pageYOffset
                || document.documentElement.scrollTop
                || document.body.scrollTop
                || 0;
  },

//元素׃滚动条偏Uȝ总距?/span> 
realOffset: function(element) {
    var valueT = 0, valueL = 0;
    do {
      valueT += element.scrollTop  || 0;
      valueL += element.scrollLeft || 0;
      element = element.parentNode;
    } while (element);
    return [valueL, valueT];
  },

//元素在页面中?/span>offsetParent累积?/span>offsetQ当offsetParent都没有滚动条Ӟ是元素在页面中的位|?/span>
cumulativeOffset: function(element) {
    var valueT = 0, valueL = 0;
    do {
      valueT += element.offsetTop  || 0;
      valueL += element.offsetLeft || 0;
      element = element.offsetParent;
    } while (element);
    return [valueL, valueT];
  },

//元素相对?/span>containing block("nearest positioned ancestor")的位|,也就是相对于最q的一?/span>position讄?/span>relative或?/span>absolute的祖先节点的位置Q如果没有就是相对于 body的位|,?/span>style.topQ?/span>style.left一P
positionedOffset: function(element) {
    var valueT = 0, valueL = 0;
    do {
      valueT += element.offsetTop  || 0;
      valueL += element.offsetLeft || 0;
      element = element.offsetParent;
      if (element) {
        if(element.tagName==’BODY’) break;
        var p = Element.getStyle(element, 'position’);
        if (p == 'relative’ || p == 'absolute’) break;
      }
    } while (element);
    return [valueL, valueT];
  },
  
  //offsetParent
  offsetParent: function(element) {
    if (element.offsetParent) return element.offsetParent;
    if (element == document.body) return element;

    while ((element = element.parentNode) && element != document.body)
      if (Element.getStyle(element, 'position’) != ’static’)
        return element;

    return document.body;
  },
 
  // caches x/y coordinate pair to use with overlap
  //
判断指定的位|是否在元素?/span>

  within: function(element, x, y) {
    if (this.includeScrollOffsets)
      return this.withinIncludingScrolloffsets(element, x, y);
    this.xcomp = x;
    this.ycomp = y;
    this.offset = this.cumulativeOffset(element);

    return (y >= this.offset[1] &&
            y <  this.offset[1] + element.offsetHeight &&
            x >= this.offset[0] &&
            x <  this.offset[0] + element.offsetWidth);
  },

//?/span>within差不多,不过考虑到滚动条Q也许是在元素上面,但不是直接在上面Q因为滚动条也许已经使元素不可见?/span> 
withinIncludingScrolloffsets: function(element, x, y) {
    var offsetcache = this.realOffset(element);

    this.xcomp = x + offsetcache[0] - this.deltaX;
    this.ycomp = y + offsetcache[1] - this.deltaY;
    this.offset = this.cumulativeOffset(element);

    return (this.ycomp >= this.offset[1] &&
            this.ycomp <  this.offset[1] + element.offsetHeight &&
            this.xcomp >= this.offset[0] &&
            this.xcomp <  this.offset[0] + element.offsetWidth);
  },

  // within must be called directly before
  //
在调用这个方法前Q必d调用withinQ返回在with指定的位|在水^或者垂直方向上占用的百分比

  overlap: function(mode, element) { 
    if (!mode) return 0; 
    if (mode == 'vertical’)
      return ((this.offset[1] + element.offsetHeight) - this.ycomp) /
        element.offsetHeight;
    if (mode == 'horizontal’)
      return ((this.offset[0] + element.offsetWidth) - this.xcomp) /
        element.offsetWidth;
  },

//q回元素相对面的真实位|?/span> 
page: function(forElement) {
    var valueT = 0, valueL = 0;

    var element = forElement;
    do {
      valueT += element.offsetTop  || 0;
      valueL += element.offsetLeft || 0;

      // Safari fix
      if (element.offsetParent==document.body)
        if (Element.getStyle(element,’position’)==’absolute’) break;

    } while (element = element.offsetParent);

    element = forElement;
    do {
      if (!window.opera || element.tagName==’BODY’) {
        valueT -= element.scrollTop  || 0;
        valueL -= element.scrollLeft || 0;
      }
    } while (element = element.parentNode);

    return [valueL, valueT];
  },

//target?/span>source的位|,大小 
clone: function(source, target) {
    var options = Object.extend({
      setLeft:    true,
      setTop:     true,
      setWidth:   true,
      setHeight:  true,
      offsetTop:  0,
      offsetLeft: 0
    }, arguments[2] || {})

    // find page position of source
    source = $(source);
    var p = Position.page(source);

    // find coordinate system to use
    target = $(target);
    var delta = [0, 0];
    var parent = null;
    // delta [0,0] will do fine with position: fixed elements,
    // position:absolute needs offsetParent deltas
    if (Element.getStyle(target,’position’) == 'absolute’) {
      parent = Position.offsetParent(target);
      delta = Position.page(parent);
    }

    // correct by body offsets (fixes Safari)
    if (parent == document.body) {
      delta[0] -= document.body.offsetLeft;
      delta[1] -= document.body.offsetTop;
    }

    // set position
    if(options.setLeft)   target.style.left  = (p[0] - delta[0] + options.offsetLeft) + 'px’;
    if(options.setTop)    target.style.top   = (p[1] - delta[1] + options.offsetTop) + 'px’;
    if(options.setWidth)  target.style.width = source.offsetWidth + 'px’;
    if(options.setHeight) target.style.height = source.offsetHeight + 'px’;
  },

//?/span>element?/span>position讄?/span>absolute的模?/span> 
absolutize: function(element) {
    element = $(element);
    if (element.style.position == 'absolute’) return;
    Position.prepare();

    var offsets = Position.positionedOffset(element);
    var top     = offsets[1];
    var left    = offsets[0];
    var width   = element.clientWidth;
    var height  = element.clientHeight;

    element._originalLeft   = left - parseFloat(element.style.left  || 0);
    element._originalTop    = top  - parseFloat(element.style.top || 0);
    element._originalWidth  = element.style.width;
    element._originalHeight = element.style.height;

    element.style.position = 'absolute’;
    element.style.top    = top + 'px’;;
    element.style.left   = left + 'px’;;
    element.style.width  = width + 'px’;;
    element.style.height = height + 'px’;;
  },

//?/span>element?/span>position讄?/span>absolute的模?/span> 
relativize: function(element) {
    element = $(element);
    if (element.style.position == 'relative’) return;
    Position.prepare();

    element.style.position = 'relative’;
    var top  = parseFloat(element.style.top  || 0) - (element._originalTop || 0);
    var left = parseFloat(element.style.left || 0) - (element._originalLeft || 0);

    element.style.top    = top + 'px’;
    element.style.left   = left + 'px’;
    element.style.height = element._originalHeight;
    element.style.width  = element._originalWidth;
  }
}

// Safari returns margins on body which is incorrect if the child is absolutely
// positioned.  For performance reasons, redefine Position.cumulativeOffset for
// KHTML/WebKit only.
if (/Konqueror|Safari|KHTML/.test(navigator.userAgent)) {
  Position.cumulativeOffset = function(element) {
    var valueT = 0, valueL = 0;
    do {
      valueT += element.offsetTop  || 0;
      valueL += element.offsetLeft || 0;
      if (element.offsetParent == document.body)
        if (Element.getStyle(element, 'position’) == 'absolute’) break;
       
      element = element.offsetParent;
    } while (element);
   
    return [valueL, valueT];
  }
}

l于?/span>Prototype的所有部分都写完了,哈哈Q越来越佩服自己的耐力?/span>

下一步决定写?/span>Scriptaculousq个񔋹行的效果库

Prototype1.5的下载ؓ什么不单点

q几天在论坛和博客上看到很多人问Prototype1.5怎么下蝲Qؓ什么下载这么困隑֑Q?/span>
Prototype的官方网站是Q?/span>http://prototype.conio.net/Q如果你一下子找不刎ͼ?/span>google上搜?/span>Prototype找C
下蝲当然要到官方|站下蝲了,但是问题?/span>Prototype官方|站更新太慢Q首上的下载连接还?/span>prototype1.4的,而且只是一个单独的js文g
那么怎么下蝲最新版本的呢?
1Q如果你只想得到一个单独的js文g使用的话Q其实官方网站提供了最新版1.5的下?/span>, 下面q接是下蝲地址?/span>http://dev.rubyonrails.org/browser/trunk/railties/html/javascripts/prototype.js?format=raw
2Q如果你惛_到详l的源文件以及测试代码,需要通过svn下蝲Q用下面的命o可以了Q?/span>svn co http://dev.rubyonrails.org/svn/rails/spinoffs/prototype
如果你没?/span>svn的话Q我已经下蝲打包了传?/span>51js论坛中,查看下面q接的帖子中有下载的Q?/span>http://bbs.51js.com/viewthread.php?tid=65070&highlight=prototype

惛_很多prototype爱好者都一直在{待着prototype1.5的发布,虽然{待的时间很长,但是q一令hȀ动的一天终于到来了
因ؓ|友提醒Q今天访?/span>prototype|站Q发现原来的|址已经自动跌{到新的网站去了,作者用了一个独立的域名http://www.prototypejs.org/
刚才?/span>google里搜?/span>prototypeQ发现搜索结果中出现的已l是新网站了Q?/span>google爬虫也蛮勤快的嘛
更让人高兴的是,一向被病的文档问题q一版有了非常大的提高,可以说是有了质的飞跃Q以前的版本基本上没有文档,要用只有自q解了Q?/font>
现在在它的官方网站上有专门的API参考以及一些用的例子Q看来作者真正的关于q个问题来了Q?/span>prototype爱好者应该高兴一把了哈哈Q赶快到prototype官方|站下蝲?/span>Engoy?/span>如果感觉阅读英文是一U折的话,可以参考我以前写的prototype 1.5使用指南pd文章.

Q-声明Q该使用指南文章{载,׃是来源于多次转蝲Q未扑ֈ原出处。再ơ感谢作者与译人员?/font>



hummer 2008-05-29 09:42 发表评论
]]>
PrototypeQ?.5 rc2)使用指南Q四Q??http://www.tkk7.com/hummer008/articles/203711.htmlhummerhummerThu, 29 May 2008 01:41:00 GMThttp://www.tkk7.com/hummer008/articles/203711.htmlhttp://www.tkk7.com/hummer008/comments/203711.htmlhttp://www.tkk7.com/hummer008/articles/203711.html#Feedback0http://www.tkk7.com/hummer008/comments/commentRss/203711.htmlhttp://www.tkk7.com/hummer008/services/trackbacks/203711.html

(十一).PrototypeQ?/span>1.5 rc2)使用指南?/span>form

q一部分提供了很多与表单操作有关的功能,包括以下部分Q当通过$Ҏq回元素Ӟ可以直接通过$(element).method()调用Q?/span>

Form对象Q提供了操作整个表单的一些方?/span>
Form.Element对象Q提供了操作某个表单元素的方?/font>
TimedObserverc:周期性表单监视器Q当表单元素值改变的时候执行一个回调函敎ͼ?/span>Form?/span>Element两种cd
EventObserverc:利用事g来监视表单元素,当表单元素值改变的时候执行一个回调函敎ͼ?/span>Form?/span>Element两种cd

Form对象Q?/span>

reset(form)Q?/span>form.reset()
serializeElements(elements)
Q将elements中的元素序列化,是q回指定的所有元素的queryString的Ş式,便于?/span>xmlhttp或其他地方?/span>

serialize(form)Q序列化整个表单
getElements(form)Q返回表单的所有可序列化元?/font>
getInputs(form, typeName, name)Q返回所有符?/span>typeName?/span>name?/span>input元素
disable(form)Q整个表单处于不可用状?/font>
enable(form) Q是整个表单可用
findFirstElement(form)Q返回类型ؓ'input’, ’select’, 'textarea’的第一个可用的非隐藏元?/span>
focusFirstElement(form)QfindFirstElement(form)q回的元素得到焦?/span>

Form.Element对象Q?/span>

focus(element) select(element)Q?/span>html元素内置Ҏ的封装,除了q回元素本n
serialize(element)Q序列化指定的表单元素,q回key=value的Ş式,q回?/span>string已经encodeURIComponent?/span>
getValue(element)Q返回元素的?/font>
clear(element)Q清除元素的?/font>
present(element)Q判断元素的值是否非I?/font>
activate(element)Q元素获得焦点
disable(element)Q元素不可?/font>
enable(element)Q是元素可用

$F = Form.Element.getValue 方便使用

Form.Element.Observer以及Form.Observerc:

周期性监视表单元素,如果表单或表单元素的值有改变Ӟ执行执行一个回调函敎ͼ使用方式如下Q?/font>

var oser=new Form.Element.Observer(element, frequency, callback)
or oser=new Form.Observer(form, frequency, callback)
callback
可以定义两个参数 form/element?/span>Form.serialize()/value

Form.Element.EventObserver?/span>Form.EventObserverc:

q两个类跟上面的差不多,只是不是周期性的监视Q而是利用元素?/span>change?/span>click事g来监视表单元素的变化Q当发生变化时执?/span>callbackQ参数跟上面一?/span>

 

(十二).PrototypeQ?/span>1.5 rc2)使用指南?/span>event.js

在介l?/span>Prototype?/span>Event对象前先介绍一下浏览器中的事g模型Q浏览器中的事g主要?/span>HTML事g、鼠标事件和键盘事gQ前两种事g比较好理解,q里主要解释一下键盘事件以及它?/span>IE?/span>firefox中的区别

键盘事g包括keydown?/span>kepress?/span>keyup三种Q每ơ敲击键盘都?/span>(依次Q?/span>)触发q三U事Ӟ其中keydown?/span>keyup是比较低U的接近于硬件的事gQ通俗的理解是q两个事件可以捕获到你敲M键盘中某个键Q?/span>keypress是相对于字符层面的较为高U点的事Ӟq个事g能够捕捉C键入了哪个字W。可以这L解,如果你敲MA键,keydown?/span>keyup事g只是知道你敲MA键,它ƈ不知道你敲的是大写的A(你同时按下了Shift?/span>)q是敲的是小?/span>aQ它是以"?/span>"为单位,你敲入了大写?/span>AQ它只是当成你敲下了shift?/span>A两个键而已Q但?/span>keypress可以捕捉C是敲入的大写?/span>Aq是写?/span>a

q要理解一个概忉|键盘中的键分为字W?/span>(可打?/span>)键和功能?/span>(不可打印)Q功能键包括Backspace, Enter, Escape, the arrow keys, Page Up, Page Down, and F1 through F12 {?/span>

下面说一下键盘事件的具体使用ҎQ?/font>

键盘事g?/span>event对象中包含一?/span>keyCode属性,IE中只有这一个属性,当ؓkeydown?/span>keyup 事g是,keycode属性表CZ具体按下的键(也称?/span>virtual keycode)Q当捕捉的是keypress事g?/span>keyCode属性指的是你键入的字符(character code)

?/span>firefox中情冉|些不同,event对象包含一?/span>keyCode属性和一?/span>charCode属性,keydown?/span>keyup事g的时候,keyCode表示的就是你具体按的键,charCode?/span>0Q当捕捉的是keypress事gӞkeyCode?/span>0Q?/span>charCode指的是你按下的字W?/span>

当捕捉的?/span>keypress事gӞ当你按的是可打印字符ӞkeyCode?/span>0Q?/span>charCode指的是你按下的字W的键|当你按的是不可打印字W时Q?/span>keyCode为按下的键的键|charCode?/span>0

注意Q功能键不会触发keypress事gQ因?/span>keypress对应的就是可打印的字W,但是有一?/span>IE?/span>FF 中的区别Q你按下一个字W键的同时按?/span>alt键,?/span>IE中不触发keypress事gQ但是在ff中可触发Q我发现?/span>IE中按?/span>ctrl键的时候只有按?/span> q键会触发事g其他的要么不会触发事Ӟ要么被浏览器IE自n捕获了,例如你按?/span>ctrl_AQ全选某个东西,你按ctrl_S保存文gQ但是在FF中就好多了,事g都是先传递到|页Q再向外传?/span>

鉴于IE?/span>FF中的区别Q如果你比较懒的话,只?/span>keydow?/span>keyup事gQ这两种事g的用在IE?/span>FF中基本上没有区别Q也不要捕获ctrl_A{被览器定义ؓ快捷键的事g

键盘事gevent对象q有三个其他的属?/span>altKey, ctrlKey, and shiftKey 来判断你按下一个键的时候是否按下了alt{键Q这三个属性用比较简单,三种事g都可以用,也不存在ie?/span>ff的兼Ҏ问?/span>

?/span>Prototype中的Event中又如下属性:

  KEY_BACKSPACE: 8,
  KEY_TAB:       9,
  KEY_RETURN:   13,
  KEY_ESC:      27,
  KEY_LEFT:     37,
  KEY_UP:       38,
  KEY_RIGHT:    39,
  KEY_DOWN:     40,
  KEY_DELETE:   46,
  KEY_HOME:     36,
  KEY_END:      35,
  KEY_PAGEUP:   33,
  KEY_PAGEDOWN: 34,

以及下面的方法:

element(event) Q返回触发事件的元素
isLeftClick(event) Q判断是否按下的左键
pointerX(event) Q事件触发时鼠标的横坐标Q对于非鼠标事gQ在ff中没有此信息Q但?/span>ie中有Q?/span>
pointerY(event)Q事件触发时鼠标所在位|的U坐?/font>
stop(event)Q阻止事件向上传播和览器的默认处理Ҏ
findElement(event, tagName) Q找到触发事件的元素的所有祖先元素中?/span>tagName?/span>tagName的一个元?/span>
observe(element, name, observer, useCapture)Q注册事件处理函?/font>
stopObserving(element, name, observer, useCapture)Q撤销注册的事?/font>



hummer 2008-05-29 09:41 发表评论
]]>
PrototypeQ?.5 rc2)使用指南Q三Q??http://www.tkk7.com/hummer008/articles/203710.htmlhummerhummerThu, 29 May 2008 01:40:00 GMThttp://www.tkk7.com/hummer008/articles/203710.htmlhttp://www.tkk7.com/hummer008/comments/203710.htmlhttp://www.tkk7.com/hummer008/articles/203710.html#Feedback0http://www.tkk7.com/hummer008/comments/commentRss/203710.htmlhttp://www.tkk7.com/hummer008/services/trackbacks/203710.html

(?/span>).PrototypeQ?/span>1.5 rc2)使用指南?/span>dom.js

q部分提供了很多(写的都有点烦?/span>)方便的操?/span>dom的方法:包含有名?/span>$Ҏ?/span>document.getElementsByClassNameҎQ以?/span>Element对象?/span>Insertion对象

以下部分一个一个的详细介绍Q?/font>

$(element)Q?/span>getElementById的封装,element可以是一个元素的id或元素本w,也可以是一个数l,q时q回一个数l,使用$ҎQ会自动调用Element.extend(element)ҎQ这L话元素可以直接调用 Element中的Ҏ, 例如Element.hide(element)可以写成q样$(element).hide()

document.getElementsByClassName(className, parentElement): Ҏclass选择元素

Element.extend(element): 扩展elementQelement可以直接调用Element?/span>Form.Element?/span>Form中定义的Ҏ

Element对象的方法:

visible: function(element)Q判?/span>element是否可见, 参数element可以是元素本w或元素id(下面的方面的参数基本上都是这L)

toggle: function(element)Q反?/span>element的可见?/span>

hide: function(element)Q隐藏元?/span>

show: function(element)Q显C元?/span>

remove: function(element)Q移除元?/span>

update: function(element, html) Q?/span>html更新element的内容,html中的script会执?/span>(下同)

replace: function(element, html)Q将element替换?/span>html

inspect: function(element)Q?/span>element的字W串表示

recursivelyCollect: function(element, property): 递归攉, 例如Element.recursivelyCollect(element, "parentNode")q回element所有的先节点, 注意只返?/span>nodeType == 1的元素,也就是不q回文本元素

ancestors: function(element): {同于上面的例子Q返回元素的所有祖先节?/span>

descendants: function(element): q回所有子孙节?/span>

immediateDescendants: function(element)Q返回元素的直接的子孙节?/span>(子节?/span>)的数l?/span>

previousSiblings: function(element)Q返回元素前面的兄弟节点

nextSiblings: function(element)Q返回位于元素后面的兄弟节点

siblings: function(element)Q返回元素所有的兄弟节点

match: function(element, selector)Q?/span>Selector?/span>matchҎ匚w元素(Selector在后面介绍), selector参数是一?/span>css selector表达式或?/span>Prototype中的一?/span>Selector实例Q如?/span>element匚wselector则返?/span>trueQ否则返?/span> falseQ例如对于一?/span>className?/span>logcss?/span>div来说Q下面的表达式返?/span>true, $(element).match("div.logcss") 待箋。?/span>

up(element, expression, index)Q利?/span>Selector.findElementҎ扑ֈelement元素的祖先节点中W合表达?/span>expression的所有元素组成的数组索引?/span>index的元素,也可以忽?/span>expression(默认?/span>*Q表C匹配所有元?/span>)?/span>index(默认?/span>0)Q直接这栯?/span>up(element, index)?/span>up(element)

down(element, expression, index)Q跟up一P只是q回的是子孙节点

previous(element, expression, index)Q返回前面的兄弟节点

next(element, expression, index)Q返回后面的兄弟节点

getElementsBySelector(element,args)Q?/span>Selector.findChildElements(element, args)的封装,args表示可以传递多个参敎ͼ每个参数是一?/span>css selector表达式,q回element的子孙节点中W合M一?/span>css selector表达式的元素l成的数l?/span>

getElementsByClassName(element, className)Q返?/span>element中的子孙节点中符?/span>clsssName的元?/span>

readAttribute(element, name)Q?/span>return $(element).getAttribute(name)Q之所以添加这个方法是因ؓ?/span>IE?/span>Safari(Mac)?/span>getAttribute不是一个真正的函数Q它没有call?/span>apply{方法,所以在很多时候调用会出现错误(Prototype中很多地方用了函数的这两个Ҏ)Q例如下面的例子(官方文档中的一个例?/span>)Q就只能使用readAttributeQ?/span>

<div id="widgets">

 <div class="widget" widget_id="7">…</div>

 <div class="widget" widget_id="8">…</div>

 <div class="widget" widget_id="9">…</div>

</div>

$$(’div.widget’).invoke(’readAttribute’, 'widget_id’)

// ["7", "8", "9"]

getHeight: function(element)Q返回元素高度,return element.offsetHeight

classNames: function(element)Q返回一?/span>Element.ClassNames对象Q改对象提供对元?/span>class的操作,包括add?/span>remove?/span>set{,一般很用,使用Element.addClassName{方法就可以?/span>(在下面)

hasClassName: function(element, className) Q判?/span>element是否含有className

addClassName: function(element, className) Q给elementd一?/span>class

removeClassName: function(element, className) Q移除元素中的一?/span>class

observe()Q调?/span>Event对象(Prototype中的Q将在后面介l?/span>)?/span>observeҎ为元素注册事?/span>handle

stopObserving() Q移除注册的事ghandle

cleanWhitespace: function(element)Q移除元素中I白的文本子节点

empty: function(element)Q判断元素是否ؓI?/span>

childOf: function(element, ancestor) Q判?/span>element是否?/span>ancestor的子孙节?/span>

scrollTo: function(element) Q滚动条Ud到元素所在的地方

getStyle: function(element, style) Q得到元素某?/span>css样式的|例如$(element).getStyle("float")

setStyle: function(element, style) Q设|元素的css样式Q?/span>style十一个对象,例如element.setStyle({left: "40px", "background-color":"#666"})

getDimensions: function(element) Q得到元素的寸Q即使元素是隐藏的也可以正确的返回,q回 return {width: originalWidth, height: originalHeight}q样的关联数l?/span>

makePositioned: function(element) Q当元素?/span>position css属性ؓstatic或不存在使,次属性更改ؓrelative

undoPositioned: function(element) Q跟makePositioned相反的操?/span>

makeClipping: function(element) Q把元素变成clipping(切片)Q也是讄元素?/span>overflow属性ؓhidden

undoClipping: function(element)Q反转上面的Ҏ对元素所做的修改

hasAttribute(element)Q判断元素是否有某个属?/span>

Element对象的方法是不是不少啊,哈哈Q下面介l有?/span>Insertion的四个类

Insertion.BeforeQ将内容插入到元素的前面Q内容在元素外面

Insertion.TopQ将内容插入到元素的剙Q内容在元素里面

Insertion.BottomQ将内容插入到元素的底部Q内容在元素里面

Insertion.AfterQ将内容插入到元素后面,内容在元素外?/span>

使用它们的方法比较简单:new Insertion.where(element, content)Q其?/span>where表示上面?/span>Before?/span>Top{,content?/span>html字符Ԍ注意其中javascript片断会执?/span>

l于写完了,Prototype?/span>ElementҎq真不少

虽然以前觉得自己?/span>Prototypeq比较熟悉,写的也有点篏Q但是发现自己收获仍然挺大的Qؓ了写些方法的具体作用和用法,必须自己一行行的把Prototype的代码弄清楚Q自己?/span>Prototype中很多精巧的写法有了更深ȝ认识和理?/span>

写这个教E的主要目的是ؓ了给大家一个快速的参考,大家q是对照着源代码看才会真正有所提高

q时我第一ơ写比较完整的一个教E,错误q稚的地方在所隑օQ希望大家批评指正,互相学习提高Q?/font>

(?/span>).Prototype使用指南?/span>Selector

Selector是利?/span>css selector来匹配选择面元素的,所以要理解Selector首先应该?/span>css selector有所理解Q下面是css2 selector的语法,当然很多览器只是支持其中的一部分Q?/span>Prototype 中的Selector主要支持tag选择器?/span>class选择器和id选择器,q有属?/span>(attribute)选择器,基本上包含我们^时所用的所有类?/span>

The following table summarizes CSS2 selector syntax, 详细的可以看http://www.w3.org/TR/REC-CSS2/selector.html:

Pattern

Meaning

Described in section

*

Matches any element.

Universal selector

E

Matches any E element (i.e., an element of type E).

Type selectors

E F

Matches any F element that is a descendant of an E element.

Descendant selectors

E > F

Matches any F element that is a child of an element E.

Child selectors

E:first-child

Matches element E when E is the first child of its parent.

The :first-child pseudo-class

E:link E:visited

Matches element E if E is the source anchor of a hyperlink of which the target is not yet visited (:link) or already visited (:visited).

The link pseudo-classes

E:active E:hover E:focus

Matches E during certain user actions.

The dynamic pseudo-classes

E:lang(c)

Matches element of type E if it is in (human) language c (the document language specifies how language is determined).

The :lang() pseudo-class

E + F

Matches any F element immediately preceded by an element E.

Adjacent selectors

E[foo]

Matches any E element with the “foo” attribute set (whatever the value).

Attribute selectors

E[foo=”warning”]

Matches any E element whose “foo” attribute value is exactly equal to “warning”.

Attribute selectors

E[foo~=”warning”]

Matches any E element whose “foo” attribute value is a list of space-separated values, one of which is exactly equal to “warning”.

Attribute selectors

E[lang|=”en”]

Matches any E element whose “lang” attribute has a hyphen-separated list of values beginning (from the left) with “en”.

Attribute selectors

DIV.warning

HTML only. The same as DIV[class~=”warning”].

Class selectors

E#myid

Matches any E element ID equal to “myid”.

ID selectors

Selector中包?/span>Selector对象和类Q?/span>

Selector对象h下面两个ҎQ?/span>

match(element)Q元素是否与?/span>selector匚wQ在Element中已l介l了
findElements(parentNode)Q?/span>parentNode中所有匹配本selector的子孙元素列?/span>

使用Ҏ也很?/span> var s=new Selector(expression); s.match(element); s.findElements($(element))Q其?/span>expression可以是如下方?/span> "div"?/span>"#id"?/span>".class"?/span>"div#id"?/span>"div[attribute]"?/span>"div[attribute=fff]"?/span>"div[attribute!=sdf]"

其中Selector也有几个静态方法,它们分别是:

matchElements(elements, expression)Q返?/span>elements中符?/span>expression的元素列?/span>
findElement(elements, expression, index)Q返?/span>elements中符?/span>expression的元素列表中索引?/span>index的元?/span>
findChildElements(element, expressions)Q找?/span>element的子孙元素中W合expressions的元素列表,其中expressions是一?/span>expression数组Q其中的expression支持"div li.#id"形式

$$ҎQ只是简单的调用return Selector.findChildElements(document, $A(arguments))

虽然Selector有这么多ҎQ但是大部分都是内部调用的,我们一般都很少使用Q因为我们有个一个方便的Ҏ$$Q对于绝大部分情况已l够了



hummer 2008-05-29 09:40 发表评论
]]>
PrototypeQ?.5 rc2)使用指南Q一Q??/title><link>http://www.tkk7.com/hummer008/articles/203707.html</link><dc:creator>hummer</dc:creator><author>hummer</author><pubDate>Thu, 29 May 2008 01:37:00 GMT</pubDate><guid>http://www.tkk7.com/hummer008/articles/203707.html</guid><wfw:comment>http://www.tkk7.com/hummer008/comments/203707.html</wfw:comment><comments>http://www.tkk7.com/hummer008/articles/203707.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/hummer008/comments/commentRss/203707.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/hummer008/services/trackbacks/203707.html</trackback:ping><description><![CDATA[<h2 style="margin: 13pt 0cm"><font size="3">(<span style="font-family: 黑体">一</span>).<span style="font-family: 黑体">序言</span></font></h2> <p style="margin: 0cm 0cm 0pt"><font size="3"><span style="font-family: 宋体">在写q个指南之前Q先介绍一?/span><font face="Times New Roman">Prototype</font><span style="font-family: 宋体">主要是干吗的Q如果你比较x</span><font face="Times New Roman">ajax/javascipt</font><span style="font-family: 宋体">斚w的应用,你应该早听说过q个</span><font face="Times New Roman"> javascript framework</font><span style="font-family: 宋体">?/span><font face="Times New Roman"> Prototype</font><span style="font-family: 宋体">是一个基?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">应用框架Q先引用一D官方网站的介绍</span></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman" size="3">Prototype is a JavaScript framework that aims to ease development of dynamic web applications. Featuring a unique, easy-to-use toolkit for class-driven development and the nicest Ajax library around, Prototype is quickly becoming the codebase of choice for web application developers everywhere.</font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><span style="font-family: 宋体">Ҏ作者自q介绍Q?/span><font face="Times New Roman">Prototype</font><span style="font-family: 宋体">的目的是Z更方便的开?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">的应用,使用它可以更加方便简单的使用</span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">~程Q开发出面向对象?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">E序Q?/span><font face="Times New Roman">Prototype</font><span style="font-family: 宋体">中包含包含了一个功能强大好用的</span><font face="Times New Roman">ajax</font><span style="font-family: 宋体">框架Q?/span><font face="Times New Roman">Prototype</font><span style="font-family: 宋体">是一个基性的框架Q很多更高层ơ的框架都以它ؓ基础Q例?/span><font face="Times New Roman">scriptaculous</font><span style="font-family: 宋体">效果?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Prototype</font><span style="font-family: 宋体">中包含一下几个部分:</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">base: Prototype</font><span style="font-family: 宋体">中应用的基本功能Q基本上其他所有部分都依赖于它Q包括用于面向对象风格的</span><font face="Times New Roman">Class.create</font><span style="font-family: 宋体">?/span><font face="Times New Roman">Object.extend</font><span style="font-family: 宋体">Q一?/span><font face="Times New Roman">Try</font><span style="font-family: 宋体">对象Q函数绑定,</span><font face="Times New Roman">number</font><span style="font-family: 宋体">扩展Q?/span><font face="Times New Roman">PeriodicalExecuter(</font><span style="font-family: 宋体">周期性执行某个函数的功能</span><font face="Times New Roman">)</font><span style="font-family: 宋体">{?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">string: </font><span style="font-family: 宋体">?/span><font face="Times New Roman">String</font><span style="font-family: 宋体">原型的扩展,?/span><font face="Times New Roman">string</font><span style="font-family: 宋体">d?/span><font face="Times New Roman">strip,escapeHTML</font><span style="font-family: 宋体">{等好用的方?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">enumerable: </font><span style="font-family: 宋体">枚Dcd</span><font face="Times New Roman">(array, hash, range</font><span style="font-family: 宋体">{?/span><font face="Times New Roman">)</font><span style="font-family: 宋体">的父cd象,提供枚Dcd的共同方?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">array: </font><span style="font-family: 宋体">?/span><font face="Times New Roman">Array</font><span style="font-family: 宋体">原型的扩展,?/span><font face="Times New Roman">array</font><span style="font-family: 宋体">d?/span><font face="Times New Roman">indexOf</font><span style="font-family: 宋体">?/span><font face="Times New Roman">without</font><span style="font-family: 宋体">{方?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">hash: </font><span style="font-family: 宋体">?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">提供了一个好用简单的</span><font face="Times New Roman">Hash</font><span style="font-family: 宋体">实现</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">range: </font><span style="font-family: 宋体">l承?/span><font face="Times New Roman">enumerable</font><span style="font-family: 宋体">Q一个范?/span><font face="Times New Roman">(</font><span style="font-family: 宋体">例如</span><font face="Times New Roman">3</font><span style="font-family: 宋体">?/span><font face="Times New Roman">67)</font><span style="font-family: 宋体">对象</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">ajax: </font><span style="font-family: 宋体">一个功能强大好用的</span><font face="Times New Roman">ajax</font><span style="font-family: 宋体">框架</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">dom: </font><span style="font-family: 宋体">对基于浏览器的开发提供了很好的跨览器封装,q添加很多强大的功能</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">selector: </font><span style="font-family: 宋体">提供了?/span><font face="Times New Roman">class</font><span style="font-family: 宋体">Q?/span><font face="Times New Roman">css</font><span style="font-family: 宋体">{选择元素的功?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">form: </font><span style="font-family: 宋体">关于表单的一些功?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">event: </font><span style="font-family: 宋体">单的夸^C件封?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">position: </font><span style="font-family: 宋体">提供了一些关于元素位|方面的功能</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><span style="font-family: 宋体">可以?/span><font face="Times New Roman">Prototype</font><span style="font-family: 宋体">想一把瑞士军刀Qؓ</span><font face="Times New Roman">javascipt</font><span style="font-family: 宋体">装了很多通用的功能,大大化了</span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">应用的开发,l?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">开发h员增M很大的信心,</span><font face="Times New Roman">Prototype</font><span style="font-family: 宋体">可以q行于以下^収ͼ使用它再也不用各U跨q_{问题烦g</span></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman" size="3">* Microsoft Internet Explorer for Windows, version 6.0 and higher</font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman" size="3">* Mozilla Firefox 1.0/Mozilla 1.7 and higher</font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman" size="3">* Apple Safari 1.2 and higher</font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><span style="font-family: 宋体">不过要注意的是:要想很好的理?/span><font face="Times New Roman">Prototype</font><span style="font-family: 宋体">Q应该首先理解一?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">面向对象开发的一些知识以后的文章对</span><font face="Times New Roman">Prototype</font><span style="font-family: 宋体">中具体的每个功能中的Ҏ做一个详l的介绍Q包括作用,实例{?/span><font face="Times New Roman">Prototype</font><span style="font-family: 宋体">官方|站是:</span><font face="Times New Roman">http://prototype.conio.net/ </font><span style="font-family: 宋体">Q目前发布版q只?/span><font face="Times New Roman">1.4, </font><span style="font-family: 宋体">但是现在?/span><font face="Times New Roman">1.5</font><span style="font-family: 宋体">已经发生了很大的变化Q而且很多Z</span><font face="Times New Roman">prototype</font><span style="font-family: 宋体">的库使用的都?/span><font face="Times New Roman">1.5</font><span style="font-family: 宋体">的,所以强烈徏议通过</span><font face="Times New Roman">svn</font><span style="font-family: 宋体">下蝲最新版代码</span></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman" size="3"> </font></p> <h2 style="margin: 13pt 0cm"><font size="3">(<span style="font-family: 黑体">?/span>).Prototype<span style="font-family: 黑体">Q?/span>1.5 rc2)<span style="font-family: 黑体">使用指南?/span>base.js</font></h2> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">base.js</font><span style="font-family: 宋体">中包含下面的内容</span></font></p> <p style="margin: 0cm 0cm 0pt"><span style="font-family: 宋体"><font size="3">cȝ创徏与承:</font></span></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Class.create(): </font><span style="font-family: 宋体">创徏一个类Q例?/span><font face="Times New Roman"> person=Class.create()</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Object.extend(destination, source): </font><span style="font-family: 宋体">?/span><font face="Times New Roman">source</font><span style="font-family: 宋体">中方法属?/span><font face="Times New Roman">copy</font><span style="font-family: 宋体">?/span><font face="Times New Roman">destination(</font><span style="font-family: 宋体">使用</span><font face="Times New Roman">for property in source)</font><span style="font-family: 宋体">Q需要注意的是,</span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">中除了基本类?/span><font face="Times New Roman">(Number, Boolean)</font><span style="font-family: 宋体">外都是引用类型,所以这U?/span><font face="Times New Roman">copy</font><span style="font-family: 宋体">一般只?/span><font face="Times New Roman">copy</font><span style="font-family: 宋体">引用而已Q?/span><font face="Times New Roman">destination</font><span style="font-family: 宋体">?/span><font face="Times New Roman">source</font><span style="font-family: 宋体">q是指向同一个方法或对象属?/span><font face="Times New Roman"> (function array object)</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><span style="font-family: 宋体">在面向对象的~程中,一般通过</span><font face="Times New Roman">Class.create</font><span style="font-family: 宋体">新徏一个类Q如果这个类l承于另一个类Q一般?/span><font face="Times New Roman">Object.extend (class.prototype, parentClass.prototype)</font><span style="font-family: 宋体">或?/span><font face="Times New Roman">Object.extend(class.prototype, aparentClassInstance)</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Object</font><span style="font-family: 宋体">构造函数的扩展Q?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Object</font><span style="font-family: 宋体">是其他对象实例的构造函?/span><font face="Times New Roman">(var a=new Object())</font><span style="font-family: 宋体">Q也是所有其他类的父c,?/span><font face="Times New Roman">Object</font><span style="font-family: 宋体">直接扩展</span><font face="Times New Roman">(</font><span style="font-family: 宋体">注意不是扩展</span><font face="Times New Roman">Object.prototype</font><span style="font-family: 宋体">Q扩?/span><font face="Times New Roman"> Object.prototype</font><span style="font-family: 宋体">相当于添加实例方?/span><font face="Times New Roman">)</font><span style="font-family: 宋体">相当于ؓ</span><font face="Times New Roman">Object</font><span style="font-family: 宋体">cL加静态方?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Object.inspect(object): </font><span style="font-family: 宋体">调用</span><font face="Times New Roman">object</font><span style="font-family: 宋体">?/span><font face="Times New Roman">inspect(</font><span style="font-family: 宋体">如果定义?/span><font face="Times New Roman">)</font><span style="font-family: 宋体">?/span><font face="Times New Roman">toString</font><span style="font-family: 宋体">ҎQ返回一个对象的字符串表C?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Object.keys(object): </font><span style="font-family: 宋体">q回一个对象的所有属性和Ҏ名称l成的数l?/span><font face="Times New Roman">, </font><span style="font-family: 宋体">例如</span><font face="Times New Roman">Object.keys(document.body)</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Object.values(object):</font><span style="font-family: 宋体">q回一个对象的所有属性和Ҏ的值组成的数组</span><font face="Times New Roman">, </font><span style="font-family: 宋体">例如</span><font face="Times New Roman">Object.values(docuement)</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Object.clone(object): </font><span style="font-family: 宋体">q回一个对象的</span><font face="Times New Roman">clone</font><span style="font-family: 宋体">版本Q其实是执行</span><font face="Times New Roman">Object.extent</font><span style="font-family: 宋体">Ҏ?/span><font face="Times New Roman">object</font><span style="font-family: 宋体">中的Ҏ属?/span><font face="Times New Roman">copy</font><span style="font-family: 宋体">C个新对象中,然后q回q个对象</span></font></p> <p style="margin: 0cm 0cm 0pt"><span style="font-family: 宋体"><font size="3">函数邦定Q?/font></span></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><span style="font-family: 宋体">定义?/span><font face="Times New Roman">Function</font><span style="font-family: 宋体">对象的两个方法,</span><font face="Times New Roman">bind</font><span style="font-family: 宋体">?/span><font face="Times New Roman">bindAsEventListener</font><span style="font-family: 宋体">Q这两个Ҏ是一个函数的两个ҎQ对?/span><font face="Times New Roman">java</font><span style="font-family: 宋体">?/span><font face="Times New Roman">c#</font><span style="font-family: 宋体">E序员来_看到q个也许感到很惊Ӟ因ؓ在他们看来函数只是一个程序语句组l结构而已?/span><font face="Times New Roman">></font><span style="font-family: 宋体">怎么q有ҎQ而且q可以扩展?q也?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">{脚本语a相对?/span><font face="Times New Roman">java</font><span style="font-family: 宋体">{一个非常强大的功能Q函C是一个对象,函数名就是这个对象的名称Q只要你愿意Q你也可以?/span><font face="Times New Roman"> new Function(</font><span style="font-family: 宋体">…</span><font face="Times New Roman">)</font><span style="font-family: 宋体">来定义函敎ͼ所以ؓ函数定义Ҏ也就很正怸q了</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><span style="font-family: 宋体">q两个函数的主要作用是ؓ了解决?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">面向对象风格~程?/span><font face="Times New Roman">this</font><span style="font-family: 宋体">的引用问题,?/span><font face="Times New Roman">javasctipt</font><span style="font-family: 宋体">?/span><font face="Times New Roman">this</font><span style="font-family: 宋体">关键字始l指向调用该函数的对象或者指向?/span><font face="Times New Roman">call</font><span style="font-family: 宋体">Q?/span><font face="Times New Roman">apply</font><span style="font-family: 宋体">Ҏ指定的对象(具体q方面的知识可以自己</span><font face="Times New Roman">google</font><span style="font-family: 宋体">一下,以下pd?/span><font face="Times New Roman">prototype</font><span style="font-family: 宋体">的介l也假设读者对</span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">语言比较熟悉了,如果不熟悉可以找?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">权威指南q本书看看)</span><span style="font-family: 宋体">要理解这个问题首先要理解</span><span style="font-family: 宋体">始终指向q个问题Q就?/span><font face="Times New Roman">this</font><span style="font-family: 宋体">q个关键字比较特D,不能把他当成一般的变量名看待,最常见的一个错误就是在q回函数的调用中使用</span><font face="Times New Roman">this</font><span style="font-family: 宋体">Q例?/span><font face="Times New Roman">return function(){this.aMethod()}, </font><span style="font-family: 宋体">当你下次调用q个q回的匿名方法时Q这?/span><font face="Times New Roman">this</font><span style="font-family: 宋体">引用的内容又指向了调用这个函数的对象了,C的一点的</span><font face="Times New Roman">this</font><span style="font-family: 宋体">是个关键字,不是变量名,不会产生闭包</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><span style="font-family: 宋体">?/span><font face="Times New Roman">Number</font><span style="font-family: 宋体">的扩?/span><font face="Times New Roman">(</font><span style="font-family: 宋体">注意</span><font face="Times New Roman">num</font><span style="font-family: 宋体">也可以看成对象,其实是在使用的时候系l自动打包成</span><font face="Times New Roman">Number</font><span style="font-family: 宋体">对象</span><font face="Times New Roman">)</font><span style="font-family: 宋体">Q?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">toColorPart</font><span style="font-family: 宋体">Q把数字转换为可以用于表C?/span><font face="Times New Roman">color</font><span style="font-family: 宋体">?/span><font face="Times New Roman">16</font><span style="font-family: 宋体">q制|例如</span><font face="Times New Roman"> 7.toColorPart()=>"07"</font><span style="font-family: 宋体">Q?/span><font face="Times New Roman">28.toColorPart()=>"1C"</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">succ: </font><span style="font-family: 宋体">q回</span><font face="Times New Roman">num++, </font><span style="font-family: 宋体">但不改变</span><font face="Times New Roman">num</font><span style="font-family: 宋体">本n的|其实是</span><font face="Times New Roman"> return this</font><span style="font-family: 宋体">Q?/span><font face="Times New Roman">1</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">times</font><span style="font-family: 宋体">Q对?/span><font face="Times New Roman">0</font><span style="font-family: 宋体">到这个数字轮调用一个函?/span><font face="Times New Roman">, </font><span style="font-family: 宋体">例如</span><font face="Times New Roman">function a(n){docuement.write(n)}, 10.times(a), </font><span style="font-family: 宋体">显C?/span><font face="Times New Roman">012345678910, </font><span style="font-family: 宋体">注意函数也是一个对象,而且与其他对象ƈ没有实质的区?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Try</font><span style="font-family: 宋体">对象Q?/span><font face="Times New Roman"> Try</font><span style="font-family: 宋体">对象提供了一个很有趣的功?/span><font face="Times New Roman">, </font><span style="font-family: 宋体">先看一下如下的代码Q?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman"><font size="3">var Ajax = { </font></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman"><font size="3">getTransport: function() {    </font></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman"><font size="3">   return Try.these(      </font></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman"><font size="3">              function() {return new XMLHttpRequest()},     </font></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman"><font size="3">              function() {return new ActiveXObject(’Msxml2.XMLHTTP’)},      </font></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman"><font size="3">             function() {return new ActiveXObject(’Microsoft.XMLHTTP’)}   )</font></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman"><font size="3">     || false;</font></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman"><font size="3">   }</font></font></p> <p style="margin: 0cm 0cm 0pt"><span style="font-family: 宋体"><font size="3">?/font></span></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Try</font><span style="font-family: 宋体">对象提供了一个方?/span><font face="Times New Roman">these, </font><span style="font-family: 宋体">q个Ҏ接受一个函数类型的参数列表Q然后轮执行这些函敎ͼ当其中一个函数没有生错误时Q就停止执行Qƈ且返回这个函数返回的|自己慢慢体会?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">PeriodicalExecuter(</font><span style="font-family: 宋体">周期性执行器</span><font face="Times New Roman">)</font><span style="font-family: 宋体">对象</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><span style="font-family: 宋体">q个对象是对</span><font face="Times New Roman">setInterval</font><span style="font-family: 宋体">Ҏ的简单封装,使用Ҏ如下</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">var a=new PeriodicalExecuter(callback, frequency ) </font><span style="font-family: 宋体">Q其?/span><font face="Times New Roman">callback</font><span style="font-family: 宋体">指要执行的函数名</span><font face="Times New Roman"> frequency</font><span style="font-family: 宋体">?/span><span style="font-family: 宋体">每次执行的时间间?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><span style="font-family: 宋体">要停止的话,调用q个对象?/span><font face="Times New Roman">stop</font><span style="font-family: 宋体">Ҏ可以了</span><font face="Times New Roman"> a.stop() </font></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman" size="3"> </font></p> <h2 style="margin: 13pt 0cm"><font size="3">(<span style="font-family: 黑体">?/span>)Prototype<span style="font-family: 黑体">Q?/span>1.5 rc2)<span style="font-family: 黑体">使用指南?/span>string.js</font></h2> <p style="margin: 0cm 0cm 0pt"><font size="3"><span style="font-family: 宋体">下面介绍</span><font face="Times New Roman">Prototype</font><span style="font-family: 宋体">?/span><font face="Times New Roman">String</font><span style="font-family: 宋体">对象的扩展部分:</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><span style="font-family: 宋体">q部分主要ؓ</span><font face="Times New Roman">string</font><span style="font-family: 宋体">对象d了几个很有用的方?/span><font face="Times New Roman">:</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">strip(): </font><span style="font-family: 宋体">L字符串两边的I白</span><font face="Times New Roman">, </font><span style="font-family: 宋体">例如</span><font face="Times New Roman">" jj ".strip()</font><span style="font-family: 宋体">q回</span><font face="Times New Roman">"jj"</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">stripTags()</font><span style="font-family: 宋体">Q去掉字W串中的</span><font face="Times New Roman">html</font><span style="font-family: 宋体">标签</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">stripScripts(): </font><span style="font-family: 宋体">L字符串中?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">代码D?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">extractScripts(): </font><span style="font-family: 宋体">q回字符串中?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">代码Q返回数l?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">evalScripts(): </font><span style="font-family: 宋体">执行字符串中?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">代码</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">escapeHTML()</font><span style="font-family: 宋体">Q将字符串中?/span><font face="Times New Roman">html</font><span style="font-family: 宋体">代码转换为可以直接显C的格式</span><font face="Times New Roman">, </font><span style="font-family: 宋体">例如?/span><font face="Times New Roman">< </font><span style="font-family: 宋体">转化?/span><font face="Times New Roman"> &lt; </font><span style="font-family: 宋体">Q在</span><font face="Times New Roman">ie6</font><span style="font-family: 宋体">中有</span><font face="Times New Roman">bug</font><span style="font-family: 宋体">Q执行这个操作返回的字符Ԍ多个连在一LI白变成了一个,所以很多换行什么的都被L?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">unescapeHTML(): escapeHTML</font><span style="font-family: 宋体">的反向过E?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">truncate(length, truncation): </font><span style="font-family: 宋体">截断Q例?/span><font face="Times New Roman">"abcdefghigkl".truncate(10)</font><span style="font-family: 宋体">q回</span><font face="Times New Roman">abcdefg</font><span style="font-family: 宋体">…</span><font face="Times New Roman">, truncation</font><span style="font-family: 宋体">默认?/span><font face="Times New Roman">"</font><span style="font-family: 宋体">…</span><font face="Times New Roman">" toQueryParams(separator)/parseQuery(separator)</font><span style="font-family: 宋体">Q将一?/span><font face="Times New Roman">querystring</font><span style="font-family: 宋体">转化Z?/span><font face="Times New Roman">hash</font><span style="font-family: 宋体">表(其实是一个对象,?/span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">中对象可以当?/span><font face="Times New Roman">hash</font><span style="font-family: 宋体">表来用,因ؓ对象的属性或Ҏ可以通过</span><font face="Times New Roman">object[propertyName]</font><span style="font-family: 宋体">来访问)</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">toArray(): return this.split('</font><span style="font-family: 宋体">’</span><font face="Times New Roman">), </font><span style="font-family: 宋体">转化Z个字W数l?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">camelize(): </font><span style="font-family: 宋体">?/span><font face="Times New Roman">background-color</font><span style="font-family: 宋体">的Ş式{化ؓ</span><font face="Times New Roman">backgroundColor</font><span style="font-family: 宋体">形式Q用?/span><font face="Times New Roman">style/css</font><span style="font-family: 宋体">?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">capitalize(): </font><span style="font-family: 宋体">q回一个首字母大写的字W串</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">inspect(useDoubleQuotes): </font><span style="font-family: 宋体">q回字符串的表示形式</span><font face="Times New Roman">, </font><span style="font-family: 宋体">例如</span><font face="Times New Roman">"sdfj""sfa".inspect() </font><span style="font-family: 宋体">q回</span><span style="font-family: 宋体">“’</span><font face="Times New Roman">sdfj"sfa</font><span style="font-family: 宋体">’”</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">gsub(pattern, replacement)</font><span style="font-family: 宋体">Q?/span><font face="Times New Roman">pattern</font><span style="font-family: 宋体">是一个正则表辑ּQ?/span><font face="Times New Roman">replacement</font><span style="font-family: 宋体">是一个函?/span><font face="Times New Roman">(</font><span style="font-family: 宋体">或者是一?/span><font face="Times New Roman">template</font><span style="font-family: 宋体">字符?/span><font face="Times New Roman">)</font><span style="font-family: 宋体">Q对于字W串中每个匹?/span><font face="Times New Roman">pattern</font><span style="font-family: 宋体">的部分?/span><font face="Times New Roman">replacement</font><span style="font-family: 宋体">处理Q然后将</span><font face="Times New Roman"> replacement</font><span style="font-family: 宋体">q回的值将原来匚w的部分替换掉Q例?/span><font face="Times New Roman">"skdjfAsfdjkAdk".gsub(/A/,function(match) {return match[0].toLowerCase()}), </font><span style="font-family: 宋体">字W串所有的</span><font face="Times New Roman">A</font><span style="font-family: 宋体">转化?/span><font face="Times New Roman">a, </font><span style="font-family: 宋体">注意</span><font face="Times New Roman">pattern</font><span style="font-family: 宋体">中不要添?/span><font face="Times New Roman">g</font><span style="font-family: 宋体">选项Q因?/span><font face="Times New Roman">gsub</font><span style="font-family: 宋体">会递归的执?/span><font face="Times New Roman">match</font><span style="font-family: 宋体">Ҏ</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">sub(pattern, replacement, count) </font><span style="font-family: 宋体">Q?/span><font face="Times New Roman">gsub</font><span style="font-family: 宋体">的另一UŞ式,不过可以讄执行的次?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">scan(pattern, iterator): </font><span style="font-family: 宋体">?/span><font face="Times New Roman">gsub</font><span style="font-family: 宋体">差不多,但是q回的是字符串本w,也就是说对于</span><font face="Times New Roman">pattern</font><span style="font-family: 宋体">中的每个匚w执行</span><font face="Times New Roman">iterator</font><span style="font-family: 宋体">Q但是不q回替换的字W串</span><font face="Times New Roman">"skdjfAsfdjkAdk".gsub(/A/,function(){alert have a A</font><span style="font-family: 宋体">’</span><font face="Times New Roman">})</font></font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman" size="3">underscore(): 'borderBottomWidth’.underscore() -> 'border_bottom_width’</font></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman" size="3">dasherize(): 'Hello_World’.dasherize() -> 'Hello-World’</font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Template</font><span style="font-family: 宋体">模板c:</span></font></p> <p style="margin: 0cm 0cm 0pt"><span style="font-family: 宋体"><font size="3">使用ҎQ?/font></span></p> <p style="margin: 0cm 0cm 0pt"><font face="Times New Roman"><font size="3">var template = new Template(replacement, pattern);                </font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">template.evaluate(object) </font><span style="font-family: 宋体">有点?/span><font face="Times New Roman">php</font><span style="font-family: 宋体">中的模板Q默?/span><font face="Times New Roman">(</font><span style="font-family: 宋体">没有提供</span><font face="Times New Roman">pattern)</font><span style="font-family: 宋体">?/span><font face="Times New Roman">{propertyName}</font><span style="font-family: 宋体">形式的东西替换了</span><font face="Times New Roman">object</font><span style="font-family: 宋体">的属性?/span></font></p> <h2 style="margin: 13pt 0cm"><font size="3">(<span style="font-family: 黑体">?/span>).Prototype<span style="font-family: 黑体">Q?/span>1.5 rc2)<span style="font-family: 黑体">使用指南?/span>Enumerable.js</font></h2> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Enumerable</font><span style="font-family: 宋体">是一个抽象对象(需要说明的是,</span><font face="Times New Roman">javascript</font><span style="font-family: 宋体">中ƈ没有cȝ概念Q所指的cM是一个函敎ͼl承一般指的是一个对?/span><font face="Times New Roman">(</font><span style="font-family: 宋体">?/span><font face="Times New Roman">)</font><span style="font-family: 宋体">它的方法属?/span><font face="Times New Roman">copy(</font><span style="font-family: 宋体">通过</span><font face="Times New Roman">Object.extend, copy</font><span style="font-family: 宋体">的是引用</span><font face="Times New Roman">)</font><span style="font-family: 宋体">到子c?/span><font face="Times New Roman">(</font><span style="font-family: 宋体">函数</span><font face="Times New Roman">)</font><span style="font-family: 宋体">?/span><font face="Times New Roman">prototype</font><span style="font-family: 宋体">属?/span><font face="Times New Roman">(</font><span style="font-family: 宋体">一个对?/span><font face="Times New Roman">)</font><span style="font-family: 宋体">中)</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Enumerable</font><span style="font-family: 宋体">不能直接使用Q它被很多枚丄型(</span><font face="Times New Roman">Hash</font><span style="font-family: 宋体">?/span><font face="Times New Roman">Array</font><span style="font-family: 宋体">?/span><font face="Times New Roman">Range</font><span style="font-family: 宋体">{)所l承Q承的cd都要实现一?/span><font face="Times New Roman">_each</font><span style="font-family: 宋体">ҎQ提供具体类型的枚DҎ</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">Enumerable</font><span style="font-family: 宋体">为其他子cL供了如下的方法:</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">each(iterator): iterator</font><span style="font-family: 宋体">是一个函数对?/span><font face="Times New Roman">, </font><span style="font-family: 宋体">q个Ҏ调用具体cd?/span><font face="Times New Roman">_each</font><span style="font-family: 宋体">Ҏ对自w包含的每个对象调用</span><font face="Times New Roman">iterator</font><span style="font-family: 宋体">Q例如如?/span><font face="Times New Roman">Enumerable</font><span style="font-family: 宋体">具体指的是一?/span><font face="Times New Roman">Array</font><span style="font-family: 宋体">Q?/span><font face="Times New Roman">eg: var a=[2,3,4], </font><span style="font-family: 宋体">?/span><font face="Times New Roman">a.each(iterator)</font><span style="font-family: 宋体">Ҏ依ơ调?/span><font face="Times New Roman">iterator(2,0) ,iterator(3,1), iterator(4,3)</font><span style="font-family: 宋体">Q其中第二个参数指的是烦引。这个方法几乎在</span><font face="Times New Roman">Enumerable</font><span style="font-family: 宋体">中的每个Ҏ中都要用?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">eachSlice(number, iterator)</font><span style="font-family: 宋体">Q将</span><font face="Times New Roman">Enumerable </font><span style="font-family: 宋体">cd对象每个每个按照</span><font face="Times New Roman">number</font><span style="font-family: 宋体">分开Q例?/span><font face="Times New Roman">[1,2,3,4,5].eachSlice(3)=>[[1,2,3],[4,5]], </font><span style="font-family: 宋体">没有提供</span><font face="Times New Roman">iterator, </font><span style="font-family: 宋体">?/span><font face="Times New Roman">iterator=Prototype.K: function(k){return k}</font><span style="font-family: 宋体">Q?/span><font face="Times New Roman">Prototype</font><span style="font-family: 宋体">中的很多</span><font face="Times New Roman">iterator</font><span style="font-family: 宋体">默认值都是这个,或者是</span><font face="Times New Roman">Prototype.emptyFunction: function() {},</font><span style="font-family: 宋体">其实实际上返回的?/span><font face="Times New Roman">[iterator([1,2,3]),iterator([4,5])]</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">all(iterator): </font><span style="font-family: 宋体">?/span><font face="Times New Roman">Enumerable</font><span style="font-family: 宋体">cd中的每个D?/span><font face="Times New Roman">iterator</font><span style="font-family: 宋体">Q如果其中有一个返?/span><font face="Times New Roman">false</font><span style="font-family: 宋体">Q则q回</span><font face="Times New Roman">false</font><span style="font-family: 宋体">Q否则返?/span><font face="Times New Roman">true</font><span style="font-family: 宋体">Q相当于判断是否每个值执?/span><font face="Times New Roman">iterator</font><span style="font-family: 宋体">都是</span><font face="Times New Roman">"true"</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">any(iterator): </font><span style="font-family: 宋体">?/span><font face="Times New Roman">all</font><span style="font-family: 宋体">相反Q判断是否每个值都?/span><font face="Times New Roman">"false"</font><span style="font-family: 宋体">Q是否有一个值是</span><font face="Times New Roman">true</font><span style="font-family: 宋体">Q?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">collect(iterator)/map: </font><span style="font-family: 宋体">Ҏ个D?/span><font face="Times New Roman">iterator</font><span style="font-family: 宋体">Q将l果l成一个新的数l返?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">detect(iterator)/find: </font><span style="font-family: 宋体">Ҏ个D?/span><font face="Times New Roman">iterator</font><span style="font-family: 宋体">Q如果有一个不?/span><font face="Times New Roman">false</font><span style="font-family: 宋体">Q则q回q个执行</span><font face="Times New Roman">iterator</font><span style="font-family: 宋体">后不?/span><font face="Times New Roman">false</font><span style="font-family: 宋体">的?/span><font face="Times New Roman">(</font><span style="font-family: 宋体">不是q回执行</span><font face="Times New Roman">iterator</font><span style="font-family: 宋体">后的?/span><font face="Times New Roman">)</font><span style="font-family: 宋体">Q相当于扑ևW一个真?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">findAll(iterator)/select: </font><span style="font-family: 宋体">相当?/span><font face="Times New Roman">detect, </font><span style="font-family: 宋体">但是扑և所有的真|q回一个数l?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">grep(pattern, iterator)</font><span style="font-family: 宋体">Q返回所以符?/span><font face="Times New Roman">pattern</font><span style="font-family: 宋体">的|</span><font face="Times New Roman">iterator</font><span style="font-family: 宋体">提供的话Q则q回执行</span><font face="Times New Roman">iterator</font><span style="font-family: 宋体">的?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">include(object)/member: </font><span style="font-family: 宋体">数组中是否包?/span><font face="Times New Roman">object</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">inGroupsOf(number, fillWith): eachSlice</font><span style="font-family: 宋体">的变异版本,按照</span><font face="Times New Roman">number</font><span style="font-family: 宋体">对象分开Q如果分开后的数组的最后一个值的</span><font face="Times New Roman">length</font><span style="font-family: 宋体">于</span><font face="Times New Roman">number, </font><span style="font-family: 宋体">则用</span><font face="Times New Roman">fillwith</font><span style="font-family: 宋体">填充</span><font face="Times New Roman">, </font><span style="font-family: 宋体">例如</span><font face="Times New Roman">[1,2,3,4,5].inGroupsOf(3)=>[[1,2,3],[4,5,null]]</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">inject(memo, iterator): </font><span style="font-family: 宋体">注入</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">invoke(method): </font><span style="font-family: 宋体">调用</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">max(iterator): </font><span style="font-family: 宋体">最大?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">min(iterator): </font><span style="font-family: 宋体">最?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">partition(iterator): </font><span style="font-family: 宋体">分离</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">pluck(property): </font><span style="font-family: 宋体">采集</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">reject(iterator): </font><span style="font-family: 宋体">不合格的产品</span><font face="Times New Roman">, </font><span style="font-family: 宋体">?/span><font face="Times New Roman">findAll</font><span style="font-family: 宋体">相反</span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">sortBy(iterator): </font><span style="font-family: 宋体">Ҏ</span><font face="Times New Roman">iterator</font><span style="font-family: 宋体">排序Q如果调用的对象?/span><font face="Times New Roman">Array</font><span style="font-family: 宋体">的话Q直接调用内|的</span><font face="Times New Roman">sort(iterator)</font><span style="font-family: 宋体">p?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">toArray()/entries: </font><span style="font-family: 宋体">调用对象的每个值组成一个数l返?/span></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">zip(): </font><span style="font-family: 宋体">例如</span><font face="Times New Roman">[2,3,4].zip([5,6,7])=>[[2,5],[3,6],[4,7]], </font><span style="font-family: 宋体">如果最后一个参数类型ؓ</span><font face="Times New Roman">function</font><span style="font-family: 宋体">Q将q回</span><font face="Times New Roman">[iterator([2,5]),iterator([3,6]),iterator([4,7])],</font></font></p> <p style="margin: 0cm 0cm 0pt"><font size="3"><font face="Times New Roman">inspect(): Enumerable</font><span style="font-family: 宋体">对象的字W串表示</span></font></p> <img src ="http://www.tkk7.com/hummer008/aggbug/203707.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/hummer008/" target="_blank">hummer</a> 2008-05-29 09:37 <a href="http://www.tkk7.com/hummer008/articles/203707.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PrototypeQ?.5 rc2)使用指南Q二Q?(?http://www.tkk7.com/hummer008/articles/203706.htmlhummerhummerThu, 29 May 2008 01:36:00 GMThttp://www.tkk7.com/hummer008/articles/203706.htmlhttp://www.tkk7.com/hummer008/comments/203706.htmlhttp://www.tkk7.com/hummer008/articles/203706.html#Feedback0http://www.tkk7.com/hummer008/comments/commentRss/203706.htmlhttp://www.tkk7.com/hummer008/services/trackbacks/203706.html

(?/span>).PrototypeQ?/span>1.5 rc2)使用指南?/span>array.js

$A = Array.from(iterable): ?/span>iterable转化为数l,如果iterable定义?/span>toArrayҎQ就调用q个ҎQ否则利?/span>iterable?/span>length属性进行枚?/span>, 如果iterable没有length属性的话就q回I数l?/span>[]

Array对象除了扩展Enumerable对象的方法外Q另外扩展了如下的几个方法,

注意以下Ҏ除了clear外都不改变原来数l,而是q回一个新数组Q?/span>

clear(): 清除数组Q利?/span>arr.length=0

first(): q回W一个元?/span>

last()Q返回最后一个元?/span>

compact(): 去除数组中gؓnull?/span>undefined的元?/span>

flatten(): 数l扁q_Q例?/span>[3,4,[6,7]]变ؓ[3,4,6,7]

without(): 去除指定的元?/span>, 可以指定多个?/span>, 例如[4,56,7,8].without(4,7) q回[56Q?/span>8]

indexOf(object): q回指定的元素在数组中的索引Q不包含则返?/span>-1

reverse(inline)Q?/span>Array内置函数reverse的增强,?/span>inline?/span>trueӞ跟内|的reverse函数效果一P改变原数l的|否则不改变原来的?/span>

reduce(): 如果数组只有一个元素,则返回这个元素,否则q回数组本n

uniq(): q回没有重复元素的数l?/span>

clone(): q回一个跟数组相同的数l,Array中的toArrayҎ覆盖?/span>Enumerable中的toArrayҎQ指向了q个Ҏ

inspect(): 跟数l的toStringҎcMQ返回对象的字符串表C,例如[2,3].inspect() q回 "[2,3]"

(?/span>).PrototypeQ?/span>1.5 rc2)使用指南?/span>hash.js

Hash对象(兌数组)?/span>Prototype新徏的一个对象,要创Z?/span>Hash对象可以调用$H(object)ҎQ用这个方法将生成一个基?/span> object对象?/span>Hash对象Q生成的Hash对象?/span>object的属性名作ؓkeyQ将object的属性值最为键|因ؓjavascript本n的特?/span>(对象本n是兌数组) Q所以实?/span>Hash也很单,Prototype中的Hash只是javascript的关联数l?/span>(对象)而已

Prototype中的Hash对象l承?/span>Enumerable对象Q所以也hEnumerable对象的所有属性和ҎQ另外它h以下的方法:

keys(): q回hash的键值数l?/span>

values(): q回值得数组

merge(hash): 合ƈ两个hash

toQueryString(): ?/span>string?/span>toQueryParamsҎxQ将hash转化Z?/span>querystring, 会调?/span>encodeURIComponent寚w和D行编?/span>

inspect(): hash的字W串表示

因ؓhash只是javascript的一个普通的对象而已Q所以添加一个键值对使用Q?/span> hash[key]=value可以了Q删除一个键值对使用 detele hash[key]可以了

(?/span>).PrototypeQ?/span>1.5 rc2)使用指南?/span>range.js

Range对象是一个承自Enumerable?/span>"范围"对象Q你可以把它看成[x,x+1,x+2,x+3……x+n]的数l看待,但是比这L数组更节省存储空_因ؓrange对象只是保存x?/span>x+n而已

要创Z?/span>Range对象调用$R(start, end, exclusive) 函数可以了Q?/span>exclusive指定是否包含end本nQ如果没有指定或?/span>false则包?/span>endQ否则不包含,你可以利?/span>Enumerable中定义的Ҏ来操?/span>range对象Q?/span>range对象只是实现?/span>Enumerable对象需要的枚D逻辑_each和覆盖了includeҎ而已

(?/span>).PrototypeQ?/span>1.5 rc2)使用指南?/span>ajax

Prototype中的ajax.js提供了一个非常好用的ajax框架Q一般应用中单的调用以下代码可以了

new Ajax.Request(

    url, {method: “get”,

    onSuccess: showFilter,

    onFailure: function(request){alert(”Server error!”)},

    onException: showError}

 );

q个框架中提供了如下的对象和Ҏ{:

Ajax对象Q?/span>

只有一?/span>getTransportҎQ返回一?/span>XMLHttpRequest对象Q另外有一?/span>activeRequestCount属性,反映当前正在处理?/span>ajax数量

Ajax.Responders对象Q?/span>

l承?/span>EnumerableQ管理全局Ajax的请求,h如下Ҏ

register(responder)Q注册一个管?/span>ajaxh的对?/span>

unregister(responder)Q撤销一个管?/span>ajaxh的对?/span>

dispatch(callback, request, transport, json)Q触发注册的处理对象的方?/span>

q个对象一般很用,pȝ中已l用如下的代码注册了一个处理对?/font>

Ajax.Responders.register({

 onCreate: function() {

    Ajax.activeRequestCount++;

 },

 onComplete: function() {

    Ajax.activeRequestCount–;

 }

});

Ajax.Basec:

Ajax的基c?/span>, 只有一个方?/span>setOptions(options), 默认request参数如下Q你可以在新?/span>Ajax.request时指定:

method:       'post’,

asynchronous: true,

contentType: 'application/x-www-form-urlencoded’,

encoding:     'UTF-8,

Ajax.Requestc:

ajax主要的类Q承自ajax.basec,客户端?/span> new Ajax.Request(url,options) 调用Q?/span>options是一个对?/span>(兌数组), ?/span>options中可以指?/span>methodQ?/span>asynchronousQ?/span>contentTypeQ?/span>encodingQ?/span>parametersQ?/span> postBodyQ?/span>username,password{选项Q其?/span>parameters可以是字W传或者关联数l象Q?/span>

另外?/span>options中还可以通过requestHeaders指定request headsQ其?/span>requestHeaders可以是数l?/span>(例如[Connection,Close,aheadkey,aheadvalue])或一个关联数l;

options中最重要的选项是指定ajax的回调方法,可以定义onComplete, onSuccess, onFailure, onException(执行q程中发生异常调用的ҎQ主要ؓonComplete, onSuccess, onFailure{回调方法生的)Q甚臛_以定?/span>on404,on503q样的回调方法,它们的参Cؓ(transport, json),其中transport求的XMLHttpRequest对象, json?/span>evalJSON的结?/span>

如果q回的是一?/span>javascript文g(Ҏq回?/span>Content-type头判?/span>)会执行evalResponseҎQ另?/span>Ajax.Request对象q有一?/span>evalJSONҎQ取得文件的时候就会执?/span>

q个对象的方法列表如下:

request(url) : 发送请求,new的时候就已经调用了,所以一般不需要?/span>

success(): 判断request是否成功?/span>

getHeader(name)Q根?/span>name得到request head

evalJSON(): 执行getHeader(X-JSON),q返回结?/span>

evalResponse(): 执行q回?/span>responseTextq返?/span>

Ajax.Updaterc?/span>:

l承?/span>Ajax.RequestQ只是比Ajax.Request增加了更?/span>html元素的功能,一般用方法是new Ajax.Updater(element, url, options), element可以是一个元素,也可以是{success:e1,failure:e2}q种形式,

默认情况下不会执行返回结果中?/span>javascriptQ如果你先执行,你可以指?/span>options中的evalScripts?/span>true

默认情况下是替换指定元素的内容,如果你希望是dQ可以指?/span>options?/span>insertion参数, insertion是一?/span>Insertion.Before?/span>Insertion.Top?/span>Insertion.Bottom?/span> Insertion.After(dom.js中介l?/span>)

Ajax.PeriodicalUpdaterc?/span>:

l承?/span>Ajax.BaseQ周期性的更新一?/span>html元素的内容,q个cM调用Ajax.Updater?/span>html元素q行周期性的更新Q用方法ؓnew Ajax.PeriodicalUpdater(container, url, options), 参数?/span>Ajax.Updater差不多,其中options可以讄frequency(默认?/span>2)Q?/span>decayQ?/span>decay指的是当h的内Ҏ有变化的时候,frequency需要g长的倍数Q默认是1Q例如如?/span>decay设ؓ2Q?/span>frequency设ؓ3而内容一直没有变化,则请求的旉依次会变?/span> 3,6,12,24{?/span>

start(): 开始更?/span>, 初始化的时候会自动调用

stop(): 停止更新



hummer 2008-05-29 09:36 发表评论
]]>
վ֩ģ壺 㽶ѾƷƵ| ͵͵޸| ޳˾Ʒþ| 3Dһ| LƷþ | ؼavëƬѹۿ| ޹Ʒþ| Ʒձ777| ҹѹۿ| ŷһëƬѿ| www.xxxx.comձ| þø߳һëƬ| պƵ| һۿƵ߲| ߹ۿվ| ѿƬ| av˾Ʒ| Ʒ޾Ʒպѷ| ɫĻAV| þ޾ƷƷ| avר| ޾Ʒþþþ| ۺһ| ŮڵƵ | 91ۺ| ޸ҹӰ| ˳ɴƬ߹ۿ| VƬ߲| ձһ| **aaaaaëƬ| ؼƬѸƵ| ޹һɾþþƷۺ | ھƷһëƬѿ| aѹۿ| ɬɬƵ| Ƭһ| þþƷAVý| Ƶվ| ޾Ʒһۺ| þùѾƷ| ëƬaëƬѲ100|