<head></head>
<script language="javascript">
function MyClass(initVar1,/*Object type*/ initVar2/*int type*/){
this.initVar1 = initVar1;
this.initVVar2 = initVar2;
this.otherVars = new Array("first","second");//this var has default values
if(typeof MyClass._initialized == "undefined"){
//MyClass.prototype.otherVars = new Array("first","second");
alert("hehe");
};
MyClass._initialized = true;
};
var myClass = new MyClass("initvar1",11);
myClass.otherVars.push("others...");
alert(myClass.otherVars);
</script>
a demo for using js:: define an Object
也可以选择使用literal 来 create 一个Object,语法如下:
var noPropertyObj = {}//定义一个Object,该Object没有任何属性
var hasPropertyObj = {name:stuinfo,address:www.chsi.com.cn}//定义一个对象,
//包含name和address两个属性
用literal create的对象和使用function不同之处在于其不能使用:var myObj = new noPropertyObj();
访问某一个property时可以这样使用: alert(hasPropertyObj.address);
加入方法:修改hasPropertyObj,为其加入show()方法来显示address属性
var hasPropertyObj = {name:stuinfo,address:www.chsi.com.cn,show:function(){alert(this.address)}}
调用此方法:hasPropertyObj.show();