js 진급 학습 의 - 대상 지향 (1)
                                            
 2133 단어  js
                    
대상 을 구축 하 는 방식 은 모두 세 가지 가 있 습 니 다.
	
	     
	
		
		//1.        
		//  :                        
		function Person(name,sex,age){
		 	var obj = new Object();
		 	obj.name = name;
			obj.sex = sex;
		 	obj.age = age;
		 	obj.eat = function(){
		 		console.log("  ");
		 	}
		 	return obj;
		 }
		 var person = createObj("  "," ",28);
		 console.log(person);
		//2.         (  、  )
		 var person = {
		 	name:"  ",
		 	sex:" ",
		 	age:30,
		 	eat:function(){
		 		console.log("  ");
		 	},
		 	say:function(){
		 		console.log("  ");
		 	}
		 };
		 console.log(person);
		//3.          
		//1)、          (  )
		//2)、             
		//      :   ()
		//      :  new    ();      new      ,  this      ,          ,this        。
		function Person(){
			this.name = "  ";
			this.sex = " ";
			this.age = 30;
			this.eat = function(){
				console.log("  ");
			}
		}
		//Person.abc = "123"; //       :         .    =   
		var person = new Person();
		//    :         .   
		console.log(person);
		//console.log(Person.abc); //       :         .   
	 
	
대상 에 게 공유 하 는 방법 추가:
	
	    2 
	
		//                       (    )
		//  :           ,                         
		function Person(name,sex,age){
			this.name = name;
			this.sex = sex;
			this.age = age;
			this.hobby = ["  ","  "];
		}
		//           2   :
		//1.                
		
		 Person.prototype.eat = function(){
		 	console.log("  ");
		 }
		 Person.prototype.say = function(){
		 	console.log("  ");
		 }
		console.log(Person.prototype);
		//2.            
		
		 Person.prototype = {
		 	constructor:Person,
		 	eat:function(){
				console.log("  ");
		 	},
		 	say:function(){
		 		console.log("  ");
		 	}
		 };
		 console.log(Person.prototype);
		//                    
		var person1 = new Person("  "," ",30);
		console.log(person1);
		var person2 = new Person("  "," ",28);
		//      ,                             
	 
	
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[2022.04.19] 자바스크립트 this - 생성자 함수와 이벤트리스너에서의 this18일에 this에 대해 공부하면서 적었던 일반적인 함수나 객체에서의 this가 아닌 오늘은 이벤트리스너와 생성자 함수 안에서의 this를 살펴보기로 했다. new 키워드를 붙여 함수를 생성자로 사용할 때 this는...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.