创建对象和方法的三种方式如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gbk"/>
<title>面向对象用法</title>
<script type="text/javascript">
var s = {};//方法一
s.name = "wws";
s.age = 32;
s.show = function(){
alert(s.name+":"+s["age"]);
};
s.show();
var s = {//方法二
name : "wws",
age : 32,
show : function(){
alert(s.name+":"+s.age);
}
};
s.show();
var s = new Object();//方法三
s.name = "wws";
s.age = 32;
alert(s.name);
</script>
</head>
<body>
</body>
</html>
其代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gbk"/>
<title>原型对象用法</title>
<script type="text/javascript">
function Person(name,age){
this.name = name;
this.age = age;
this.show = function(){
//alert("aaa");
alert(this.name+":"+this.age);
};
}
var s = new Person("wws",32);
var d = new Person("wang",22);
//alert(s.name);
//s.show();
//d.show();
//alert(s.constructor==Person);
alert(s instanceof Person)
</script>
</head>
<body>
</body>
</html>