标签:prototype cti 避免 函数 nbsp 应该 eof bsp log
1 var lev=function(){ 2 return "嘿哈"; 3 }; 4 function Parent(){ 5 var Child = new object(); 6 Child.name = "李小龙"; 7 Child.age = "30"; 8 Child.lev = lev; 9 return Child; 10 11 }; 12 var x=Parent(); 13 alert(x.name); 14 alert(x.lev());
说明:
1 var lev=function(){ 2 return "嘿哈"; 3 }; 4 function Parent(){ 5 this.name = "李小龙"; 6 this.age = "30"; 7 this.lev = lev; 8 }; 9 var x=Parent(); 10 alert(x.name); 11 alert(x.lev());
说明:
1 var lev=function(){ 2 return "嘿哈"; 3 }; 4 function Parent(){ 5 Parent.prototype.name = "李小龙"; 6 Parent.prototype.age = "30"; 7 Parent.prototype.lev = lev; 8 }; 9 var x=Parent(); 10 alert(x.name); 11 alert(x.lev());
说明:
1 function Parent(){ 2 this.name = "李小龙"; 3 this.age = "30"; 4 }; 5 Parent.prototype.lev=function(){ 6 return this.name; 7 } 8 var x=Parent(); 9 alert(x.name); 10 alert(x.lev());
说明:
1 function Parent(){ 2 this.name = "李小龙"; 3 this.age = "30"; 4 if(typeof Parent.lev == "undefined"){ 5 Parent.prototype.lev = function(){ 6 return this.name; 7 } 8 Parent.lev = true; 9 } 10 }; 11 12 var x=Parent(); 13 alert(x.lev());
说明:
if(typeof Parent.lev == "undefined"){
Parent.prototype.lev = function(){
return this.name;
}
Parent.lev = true;
} 从而保证创建该对象的实例时,属性的方法不会被重复的创建。
标签:prototype cti 避免 函数 nbsp 应该 eof bsp log
原文地址:http://www.cnblogs.com/liuhongli/p/6411479.html