查看: 802|回复: 1
|
Prototype.js 1.5推出了
[复制链接]
|
|
如果你还不知道prototype.js这个冬冬的存在,你一定要看看。Prototype.js是一个javascript framework,利用它来编写你的js程式码,将会事半功倍。Prototype.js强化了js的许多类型,包括DOM Element,Array,String,Number,Enumerable,class等等,还加入了Ajax物件。还有,如果你接触过Ruby的话,你会惊喜地发现编写Javascript就好像在编写Ruby这样,程式码可以很简捷。来看看下面的例子:
- <#String>
- "chinese-cari".camelize(); -> "chineseCari"
- "<script>4+2</script>".evalScript(); -> [6]
- "Hello World!".gsub("!"," Cari!"); -> "Hello World Cari!"
- <#Array>
- ["david","alan","bill","steve"].each(function(name){
- alert(name);
- });
- ["dave",["david","alan",["alan"]],"Lawrence"].flatten(); ->["dave","david","alan","alan","Lawrence"]
- <#Hash>
- var myHash = new Hash({
- name:"Michael",
- occupation:"Doctor",
- age:13
- });
- myHash["name"]; -> "Michael"
- myHash.keys(); ->["name","occupation","age"]
- myHash.toQueryString(); -> "name=Michael&occupation=Doctor&age=13"
- <Function>
- //binding object function
- var obj = {
- name:"David",
- fx:function(){
- alert(this.name+", "+arguments[0]);
- }
- }
- var fx2 = obj.fx.bind(obj);
- fx2("Nice to meet you!"); -> "David, Nice to meet you!"
- <Element>
- $('test') equivalent to document.getElementById('test')
- $('special') <-- css class element collector. get all element with class name 'special'
- $A($('special')) <-- cast the element collection into Enumerable, can access to most of the enumerable methods
- ["div1","div2","div3"].each(Element.hide); <-- set the style of display to "none" of all the elements with id in the array
- <Ajax!!>
- URL = 'http://www.google.com/search?q=Prototype';
- new Ajax.Request('/proxy?url=' + encodeURIComponent(URL), {
- method: 'get',
- onSuccess: function(transport) {
- var notice = $('notice');
- if (transport.responseText.match(/<a class=l href="http:\/\/prototypejs.org/))
- notice.update('Yeah! You are in the Top 10!').setStyle({ background: '#dfd' });
- else
- notice.update('Damn! You are beyond #10...').setStyle({ background: '#fdd' });
- }
- });
复制代码
更多说明,参考prototype.js的API Doc
http://prototypejs.org/api/
|
|
|
|
|
|
|
|
发表于 27-1-2007 06:47 PM
|
显示全部楼层
好像很少人董。
我也不懂有这样好的东西
等下去看看
吃饭先 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|