面向过程是是一种以过程为中心的编程思想;
面向对象主要是把事物给对象化。
1 var obj={ 2 name:"gfhs"; // 属性 3 show:function( ){ alert(this.name); } //方法
1 var obj=function( ){ // 构造一个类 2 } // 功能不一样 类 构造函数 3 obj.prototype.showNmae=function(){ alert(this.name); }; //原型 4 var yue=new obj( ); //给类添加对象 5 yue.name="gfhf"; //给类添加属性
1 var obj=function( ){ 2 this.name = name; 3 this.show= function(){alert(this.name); } 4 var yue = new obj("wy");//创建对象并赋值
1 var Person=function (name,age){ 2 //功能:构造一个类(构造函数) 3 this.name=name; 4 this.age=age; 5 this.showName=function (){ 6 alert(this.name); 7 } 8 };//功能:类,构造函数 9 var tang=new Person("tang","18"); 10 var yue=new Person("yue","18"); 11 Person.prototype.sex="man"; 12 Person.prototype.showSex=function (){ 13 alert(this.sex); 14 } 15 tang.showSex(); 16 yue.showSex(); 17 //添加属性 18 obj.sex="girl"; 19 //添加方法 20 obj.showSex=function(){ alert(this.sex); };
View Code1 String.prototype.index="哈哈"; //对象名.prototype.属性=值; 2 String.prototype.showIndex=function (){ //对象名.prototype.方法名=function( ){ }; 3 4 5 alert(this.index); 6 }
7、原型的使用方式
1 Date.prototype.getWeek=function (){ 2 3 var arr=["日","一","二","三","四","五","六"]; 4 var oDay=this.getDay(); 5 // alert("星期"+arr[oDay]); 6 return "星期"+arr[oDay]; 7 } 8 var newDate=new Date(); 9 alert(newDate.getWeek());
arr.show1=show;arr.show1(); //arr
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <style type="text/css"> 7 div{ 8 height: 100px; 9 width: 100px; 10 background-color: #ccc; 11 display: none; 12 } 13 </style> 14 <script type="text/javascript"> 15 window.onload=function (){ 16 var aBtn=document.getElementsByTagName("input"); 17 var aDiv=document.getElementsByTagName("div"); 18 for (var i = 0; i < aBtn.length; i++) { 19 aBtn[i].index=i; 20 aBtn[i].onclick=function (){ 21 22 for (var i = 0; i < aBtn.length; i++) { 23 aDiv[i].style.display="none"; 24 }; 25 aDiv[this.index].style.display="block"; 26 } 27 }; 28 } 29 </script> 30 </head> 31 <body> 32 <input type="button" value="按钮1"> 33 <input type="button" value="按钮2"> 34 <input type="button" value="按钮3"> 35 <div style="display:block">块1</div> 36 <div>块2</div> 37 <div>块3</div> 38 </body> 39 </html>
1 function Tab(){ 2 this.aBtn=document.getElementsByTagName("input"); 3 this.aDiv=document.getElementsByTagName("div"); 4 5 for (var i=0; i<this.aBtn.length; i++){ 6 7 this.aBtn[i].index=i; 8 this.aBtn[i].onclick=function (){ 9 10 for (var i = 0; i < aBtn.length; i++) { 11 aDiv[i].style.display="none"; 12 }; 13 aDiv[this.index].style.display="block"; 14 } 15 } 16 }
1 Tab.prototype.fnClick=function (that){ 2 for (var i = 0; i < this.aBtn.length; i++) { 3 this.aDiv[i].style.display="none"; 4 }; 5 this.aDiv[that.index].style.display="block"; 6 }
1 function Tab(){ 2 3 this.aBtn=document.getElementsByTagName("input"); 4 this.aDiv=document.getElementsByTagName("div"); 5 6 var _this=this; 7 8 for (var i=0; i<this.aBtn.length; i++){ 9 10 this.aBtn[i].index=i; 11 this.aBtn[i].onclick=function (){ 12 _this.fnClick(this); 13 } 14 } 15 }
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <style type="text/css"> 7 div{ 8 height: 100px; 9 width: 100px; 10 background-color: #ccc; 11 display: none; 12 } 13 </style> 14 <script type="text/javascript"> 15 Tab.prototype.fnClick=function (that){ 16 for (var i = 0; i < this.aBtn.length; i++) { 17 this.aDiv[i].style.display="none"; 18 }; 19 this.aDiv[that.index].style.display="block"; 20 } 21 22 function Tab(){ 23 24 this.aBtn=document.getElementsByTagName("input"); 25 this.aDiv=document.getElementsByTagName("div"); 26 27 var _this=this; 28 29 for (var i=0; i<this.aBtn.length; i++){ 30 31 this.aBtn[i].index=i; 32 this.aBtn[i].onclick=function (){ 33 _this.fnClick(this); 34 } 35 } 36 } 37 38 window.onload=function (){ 39 new Tab(); 40 } 41 </script> 42 </head> 43 <body> 44 <input type="button" value="按钮1"> 45 <input type="button" value="按钮2"> 46 <input type="button" value="按钮3"> 47 <div style="display:block">块1</div> 48 <div>块2</div> 49 <div>块3</div> 50 </body> 51 </html> 52
面向过程编程到面向对象编程的转换实例,布布扣,bubuko.com
原文:http://www.cnblogs.com/Hfive/p/3586739.html