使用Javascript 实现类

/**
 * 人类
 * @author rubekid
 */
function Person(options){
	//私有属性
	var _name; //姓名    
	var _age; //年龄
	
	/**
	 * 构造函数
	 */
	var _construct = function(){
		_name = options.name||"";
		_age = options.age||0;
		console.log("construct");
	}();
	
	this.getName = function(){
		return _name;
	};
	
	this.setName = function(name){
		_name = name;
	};
	
	this.getAge = function(){
		return _age;
	};
	
	this.setAge = function(age){
		_age = age;
	};
}


//测试代码
var p = new Person({
	name:"rubekid",
	age:1
});
console.log(p.getAge());
p.setAge(4);
console.log(p.getAge());
posted @ 2014-07-23 15:58  rubekid  阅读(177)  评论(0编辑  收藏  举报