<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js对象的创建7</title>
<meta name="author" content="ASUS" />
<!-- Date: 2014-11-13 -->
this.name = name;
this.age = age;
this.friends = friends;
/*
Person.prototype = {
constructor : Person,
sayHello : function(){
alert(this.name+","+this.age+",["+this.friends+"]");
}*/
//判断Person.prototype.sayHello是否已经存在 ,如何不存在就会创建,反之不创建
if(!Person.prototype.sayHello){
Person.prototype = {
constructor : Person,
sayHello : function(){
alert(this.name+","+this.age+",["+this.friends+"]");
}
}
}
}
var p1 = new Person("gui1",20,["aa","bb"]);
var p2 = new Person("gui2",22,["cc","dd"]);
p2.sayHello();
alert(p1.sayHello==p2.sayHello);//false
alert(p1.prototype==p2.prototype);//true
</script>
</head>
<body>
</body>
</html>
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js对象的创建7</title>
<meta name="author" content="ASUS" />
<!-- Date: 2014-11-13 -->
<script type="text/javascript">
function Person(name,age,friends){this.name = name;
this.age = age;
this.friends = friends;
/*
Person.prototype = {
constructor : Person,
sayHello : function(){
alert(this.name+","+this.age+",["+this.friends+"]");
}*/
//判断Person.prototype.sayHello是否已经存在 ,如何不存在就会创建,反之不创建
if(!Person.prototype.sayHello){
Person.prototype = {
constructor : Person,
sayHello : function(){
alert(this.name+","+this.age+",["+this.friends+"]");
}
}
}
}
var p1 = new Person("gui1",20,["aa","bb"]);
var p2 = new Person("gui2",22,["cc","dd"]);
p2.sayHello();
alert(p1.sayHello==p2.sayHello);//false
alert(p1.prototype==p2.prototype);//true
</script>
</head>
<body>
</body>
</html>