原型设计模式prototype-构造js自己定义对象

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
/*
 * 原型模式:
 	prototype 原型属性
 	对象的原型拥有的属性。该对象的实例对象也会同一时候拥有
 */
 function Test(){
	
}
Test.age=20;
var tt=new Test();//这样的情况age属性不会再new时候被加入哦
alert(tt.age)//undefine
//--------------------
 function Test(){
	
}
Test.prototype.age=20;//指定原型有的属性
var tt=new Test();
alert(tt.age)//这样就有了值,非undefine了
//-------------------------
function Student(){};
alert(Student.prototype);
for(pro in Student.prototype){
	alert(pro);//弹出为空,默认该对象没有不论什么属性
}
Student.prototype.name="黑马";
Student.prototype.age=3;
Student.prototype.getName=function(){
	return this.name;
}

var stu1=new Student();
//stu1.name="传智"//会覆盖原型属性中的值
delete stu1.name;//删除对象实例的属性,原型属性还是存在的哦。
alert(stu1.name);
</script>
</head>
<body>

</body>
</html>

原文地址:https://www.cnblogs.com/gccbuaa/p/7094517.html