javascript的物件導向用法

來源:互聯網
上載者:User

標籤:javascript物件導向使用方法

Javascript的物件導向使用方法簡單介紹如下:                                   //聲明構造Person類。使用function作為構造方法。

      function Person(){

  }

      //Person類的公用屬性。使用.prototype聲明。

  Person.prototype = {

name:"張三",

age:22,

gender:"男",

eat:function(s){

alert("我吃:" + s);

}

  };

  //構造對象。

  var p = new Person();

       alert(p.eat("apple"))//輸出:"我吃:apple"

 

       

  function User(pwd){

  var passwd = pwd ;//私人的屬性

  function getPwd(){//私人的調用方法

return passwd ;

  }

  

  this.pwdService = function(){//特權函數(共有方法通過特權方法訪問私人屬性)

return getPwd();

  }

  }


  User.prototype.check = function(p){//共有方法

return this.pwdService() == p;

  }


  var u = new User("123");

  alert(u.passwd)//輸出:"undifined"

  alert(u.pwdService())//輸出:"123"

  alert(u.check("123"))//輸出:"true"


javascript的物件導向用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.