易截截图软件、单文件、免安装、纯绿色、仅160KB

[整理]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种方式是目前最广泛使用的,尽量使用它们避免不必要的问题。


相关文档:

JavaScript constructor属性

Definition and Usage
定义与用法The constructor property is a reference to the function that created an object.
constructor属性是所建立对象的函数参考Syntax
语法object.constructor
Example 1
举例
In this example we will show how to use the constructor property:
在这个举例中我们将展示如何使用cons ......

javascript一段时间代码

<SCRIPT language=javaScript>
<!--
now = new Date(),hour = now.getHours()
if(hour < 6){document.write("凌晨好!")}
else if (hour < 9){document.write("早上好!")}
else if (hour < 12){document.write("上午好!")}
else if (hour < 14){document.write("中午好!")}
else if (hour &l ......

JavaScript常用方法总结——判断浏览器类型

判断浏览器类型
<SCRIPT language=javascript>
if(navigator.appVersion.indexOf("MSIE 6.") != -1 ){
window.location = "111.htm" ;
}
</SCRIPT>
按钮背景图片替换
<input type=button style="background-image:url(a.gif)" value=test onmouseover="this.style.backgroundImage='url(b.gif ......

JavaScript_判断网络图片是否存在

//探测图片是否存在
function IsExist(url)
{
x = new ActiveXObject("Microsoft.XMLHTTP")
x.open("HEAD",url,false)
x.send()
return x.status==200
}
判断某一个网络地址的图片是否存在,如果存在,就会返回true ,不存在就返回 false~~~~ ......

JavaScript的function

函数是进行模块化程序设计的基础,编写复杂的Ajax应用程序,必须对函数有更深入的了解。
  javascript中的函数不同于其他的语言,每个函数都是作为一个对象被维护和运行的。通过函数对象的性质,可以很方便的将一个函数赋值给一个变量或者将函数作为参数传递。在继续讲述之前,先看一下函数的使用语法:
以下是引用片段 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号