转:JS中OOP的写法【1】-封装

/*oop中的封装在javascript中的应用
	man
	构造方式 var 变量名 = new man(参数1,参数2,参数3)
  */
  var man = function(){
	var paramsLength = arguments.length;/*可变参数长度*/
	var name = "张三";/*默认nane值*/
	var age = 25; /*默认age值*/
	var business = "web bulider";/*默认职业值*/
	/*处理参数*/
	if(paramsLength==3){
		name = arguments[0];
		age = arguments[1];
		business = arguments[2];
	}
	/*获得name方法*/
	this.getname = function(){
		return name;
	};
	/*name赋值方法*/
	this.setname = function(mingzi){
		name = mingzi;
	};
	/*获得age方法*/
	this.getage = function(){
		return age;
	};
	/*age赋值方法*/
	this.setage= function(nianling){
		age = nianling;
	};
	/*获得business方法*/
	this.getbusiness = function(){
		return business;
	};
	/*business赋值方法*/
	this.setbusiness= function(zhiye){
		business = zhiye;
	};
	/*返回内容*/
	this.stringMe = function(){
		return "姓名为"+name+",他的年龄为"+age+",职业是"+business;
	}
  }
 
  /*实例化*/
  var me = new man("tomie",25,"打酱油的");
   alert(me.stringMe());
  me.setname("李四");
  me.setage(1000);
  me.setbusiness("做俯卧撑的");
  alert(me.stringMe());

 

你可能感兴趣的:(JavaScript,Web,oop)