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"
原文地址:http://iamevergreen.blog.51cto.com/6848524/1659883