prototype1.6和mootools1.2.3在本人目前工作中比较常用的方法比较

由于目前在项目中比较常用到类继承、事件绑定、Ajax方法,下面是在prototype1.6和mootools1.2.3中这几个实现的比较:
prototype1.6.js
<HTML>
<HEAD>
<title>prototype</title>
<script type="text/javascript" src="prototype.js"></script>
<SCRIPT LANGUAGE="JavaScript">
var C=Class.create({
	a:null,
	b:null,
	initialize:function(a,b){
		this.a=a;
		this.b=b
	},
	speak:function(){
		alert(this.a+" "+this.b);
	}
});
var D=Class.create(C,{
	c:null,
	initialize:function(a,b,c){
		C.prototype.initialize.apply(this,[a,b]);
		this.c=c;
	},
	speak:function(){
		//C.prototype.speak.apply(this);
		//alert(this.a+" "+this.b+" "+this.c);
		var url="http://192.168.1.62:8080/WEB-T_V1.1.43I/query.do";
		new Ajax.Request(url,{
			method:"Get",
			parameters:{
				serviceType:"poiCityService",
				r:0.6617610317731282
			},
			onSuccess:this.callback,
			onFailure:this.error
		});
	},
	callback:function(res){
		var text=res.responseText;
		//var obj=eval('('+text+')');
		var obj=text.evalJSON(true);
		alert(obj.provinceList.length);
	},
	error:function(){
		alert("error!");
	}
});
var Obj={
	a:"a",
	b:"b",
	c:"C"
};
function Ttt(){
	alert(this.a+" "+this.b+" "+this.c);
};
(Ttt.bind(obj))();

var speaker=new D("I","speak","out.");

window.onload=function(){
	Event.observe($('fg'),"click",speaker.speak.bind(speaker));
}
</SCRIPT>
</HEAD>
<BODY>
<button id="fg">test</button><BR>
<div id="data"></div>
</BODY>
</HTML>


mootools1.2.3.js

<HTML>
<HEAD>
<title>mootools</title>
<script type="text/javascript" src="mootools-1.2.3-core.js"></script>
<SCRIPT LANGUAGE="JavaScript">
var Speaker=new Class({
	a:null,
	b:null,
	initialize:function(a,b){
		this.a=a;
		this.b=b;
	},
	speak:function(){
		alert(this.a+" "+this.b);
	}
});
var BigVioceSpeaker=new Class({
	Extends:Speaker,
	c:null,
	initialize:function(a,b,c){
		//Speaker.prototype.initialize.apply(this,[a,b]);
		this.parent(a,b);
		this.c=c;
	},
	speak:function(){
		alert(this.a+" "+this.b+" "+this.c);
	}

});
var Some=new Class({
	some:null,
	initialize:function(a){
		this.some=a;
	},
	say:function(str){
		alert(str.totalpage);
	}
});
var SomeBigVoiceSpeaker=new Class({
	Implements:[Some,BigVioceSpeaker],
	initialize:function(a,b,c,d){
		BigVioceSpeaker.prototype.initialize.apply(this,[a,b,c]);
		Some.prototype.initialize.apply(this,[d]);
	},
	speak:function(){
		//alert(this.a+" "+this.b+" "+this.c+" "+this.some);
		var url="http://localhost:8888/webt/query.do";
		new Request.JSON({
			url:url,
			//update:$("data"),
			data:{serviceType:"poiService",
				acode:"110000",
				page:3,
				name:"%E7%9F%A5%E6%98%A5"
			},
			method:"get",
			onSuccess:this.say
		}).send();
	}
});
var speaker=new SomeBigVoiceSpeaker("I","speak","some","big voice");
//speaker.speak();
//speaker.say();
window.onload=function(){
	var obj=$("fg");
	obj.addEvent("click",speaker.speak.bind(speaker));	
}
</SCRIPT>
</HEAD>
<BODY>
<button id="fg">test</button><BR>
<div id="data"></div>
</BODY>
</HTML>


可以看出他们在实现上差异不大。prototype没有支持多继承,mootools支持但是对初始化成员的方法好像没有,但继承可以调用this.parent();在多继承下不可用,这里采用变通的方法Class.prototype.initialize.apply();初始化各个类。

你可能感兴趣的:(C++,c,工作,prototype,C#)