面向對象與原型 第一天

                 

一.建立對象javascript

                 var box=new Object();                     //建立對象java

                 box.name= 'lee';                          //t添加屬性函數

                  box.run=function(){                   //添加方法this

                   return  this.name;                    box 做用域下的方法this
                }spa

                

                 var    box1=box;prototype

                  box1.name='KKK'; code

                 box1.run=function(){                   //添加方法對象

                    return  this.name;                    box 做用域下的方法this
                }blog

               alert(box.run());                       //輸出都是kkK;ip

               alert(box1.run);

             覆蓋了原來的box,因此引起了工廠模式,集中實例化

 

二.  工廠模式(集中實例化函數)

                 function createoject(name){

                      var  obj=new Object();

                      obj.name=name;

                     obj.run=function(){ 

                       return this.name;
             }

                             return           obj;
   } 
                       

             var box1=createobject('lll'); 

             var box2=createobject('kkk'); 

             alert(box.run());                                                      

             alert(typeof box 1)

             alert(typeof box2)             都是object      

             alert(box1 instanceof Object)                都是true                          

 

                   工廠模式解決了大量實例化的問題  可是沒法識別是哪一個對象的實例   

  3、構造函數

            function Box(name){

           this.name=name                                                                           1.構造函數沒有 new Object

            this.run=function(){                                                                     自動會生成          var  obj=new Object();

                    return this.name;                                                                 2.this 至關於obj
             };                                                                                                3 .沒有rerturn 語句 不須要返回引用
};                                                                                                             4.必須首字母大寫   

                             var box1=new Box('LLL');

                             var box2=new Box('kkk');                                                    

                              alert(box1.run());

                             alert(box2.run());

                               alert(typeof box 1)    Object

                               alert(box1 instanceof Object)             //   都是true  

                                alert(box1  instanceof Box)                        // true  

                                alert(box2 instanceof Box)    

                       構造函數 解決了對象的識別問題       

                          對象冒充       解決了建立新的對象調用裏面函數的問題

                             var o=new Object();

                            Box.call(o,'JJ') ;

                            alert(o.run());      

                     


                            alert(box1.run()==box2.run())                              構造函數體內方法的值是相等的

                            alert(box1.run==box2.run)                    false    比較的是實例化後的地址  不同    (引用類型)

                             

                    function Box(name){

 

                    this.name=name                                                                       

 

                     this.run=run                                                                                       

     };                  

                     function run(){

                     retrun this.name;
}                     

                                                        把函數放在全局後  實例化的地址同樣  但同時引起了惡意調用

                                                                     alert(run());                          直接調用


四.原型 

          咱們建立的每個函數都有prototype原型屬性,這個屬性就是一個對象,用途就是實例共享屬性和方法  也就是說沒必要在構造函數裏定義屬性和方法,直接使用其餘實例的屬性和方法  。

              function Box(){}

             Box.prototype.name='lee';                                         //原型屬性

            Box.prototype.run=function(){                                   、、原型方法
                   return this.name;

             }

              var box1=new Box()  ;

              var box2=new Box()  ;

              alert(box1.run());

               alert(box1.run==box2.run)                   共享地址是同樣的

              

               alert (box1.prototype)     undefine

               alert(box1._proto_)             object

               alert(box1.construct0r)       能夠獲取構造函數自己   

                判斷1一個對象實例是否是指向了原型對象

                 基本上實例化了就是自動指向

                  alert(Box.prototype.isPrototypeof(box1))      

                  var o=new object();      

                 alert(Box.prototype.isPrototypeof(0))                    false;

               原型模式的執行流程

               先找實例裏面的再找原型裏面的

               實例屬性不會共享的 知會共享原型屬性

                     delete  實例屬性    delete box.name;

                                原型屬性       delete box.prototype.name;

               判斷2    判斷實例中是否含有實例屬性

                      hasOwnProperty  

                  alert(box1.  hasOwnProperty('name');

              判斷3     判斷實例和原型是否含有屬性

                     alert('name' in box1)  

  使用字面量的方式建立原型對象

            function Box(){}

 Box.prototype={ constructor Box; name:'KKK'; run:{ return this.name;
} }

Box.prototype={} 重寫原型對象 切斷與原來原型對象的關係
string.prototype.addstring=function(){
return 'this 被添加'

}                                                 //內置引用類型添加方法

                      alert('Lee'.addstring());

 

  原型模式出現了構造傳參數和共享問題


五. 組合構造模式和原型模式

   動態原型模式

function Box(name){ this.name=name; } if (typeof this.run!='function'){ Box.prototype.run=function(){ return this.name; } }

六.寄生構造模式  工廠模式+構造模式

function Box(name){
   var obj=new Object();
     obj.name=name;
  obj.run=function(){

          return this.name;

}
     return obj;

}

 


七.穩妥構造模式 禁止使用NEW 和this

相關文章
相關標籤/搜索