网络编程
位置:首页>> 网络编程>> JavaScript>> prototype.js1.4版开发者手册(7)

prototype.js1.4版开发者手册(7)

作者:THIN 来源:cnblogs 发布时间:2007-09-30 14:09:00 

标签:prototype,手册

prototype.js参考


JavaScript类扩展

prototype.js 类库实现强大功能的一种途径是扩展已有的JavaScript 类。


对Object的扩展


MethodKindArgumentsDescription
extend(destination, source)
destination: any object, source: any object提供一种通过拷贝所有源以象属性和函数到目标函数实现继承的方法
inspect(targetObj)statictargetObj: any object返回可读性好关于目标对象的文字描述,如果对象实例没有定义一个inspect函数,默认返回toString函数的值。



对Number的扩展


Method
ArgumentsDescription
toColorPart()
(none)返回数字的十六进制表示形式。在把一个RGB数字转换成HTML表现形式时很有用。
succ()instance(none) 返回下一个数字,这个方法可用于迭代调用场景中。
times(iterator)instanceiterator: a function object conforming to Function(index)Calls the iterator function repeatedly passing the current index in the index argument. 反复调用iterator函数并传递当前index到iterator的index参数。


下面的例子用提示框显示0-9。

 

<script> function demoTimes(){ var n = 10; n.times(function(index){ alert(index); }); /*************************** * you could have also used: *           (10).times( .... ); ***************************/ } </script> <input type=button value="Test Number.times()" onclick="demoTimes()">


对Function扩展


MethodKindArgumentsDescription
bind(object)
object: the object that owns the method返回function的实例,这个实例和源function的结构一样,但是它已被绑定给了参数中提供的object,就是说,function中的this指针指向参数object。
bindAsEventListener(object)instanceobject: the object that owns the method用法和上面的bind一样,区别在于用来绑定事件。


让我们看看如何运用这些扩展。

 

<input type=checkbox id=myChk value=1> Test? <script> //declaring the class var CheckboxWatcher = Class.create(); //defining the rest of the class implementation CheckboxWatcher.prototype = { initialize: function(chkBox, message) { this.chkBox = $(chkBox); this.message = message; //assigning our method to the event  this.chkBox.onclick = this.showMessage.bindAsEventListener(this);  }, showMessage: function(evt) { alert(this.message + ’ (’ + evt.type + ’)’); } }; var watcher = new CheckboxWatcher(’myChk’, ’Changed’); </script>

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com