JavaScript - Object

yeong ·2022년 11월 17일

js

목록 보기
23/49

Object 클래스 함수
요소가 하나도 선언되어 있지 않은 클래스 함수 - 요소가 없는 객체 생성
생성된 Object 객체에 필요한 요소를 추가하여 사용 - 원하는 요소의 객체를 하나만 생성

	var student=new Object(); //요소가 하나도 없는 객체 생성 -Empty Object
	//객체변수를 출력하면 객체를 생성한 클래스 함수명을 제공받아 출력 
	//alert(student); //[object object]
	
	//Object 객체에 요소 추가 	
	student.num=1000;
	student.name="홍길동";
	student.address="서울시 강남구";
	//alert("학번 ="+student.num+"이름 ="+student.name+"주소 ="+student.address);
	
	student.display=function() {
		alert("학번 ="+student.num+"이름 ="+student.name+"주소 ="+student.address);
	}
	
	student.display();

자바스크립트에서는 Object 객체를 보다 쉽게 생성하기 위해 JSON 기능 제공
JSON(JavaScript Object Notation) : 자바스크립트 객체 표기법
=> 자바스크립트에서 객체를 쉽게 생성하기 위해 제공되는 방법
=> Object 객체 : {},Array 객체 : []

	var student={};
	alert(student);
	//Object 객체에 요소 추가 	
	student.num=1000;
	student.name="홍길동";
	student.address="서울시 강남구"
	student.display=function() {
		alert("학번 ="+student.num+"이름 ="+student.name+"주소 ="+student.address);
	}
	
	student.display();

✨JSON을 이용해서 객체를 생성하면 객체 생성시 요소 추가 가능
형식) var 변수명 = {"요소명":값,"요소명":함수,...}

var student={"num":1000,"name":"홍길동","address":"서울시 강남구"
			,display:function(){
	alert("학번 ="+student.num+"이름 ="+student.name+"주소 ="+student.address);
			}
	};
	student.display();

ES6에서는 Object 객체에서 메소드를 간결하게 표현 가능
요소명:function(매개변수,...) { 명령; ... } >> 요소명(매개변수,...) { 명령; ... }

var student={num:1000,name:"홍길동",address:"서울시 강남구"
		,display(){		
			alert("학번 = "+this.num+", 이름 = "+this.name+", 주소 = "+this.address);
		}
	};
	student.display();

0개의 댓글