js面向對象實現面向對象(一)

前言

衆所周知周知,js是一個面向過程的弱類型動態語言,可是在開發的過程當中爲了良好的封裝和功能的實現咱們須要面向對象,好比靜態類型方法,公有私有變量,方法,繼承,多態等等,js能夠用本身的「騷操做去實現」。而js的面向對象是創建在它是依靠原型鏈實現的。javascript

類和實例屬性方法,私有屬性方法

new 關鍵詞的做用,構造函數java

  • 建立一個空對象,並使該空對象繼承Func.prototype;
  • 執行構造函數,並將this指向剛剛建立的新對象;
  • 返回新對象
function Super(name,age) {
  this.name= name;//實例屬性
  this.age = age;
  //實例方法
  this.show = function() {
    console.log(name,age)
  }
  // 實例私有屬性
  var height = 180;
  // 實例私有方法 
  function _height() {
         console.log(height)
  }   
}
var boy = new Super('yzg',26);
boy.show() // yzg,26
複製代碼

如上所示,就能給對象新建一個類Super構造函數,有實例方法和屬性和私有方法屬性閉包

靜態私有屬性和方法

這裏咱們利用閉包的原理,返回類。就能夠在閉包內創建靜態屬性和方法函數

var Super = (function() {
      var salary = 20000; //靜態私有屬性
      //靜態私有方法
      var showSalary = function(){
        console.log(salary)
      }
      var _Super = function(name,age) {
                 this.name= name; //實例屬性
                 this.age = age;  
                 //實例方法
                 this.show = function() {
                     console.log(name,age)
                  }
                  // 實例私有屬性
                var height = 180;
                // 實例私有方法 
                function _height() {
                       console.log(height)
                  }   
               }
      return _Super;
}())
複製代碼

實例公有方法和屬性

咱們能夠利用function的protptye實現公共方法和屬性ui

var Super = (function() {
      var salary = 20000; //靜態私有屬性
      //靜態私有方法
      var showSalary = function(){
        console.log(salary)
      }
      var _Super = function(name,age) {
                 this.name= name; //實例屬性
                 this.age = age;  
                 //實例方法
                 this.show = function() {
                     console.log(name,age)
                  }
                    // 實例私有屬性
                var height = 180;
                // 實例私有方法 
                function _height() {
                       console.log(height)
                  }   
               }
     //實例公有屬性 
     _Super.prototype.address='cn anhui';
     //實例公有方法 
     _Super.prototype.showAddress= function () {
                      console.log(this.address)
                   } 
      return _Super;
}())
複製代碼

靜態公有方法和屬性

相似於實例的公有方法和屬性,只不過此次咱們是直接將屬性綁定到構造函數上而不是掛在prototype上。this

var Super = (function() {
      var salary = 20000; //靜態私有屬性
      //靜態私有方法
      var showSalary = function(){
        console.log(salary)
      }
      var _Super = function(name,age) {
                 this.name= name; //實例屬性
                 this.age = age;  
                 //實例方法
                 this.show = function() {
                     console.log(name,age)
                  }
                    // 實例私有屬性
                var height = 180;
                // 實例私有方法 
                function _height() {
                       console.log(height)
                  }   
               }
      return _Super;
}())
複製代碼

實例公有方法和屬性

咱們能夠利用function的protptye實現公共方法和屬性spa

var Super = (function() {
      var salary = 20000; //靜態私有屬性
      //靜態私有方法
      var showSalary = function(){
        console.log(salary)
      }
      var _Super = function(name,age) {
                 this.name= name; //實例屬性
                 this.age = age;  
                 //實例方法
                 this.show = function() {
                     console.log(name,age)
                  }
                  // 實例私有屬性
                 var height = 180;
                  // 實例私有方法 
                  function _show() {
                      console.log(height)
                   }   
               }
     //實例公有屬性 
     _Super.prototype.address='cn anhui';
     //實例公有方法 
     _Super.prototype.showAddress= function () {
                      console.log(this.address)
                   } 
      return _Super;
}())
複製代碼

靜態公有方法和屬性

相似於實例的公有方法和屬性,只不過此次咱們是直接將屬性綁定到構造函數上而不是掛在prototype上。prototype

var Super = (function() {
      var salary = 20000; //靜態私有屬性
      //靜態私有方法
      var showSalary = function(){
        console.log(salary)
      }
      var _Super = function(name,age) {
                 this.name= name; //實例屬性
                 this.age = age;  
                 //實例方法
                 this.show = function() {
                     console.log(name,age)
                  }
                   // 實例私有屬性
                var height = 180;
                // 實例私有方法 
                function _height() {
                       console.log(height)
                  }   
               }
     //實例公有屬性 
     _Super.prototype.address='cn anhui';
     //實例公有方法 
     _Super.prototype.showAddress= function () {
                      console.log(this.address)
                   } 
    //靜態公有屬性
    _Super.company = 'dxyt';
   //靜態公有方法
   _Super.showCompany = function () {
                      console.log(this.company)
                   } 
      return _Super;
}())
複製代碼
相關文章
相關標籤/搜索