Javascript - Object Oriented Programming(OOP)
2020. 7. 29. 15:56ㆍjavascript
1. OOP란?
객체 지향형 프로그래밍으로 절차형 프로그래밍과 비교되며 사용 목적으로는 간편하고 간결하게 사용하기 위함이다.
2. Javascript에서 Object를 정의하는 방법
(1) function 사용하기
function make_person(name,age){
this.name = name;
this.age = age;
this.greeting = function(){
console.log('name : ',name,' age : ',age);
}
}
let salva = new Person('salva',25);
(2) class 사용하기(ES6)
class person {
constructor(name,age){
this.name = name;
this.name = age;
}
greeting(){
console.log('hi');
}
}
const pio = new person('pio',25);
3. prototype 사용하기
prototype이란 '객체에 대한 정보를 정의하는 것'으로 우리는 prototype으로 통해 OOP를 실행시, 객체에 대한 정보에 접근 및 사용 및 변경을 할 수 있다.
function make_person (name,age){
this.name = name;
this.age = age;
}
make_person.prototype.printName = function(){
console.log('name : ',this.name);
}
//ƒ (){
// console.log('name : ',this.name);
//}
let person = new make_person('salva',25);
person.printName();
//name : salva
4. prototype 사용의 이유
(1) 메모리의 절약
(2) 편리하게 Object의 값을 추가, 삭제, 변경을 할 수 있다.
(3) __prototype__의 사용으로 객체의 수정 용이하다
'javascript' 카테고리의 다른 글
call, apply, bind (2) - apply (0) | 2020.08.28 |
---|---|
call, apply, bind (1) - call (0) | 2020.08.22 |
call, apply, bind를 시작하기 전에 this (0) | 2020.08.11 |
Callback function (0) | 2020.08.11 |
Javascript - ES6 Class & super (0) | 2020.07.30 |