function Book(point){
this.point = point;
};
Book.prototype.getPoint = function(){
return this.point + 200;
};
var obj = new Book(100);
obj.getPoint();
function 구분
function 오브젝트도 인스턴스
new 연산자로 생성하는 인스턴스는
function Book(point){
this.point = point;
};
Book.prototype.getPoint = function(){
return this.point + 200;
};
var obj = new Book(100);
log(obj.point);
log(obj.getPoint());
// 100
// 300
위의 코드는 function 인스턴스를 생성하는 전형적인 형태
인스턴스 생성 순서, 방법
function Book(point){...}
Book.prototype.getPoint = function(){}
var obj = new Book(100);
console.log(obj.point);
console.log(obj.getPoint());
return this.point + 200;에서
강좌의 함수/메소드 사용 기준
new 연산자와 함께 인스턴스를 생성하는 함수
new 연산자
생성자 함수
코딩 관례로 생성자 함수의 첫 문자는 대문자
function Book(point){
this.point = point;
};
Book.prototype.getPoint = function(){
return this.point;
};
var obj = new Book(10);
new 연산자로 인스턴스 생성을 제어하고, 생성자 함수인 Book()으로 인스턴스를 생성하여 반환
new와 생성자 함수 실행 과정
엔진이 new 연산자를 만나면
function 오브젝트를 생성할 때
[[Construct]]에서 인스턴스를 생성하여 반환
반환된 인스턴스를 new 연산자가 받아
new라는 뉘앙스로 인해
function Book(point){
this.point = point;
};
Book.prototype.getPoint = function(){
return this.point;
};
var bookObj = new Book(10);
Book 인스턴스: {
point: 10,
__proto__ = {
constructor: Book,
getPoint: function(){},
__proto__: Object
}
}
new Book(10) 실행
빈 Object를 생성
오브젝트에 내부 처리용 프로퍼티를 설정
오브젝트의 [[Class]]에 "Object" 설정
Book.prototype에 연결된 프로퍼티(메소드)를
Book function 오브젝트: {
prototype: {
constructor: Book
}
}
생성하는 function 오브젝트를 참조
개인 경험
ES5: constructor 변경 불가
ES6: constructor 변경 가능
var Book = function(){};
var result = Book === Book.prototype.constructor;
log("1:", result);
var obj = new Book();
log("2:", Book === obj.constructor);
log("3:", typeof Book);
log("4:", typeof obj);
// 1:true
// 2:true
// 3:function
// 4:object
Book === Book.prototype.constructor;
Book === obj.constructor;
typeof Book;
typeof obj;
function 오브젝트를 인스턴스로 생성했더니
오브젝트 타입이 바뀐다는 것은
prototype 확장
프로퍼티 공유
인스턴스 상속(Inheritance)
function Book(title){
this.title = title;
};
Book.prototype.getTitle = function(){
return this.title;
};
function Point(title){
Book.call(this, title);
};
Point.prototype = Object.create(Book.prototype, {});
var obj = new Point("자바스크립트");
log(obj.getTitle());
// 자바스크립트
JS에서 prototype은 상속보다
ES5 상속은 OOP의 상속 기능 부족
class Book{
constructor(title){
this.title = title;
};
getTitle(){
return this.title;
}
};
class Point extends Book{
constructor(title){
super(title);
};
};
const obj = new Point("자바스크립트");
log(obj.getTitle());
// 자바스크립트
prototype에 프로퍼티를 연결하여 작성
name에 프로퍼티 이름 작성
value에 JS 데이터 타입 작성
prototype에 null을 설정하면 확장 불가
function Book(){};
Book.prototype = {
constructor: Book,
setPoint: function(){}
};
var obj = new Book();
log(obj.constructor);
// function Book(){}
오브젝트 리터럴{}을 사용하여
constructor가 없어도 인스턴스가 생성되지만
function Book(point){
this.point = point;
};
Book.prototype.getPoint = function(){
return this.point;
};
var obj = new Book(100);
obj.getPoint();
obj: {
point: 100,
__proto__ = {
constructor: Book,
getPoint: function(){},
__proto__: Object
}
}
prototype 확장
function Book(point){};
Book.prototype.getPoint = function(){}
var obj = new Book(100);
obj.getPoint()
인스턴스를 생성하면
this로 메소드를 호출한 인스턴스 참조
인스턴스에서 메소드 호출 방법
__proto__
에 설정되며this.prototype.setPoint() 형태가 아닌 this.setPoint() 형태로 호출
function Book(){
log("1:", this.point);
};
Book.prototype.getPoint = function(){
this.setPoint();
log("2:", this.point);
};
Book.prototype.setPoint = function(){
this.point = 100;
};
var obj = new Book();
obj.getPoint();
// 1:undefined
// 2:100
console.log("1:", this.point);
obj.getPoint();
this.setPoint();
this.point = 100;
function Book(point){
this.point = point;
};
Book.prototype.getPoint = function(){
return this.point;
};
var obj = new Book(100);
log(obj.getPoint());
log(Book.prototype.getPoint());
// 100
// undefined
Book.prototype.getPoint();
Book.prototype을
obj 인스턴스에는 point가 있지만
사용하는 시점에 prototype의 프로퍼티 공유
prototype의 프로퍼티로 인스턴스를 생성하지만
인스턴스의 메소드를 호출하면
원본 prototype에 메소들르 추가하면
function Book(){
this.point = 100;
};
var obj = new Book();
log(obj.getPoint);
Book.prototype.getPoint = function(){
return this.point;
};
var result = obj.getPont();
log(result);
// undefined
// 100
var obj = new Book();
console.log(obj.getPoint);
Book.prototype.getPoint = function(){return this.point;};
var result = obj.getPoint();
return this.point;
이런 특징을 활용하여
obj 인스턴스 = {
point: 100,
getPoint: function(){},
__proto__: {
getPoint: function(){}
}
}
prototype에 연결된 프로퍼티도
인스턴스의 프로퍼티를
인스턴스마다 값을 다르게 가질 수 있음
function Book(point){
this.point = point;
};
Book.prototype.getPoint = function(){
return 100;
};
var obj = new Book(200);
obj getPoint = function(){
return this.point;
};
log(obj.getPoint());
// 200
Book.prototype.getPoint = function(){return 100;};
obj.getPoint = function(){return this.point;};
obj 인스턴스 구성 형태
obj 인스턴스 = {
getPoint: function(){return this.point;},
__proto__: {
getPoint: function(){return 100;}
}
}
obj.getPoint();
인스턴스 프로퍼티는 공유되지 않음
Class 접근