
- 메소드 표현방법 (1,2,3)
const obj = { name : 'Min Tae', age: 40, getFamilyName: function(){ return 'kim'; }, // 1번 표현법 getLastName : () => 'kim', // 2번 표현법 getBloodType() { return 'B'; // 3번 표현법 } };
- getter, setter
객체 내부적으로는 실제 함수(코드)인데 객체 외부에서는 속성(데이터)처럼 보이게 하는 것class Person { _bloodType : string; constructor(bloodType: string){ this._bloodType = bloodType; } set bloodType(btype:string){ if (btype === 'A' || btype === 'B' || btype === 'O' || btype === 'AB') this._bloodType = btype; // 외부에서 데이터가 변경 될 시 올바른 혈액형만 들어가게하는 것 } get bloodType(){ return `${this._bloodType}형`; } } const p1 = new Person('B'); p1.bloodType; p1.bloodType = 'C'; /// 외부에서 속성의 값을 변경하는 것처럼 보임.
- 객체 구성
객체 속성 추가 const obj = { name : 'Min Tae', age: 40, getFamilyName: function(){ return 'kim'; }, getLastName : (x) => x, getBloodType() { return 'B'; } }; obj.bloodType = 'A' // 객체 속성추가자바스크립트에서 객체 속성 삭제 방지는 객체를 생성하는 3번쨰 방법인 Object를 이용하는 방법을 알아야한다. 아래 확인
const myObj = Object.create(null, { name: { value : 'jake', writable: false, // 수정 가능여부 결정 configurable : true // 삭제 가능여부 결정 } });타입스크립트는 타입을 지정해주면 삭제가 안되고 삭제가 되게 하고싶다면 타입이 선택사항이라는 표시인 ? 를 추가하면 삭제가능