[整理]JavaScript最流行的2种定义类的方式
转自:http://www.cnblogs.com/greki/archive/2009/06/02/1494863.html
其它方式:工厂方式,构造函数方式,原型方式都各有各的大缺陷,这里就不一一介绍了,想了解的可以去看一下这本著作的第3章节。
1. 混合构造函数/原型方式
function Car(sColor, iDoors, iMpg) {
this .color = sColor;
this .doors = iDoors;
this .mpg = iMpg;
this .drivers = new Array(“Mike”, “Sue”);
}
Car.prototype.showColor = function () {
alert( this .color);
};
var oCar1 = new Car(“red”, 4 , 23 );
var oCar2 = new Car(“blue”, 3 , 25 );
oCar1.drivers.push(“Matt”);
alert(oCar1.drivers); // outputs “Mike,Sue,Matt”
alert(oCar2.drivers); // outputs “Mike,Sue”
优点:具有其它方式的优点而没有其它方式的缺点
不足:封装性欠缺
2 . 动态原型方式
function Car(sColor, iDoors, iMpg)
{
this .color = sColor;
this .doors = iDoors;
this .mpg = iMpg;
this .drivers = new Array(“Mike”, “Sue”);
if ( typeof Car._initialized == “undefined”)
{
Car.prototype.showColor = function ()
{
alert( this .color);
} ;
Car._initialized = true ;
}
}
优点:封装性比上一个方式更好
不足:就是看上去奇怪一点,呵呵
总之,以上2种方式是目前最广泛使用的,尽量使用它们避免不必要的问题。
相关文档:
IE可以调用:
<script type="text/javascript">
// 说明:获取页面上选中的文字
// 整理:http://www.CodeBit.cn
function getSelectedText() {
if (window.getSelection) {
// This technique is the most likel ......
原文:《Speeding
up JavaScript: Working with the DOM》
作者: KeeKim Heng, Google Web Developer
在我们开发互联网富应用(RIA)时,我们经常写一些javascript脚本来修改或者增加页面元素,这些工作最终是DOM——或者说文档对象模
型——来完成的,而我们的实现方式会影响到应用的响应速度。 ......
最经使用jquery.form.js做了一个查询页面,在搜索出结果后,需要分页,在分页中点击下一页页才用ajax提交,在提交没有返回结果是需要给客户显示一下正在加载数据,故用jquery.1.3.2.js做了一个信息提示的脚本。
代码如下:
<div id="loadProcess" style="z-index:1;visibility:hidden;width:300px;position:absolute;t ......
//探测图片是否存在
function IsExist(url)
{
x = new ActiveXObject("Microsoft.XMLHTTP")
x.open("HEAD",url,false)
x.send()
return x.status==200
}
判断某一个网络地址的图片是否存在,如果存在,就会返回true ,不存在就返回 false~~~~ ......