原型设计模式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>

原型设计模式prototype-构造js自定义对象,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。