标签:
<!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>js01_hello</title>
<meta name="author" content="Administrator" />
<script type="text/javascript">
/**
*基于原型的创建虽然可以有效的完成封装,但是依然有一些问题
* 1、无法通过构造函数来设置属性值
* 2、当属性中有引用类型变量是,可能存在变量值重复
*/
function Person(){
}
Person.prototype = {
constructor:Person,
name:"Leon",
age:30,
friends:["Ada","Chris"],
say:function() {
alert(this.name+"["+this.friends+"]");
}
}
var p1 = new Person();
p1.name = "John";
p1.say();//john[ada,chris]
//会在原型中找friends,所以Mike是在原型中增加的
p1.friends.push("Mike");//为p1增加了一个朋友
var p2 = new Person();
//此时原型中就多了一个Mike,这就是原型所带来的第二个问题
p2.say();//leon ada chris mike
</script>
</head>
<body>
</body>
</html>
标签:
原文地址:http://www.cnblogs.com/aicpcode/p/4281548.html