javascript面向对象编程----类的定义

最简单的javascript类的实现,

缺点所有属性都是公开的

 

window.onload = function() {
	var someone = new Person("someone");
	var sb = new Person("sb", 99);

	someone.setName("wod").setAge(100).showInfo();
	// alert(typeof Person);//function
	// alert(typeof someone);//object
};

/*******************************************************************************
 * class Person
 ******************************************************************************/
var Person = function(name, age) {
	if (name == undefined)
		throw new Error("构造器需要 name参数");

	this.setName(name);
	this.setAge(age);
};

Person.prototype = {
	getName : function() {
		return this.name;

	},
	setName : function(name) {
		this.name = name;
		return this;// 能够链式调用
	},
	getAge : function() {
		return this.age;

	},
	setAge : function(age) {
		if (age < 0 || age > 120)
			throw new Error("invalid Person age");// 简单check
		this.age = age || 20;// 构造器无age参数时,默认值20
		return this;

	},
	showInfo : function() {
		return this.name + "'s" + " age is " + this.age + " years old.";
	}
};

 

 

 

用闭包实现带有私有成员的类

 

window.onload = function() {
	var someone = new Person("t");
	var sb = new Person("sb", 99);

	var bbb = someone.setAge(12).showInfo();
	alert(bbb)
};

/*******************************************************************************
 * class Person
 ******************************************************************************/
var Person = function(newName, newAge) {
	if (newName == undefined)
		throw new Error("构造器需要 name参数");
	// -------------------private start-------------------
	// 私有属性
	var name, age;
	// 私有方法
	function checkAge(age) {

		if (age < 0 || age > 120) {
			throw new Error("invalid Person age");// 简单check
			return false;
		}
		return true;
	}
	// -------------------private end -------------------

	// -------------------public start-------------------
	// 特权方法(privileged methods),能够访问私有属性,需要访问私有属性的方法才设成特权方法
	this.getName = function() {
		return name;
	};

	this.setName = function(newName) {
		name = newName;
		return this;// 能够链式调用
	};

	this.getAge = function() {
		return age;
	};

	this.setAge = function(newAge) {
		checkAge(newAge)
		age = newAge || 20;// 构造器无age参数时,默认值20
		return this;
	};

	this.showInfo = function() {
		return name + "'s" + " age is " + age + " years old.";
	};
	//
	// 构造器
	this.setName(newName);
	this.setAge(newAge);
};

// 公开的不需访问私有属性的方法定义
Person.prototype = {

	method1 : function() {
		// ...
	},
};
// -------------------public end-------------------

 

 

 静态方法的定义

 

window.onload = function() {
	

	Person.staticMethod1();//静态调用
	Person.showPersonNum();
};

/*******************************************************************************
 * class Person
 ******************************************************************************/
var Person = (function() {

	// -------------------private start-------------------
	// private static attributes 私有静态属性
	var numOfPersons = 0;
	var numOfFingers = 10;
	// 私有静态方法

	function showFingerNum() {
		return numOfFingers;
	}

	var ctor = function(newName, newAge) {
		if (newName == undefined)
			throw new Error("构造器需要 name参数 constructor need a name");

		// 私有属性
		var name, age;
		
		// 私有方法(与定义在return外的私有静态方法用法上一样)
		function checkAge(age) {

			if (age < 0 || age > 120) {
				throw new Error("invalid Person age");// 简单check
				return false;
			}
			return true;
		}
		// -------------------private end -------------------

		// -------------------public start-------------------
		// 特权方法(privileged methods),能够访问私有属性,需要访问私有属性的方法才设成特权方法
		this.getName = function() {
			return name;
		};

		this.setName = function(newName) {
			name = newName;
			return this;// 能够链式调用
		};

		this.getAge = function() {
			return age;
		};

		this.setAge = function(newAge) {
			checkAge(newAge);
			age = newAge || 20;// 构造器无age参数时,默认值20
			return this;
		};

		this.showInfo = function() {
			return name + "'s" + " age is " + age + " years old."
					+ showFingerNum();
		};

		// 构造器
		numOfPersons++;
		this.setName(newName);
		this.setAge(newAge);
	};

	// public privileged static method 能够访问私有属性的静态方法;
	ctor.showPersonNum = function() {
		alert("there has "+numOfPersons+" Person been created");
	};
	return ctor;

})();
// public non-privileged static method 公开的不需访问私有属性的静态方法定义
Person.staticMethod1 = function() {
	alert("Person.staticMethod1");
};

// public, non-privileged methods 公开的不需访问私有属性的方法定义
Person.prototype = {
	method1 : function() {
		alert("method1");
	},
	method2 : function() {
		alert("method2");
	}
};
// -------------------public end-------------------
 

你可能感兴趣的:(JavaScript)