javascript面向對象用法

           function Person(){//構造方法

  }


  Person.prototype = {

name:"張三",

age:22,

gender:"男",

eat:function(s){

alert("我吃:" + s);

}

  };

  var p = new Person();

 


  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)//輸出:"undefined"

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

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章