日期:2014-05-16  浏览次数:20431 次

前台是个什么东西---JavaScript---JS面向对象

<html>
<head>
	
</head>
<body>
	<button onclick="person1.introduce()">person1</button>
	<button onclick="person2.introduce()">person2</button>
	<button onclick="person3.introduce()">person3</button>
	<button onclick="person4.introduce()">person4</button>
	<button onclick="checkFunc()">checkFunc</button>
	<button onclick="checkFuncByProto()">checkFuncByProto</button><br/>
	<button onclick="testCnstrct()">testCnstrct</button>
	<button onclick="testCnstrctpttp()">testCnstrctpttp</button>
	<button onclick="testDbproto()">testDbproto</button>
	<button onclick="JSONFontI()">JSONFontI</button>
	<button onclick="JSONFontII()">JSONFontII</button><br/>
	<button onclick="testFactory()">testFactory</button>
	<button onclick="testAbstractFactory()">testAbstractFactory</button>
	
	<script>
		var name = "1111111111"
		
		
		
		/********************************************/
		/* 第一种构建对象的办法*/
		/* ***推断***,{}即为对象.*/
		/********************************************/
		var person1 = {};
		person1.name = "junzi";
		person1.age = 23;
		person1.introduce = function () {
			alert("My name is " + this.name + ".I'm " + this.age);
		};
		
		
		
		/********************************************/
		/* 第二种构建对象的办法,类似JSON格式":"*/
		/********************************************/
		var person2 = {
			name: "junzi",
			age: 23,
			introduce: function () { 
							alert("My name is " + this.name + ".I'm " + this.age); 
						}
		};
		
		
		
		/********************************************/
		/* 第三种构建对象的办法,与Java中有些相似*/
		/********************************************/
		var Person = function () {
			this.name = "junzi";
			this.age = 23;
			this.introduce = function () {
								alert("My name is " + this.name + ".I'm " + this.age);
							};
		};
		var person3 = new Person();
		
		
		
		/********************************************/
		/* 第四种构建对象,与第三种类似,只不过参数通过new 构造传入 */
		/********************************************/
		var Person = function (name, age) {
			this.name = name;
			this.age = age;
			this.introduce = function () {
								alert("My name is " + this.name + ".I'm " + this.age);
							};
		};
		var person4 = new Person("junzi",23);
		
		
		
		/********************************************/
		/* 看看实例之间的关系@@ */
		/********************************************/
		var person5 = new Person("chaokong",23);
		function checkFunc(){
			alert(person4.introduce==person5.introduce);//结果是false,说明introduce有两个实现分别在4和5中
			alert(person4.age==person5.age);// 结果是true,说明基本类型的话之比较数值是否相同
			alert(person4.name==person5.name);// 如果两个名字值不同,则为false,***推测***在javascript中的变量比较的是值是否相同,而方法被视为对象.于是下边...
		}
		
		
		
		/********************************************/
		/* 通过prototype的方式 */
		/********************************************/
		var PersonI = function(name,age){
			this.name = name;
			this.age = age;
		}
		/*
		 * 在JavaScript中,prototype对象是实现面向对象的一个重要机制。
		 * 每个函数就是一个对象(Function),函数对象都有一个子对象,即prototype对象,类是以函数的形式来定义的。
		 * prototype表示该函数的原型,也表示一个类的成员的集合。
		 * 在通过new创建一个类的实例对象的时候,prototype对象的成员都成为实例化对象的成员。
         * 1、该对象被类所引用,只有函数对象才可引用;
         * 2、在new实例化后,其成员被实例化,实例对象方可调用。
         * 同时,函数是一个对象,函数对象若直接声明成员,不用被实例化即可调用。 
         */
		PersonI.prototype.introduce = function(){
											alert("My name is " + this.name + ".I'm " + this.age);
										}
		var person6 = new PersonI("junzi",23);
		var person7 = new PersonI("chaokong",23);
		function checkFuncByProto(){
			alert(person6.introduce==person7.introduce);//
			alert(person6.age==person7.age);
			alert(person6.name==person7.name);
		}
		
		
		
		/********************************************/
		/* 研究研究prototype*/
		/********************************************/
		/*
		 * JavaScript中创建一个对象分以下几步:
		 * <1> var p={}; 也就是说,初始化一个对象p。
		 * <2> p