javascriptの中でアナログは対象に向かってプログラミングします.

1580 ワード


	
		016
		
			
			//    
			//js          ,      
			/*
			var obj=new Object();//var obj={};
			obj.name='z3';
			obj.age=' ';
			obj.sayName=function(){alert('    ');};
			
			obj.sayName();
			*/
			//    
			//     ,    
			/*
			function createPerson(name,sex,age){
				var obj=new Object();
				obj.name = name;
				obj.sex = sex;
				obj.age = age;
				obj.sayName=function(){alert("  "+name);};
				return obj;
			}
			
			var p1 = createPerson('z3',' ',20);
			var p2 = createPerson('l4',' ',21);
			
			alert(p1.name);//z3
			alert(p1.sex);// 
			p1.sayName();//  z3
			*/
			
			//           new Array new Date
			
			//          (    )
			function Person(name,age,sex){
				this.name=name;
				this.age=age;
				this.sex=sex;
				this.sayName=function(){alert(name);};
			}
			//       new               
			/*
			var p1=new Person('xiao 1',21,' ');
			var p2=new Person('xiao 2',22,' ');
			//alert(p1.name);//xiao 1
			//p2.sayName();//xiao 2
			
			alert(p1==p2);//false     ,
			alert(p1.constructor==Person);//true
			alert(p2.constructor==Person);//true
			alert(p1 instanceof Person);//true
			alert(p1 instanceof Object);//true
			*/
			//       
			//1       
			var p1=new Person('xiao',20,' ');
			//2          
			Person('xiao 2',20,' ');//    windows,    this windows,         
			alert(name);//xiao 2
			//3               
			var o = new Object();
			//call apply
			Person.call(o,'xiao 4',10,' ');
			o.sayName();//xiao 4