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"