09/26 Javascript - object

yookyungmin·2022년 9월 26일
0

자바 스크립트에서 객체를 사용하는 방법 3가지

  • 기본 객체(Object)를 사용하는방법
let student = new Object();
student.name = "Jack";
student.kor = 100;
student.eng = 50;
//student.1 = "one" // 안됨
console.log(student);
  • 객체 liternal을 사용하는 방법(중요, 새로 만들때)
    자바 스크립트에선 []배열 {} 객체
let person={
"id" :1001,
"name" :"Tom",
"contact" : "01012344321",
"1":"one" // 객체Liternal에서만 허용

}

console.log(person.id);
console.log(person.name);
console.log(person.contact);
console.log(person["1"]);
    1. JSON , JAVA SCRIPT OBJECT NOTAIION - 자바스크립트에서 객체를 표기하는 문법
    1. Cross Language Data Serialization
    1. ex) 자바와 파이선이 데이터를 주고 받을떄 XML 사용
      //자바의 데이터 표시 방식을 XML로 변환하는걸 직렬화 과정이라고 한다, 변환 된 XML 을 다시 해석하는걸 역직렬화라고 한다 중요 // xml -> JSON , 둘다사용해야 하는 경우가 많다
  • 3 생성자를 사용하는 방법
function CafeMenu(pid, pname, price){
	this.pid = pid;
    this.pname = pname;
    this.price = price;
    //this를붙여야 멤버 필드라는 뜻이 된다
      this.printInfo = function(){ // 객체 멤버 메서드 만드는방법
		console.log(this.pid+":"+this.pname+":"+price);
		}
}

let menu = new CafeMenu(pid, pname, price);
  • 객체 Liternal 실습
  let person = {
            "name":"jack",
            "contact":"01012344321",
            "hello":function(){  //hello key값에 함수 연결
                console.log("Hello My Name is "+ this.name);
            },
            "hobby":["java", "Reading", "Piano"],
        
            "score":{
                "kor":80,
                "eng":100,
                "math":50
            },
            "favoriteFruits":function(){
                            return["Apple", "Strawberry", "BlueBerry"];
                        },

            "abilities":function(){
                    return {  
                    "java":function(){return 100;},
                    "css":function(){return 90;},
                    "js":function(){return 70;},
                    }
            },
                "helloTo":function(friend){   //friend 객체를 받겠다
                    console.log("hello"+friend.name);
                },

                "Dron": function(){  //대문자를 사용했기에 생성자
                    this.brand = "Samsung";
                    this.price=100000;
                }

            };

0개의 댓글