标签:
<!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">
/**
* 原型是js中非常特殊一个对象,当一个函数创建之后,会随之就产生一个原型对象
* 当通过这个函数的构造函数创建了一个具体的对象之后,在这个具体的对象中
* 就会有一个属性指向原型
*/
//第一种状态
function Person(){
}
/**
*使用如下方式来编写代码,当属性和方法特别多时,编写起来不是很方便,可以通过json的格式
* 来编写
*/
// Person.prototype.name = "Leon";
// Person.prototype.age = 23;
// Person.prototype.say = function() {
// alert(this.name+","+this.age);
// }
/**
* 以下方式将会重写原型
* 由于原型重写,而且没有通过Person.prototype来指定
* 此时的constructor不会再指向Person而是指向Object
* 如果constructor真的比较重要,可以在json中说明原型的指向
*/
Person.prototype = {
constructor:Person,//手动指定constructor
name:"Leon",
age:23,
say:function() {
alert(this.name+","+this.age);
}
}
var p1 = new Person();
p1.say();
alert(p1.constructor==Person);
</script>
</head>
<body>
</body>
</html>
标签:
原文地址:http://www.cnblogs.com/aicpcode/p/4281542.html