const person = new object();
//프로퍼티 동적생성
person.name = 'Lee';
person.sayHello = function() {
console.log('Hi! my name is ' + this.name);
}
console.log(person); // {name: 'Lee', sayHello: f}
person.sayHello(); // Hi ! My name is Lee
const circle1 = {
radius: 5,
getDiameter() {
return 2 * this.radius;
}
};
const circle2 = {
radius: 10,
getDiameter() {
return 2 * this.radius;
}
};
function Circle(radius){
//생성자 함수 내부의 this는 생성자 함수가 생성할 인스턴스를 가리킨다.
this.radius = radius;
this.getDiameter = function() {
return 2 * this.radius;
};
}
//인스턴스의 생성
const circle1 = new Circle(5);
const circle2 = new Circle(10);
function foo() {
console.log(this);
}
foo() // output : window
const obj = { foo } ;
obj.foo(); // obj
//생성자 함수로서 호출
const inst = new foo(); // inst
//생성자 함수(template)
function Circle(radius) {
this.radius = radius;
this.getDiameter = function() {
return 2 * this.radius;
};
}
//인스턴스 생성
const circle1 = new Circle(5); // 반지름이 5인 Circle 객체를 생성
바인딩 (name binding)
바인딩이란 식별자와 값을 연결하는 과정을 의미한다. 예를 들어, 변수 선언은 변수 이름(식별자)과 확보된 메모리 공간의 주소를 바인딩하는 것이다.
this 바인딩은 this와 this가 가리킬 객체를 바인딩하는 것이다.function Circle(radius) { // 1. 암묵적으로 인스턴스가 생성되고 this에 바인딩된다. console.log(this); // Circle {} this.radius = radius; this.getDiameter = function() { return 2 * this.radius; }; }
function Circle(radius){
//1.암묵적으로 인스턴스가 생성되고 this에 바인딩된다.
//2. this에 바인딩되어 있는 인스턴스를 초기화한다.
this.radius = radius;
this.getDiameter = function() {
return 2 * this.radius;
};
}
function Circle(radius){
//1.암묵적으로 인스턴스가 생성되고 this에 바인딩된다
//2. this에 바인딩되어 있는 인스턴스를 초기화한다.
this.radius = radius;
this.getDiameter = function() {
return 2 * this.radius;
};
//3. (완성된 인스턴스가 바인딩 된) this가 암묵적으로 반환된다
};
//인스턴스 생성. Circle 생성자 함수는 암묵적으로 this를 반환한다.
const circle = new Circle(1);
console.log(circle); // Circle {radius: 1, getDiameter: f}
function Circle(radius){
//1.암묵적으로 인스턴스가 생성되고 this에 바인딩된다.
//2.this에 바인딩되어 있는 인스턴스를 초기화한다.
this.radius = radius;
this.getDiameter = function() {
return 2 * this.radius;
};
// 3.암묵적으로 this를 반환한다.
// 명시적으로 객체를 반환하면 암묵적인 this 반환이 무시된다.
return {};
//인스턴스 생성. Circle 생성자 함수는 명시적으로 반환한 객체를 반환한다.
const circle = new Circle(1);
console.log(circle) // {}
function Circle(radius) {
//1.암묵적으로 빈 객체가 생성되고 this에 바인딩된다.
//2. this에 바인딩되어 있는 인스턴스를 초기화한다.
this.radius = radius;
this.getDiameter = function() {
return 2 * this.radius;
};
// 3.암묵적으로 this 반환한다.
// 명시적으로 원시 값을 반환하면 원시 값 반환은 무시되고 암묵적으로 this가 반환된다.
return 100;
}
//인스턴스 생성. Circle 생성자 함수는 명시적으로 반환한 객체를 반환한다.
const circle = new Circle(1);
console.log(circle); // Circle {radius: 1 , getDiameter: f}
//일반 함수도 객체다.
function foo(){}
//함수는 객체이기에 프로퍼티를 소유할 수 있다.
foo.prop = 10;
//함수는 객체이므로 메서드를 소유할 수 있다.
foo.method = function () {
console.log(this.prop);
};
foo.method(); // 10
function foo() {}
//일반적인 함수로서 호출: [[Call]]이 호출된다.
foo();
//생성자 함수로서 호출: [[Construct]]가 호출된다.
new foo();
내부 메서드 [[Call]]을 갖는 함수 객체를 callable이라 하며, 내부 메서드 [[Construct]]를 갖는 함수 객체를 constructor, [[Construct]]를 갖지 않는 함수 객체를 non-constructor라고 부른다.
constructor는 생성자함수로서 호출할 수 있는 함수.
non-constructor는 객체를 생성자 함수로서 호출할 수 없는 함수를 의미한다.
모든 함수 객체가 [[Constructor]]를 갖고 있는 것은 아니다.
다시 말해, 함수 객체는 constructor일 수도 있고, non-constructor일수도 있다.
//일반 함수 정의: 함수 선언문, 함수 표현식
function foo() {}
const bar = function() {};
//프로퍼티 x의 값으로 할당된 것은 일반 함수로 정의된 함수다. 이는 메서드로 인정하지 않는다.
const baz = {
x: function() {}
};
//일반 함수로 정의된 함수만이 constructor다.
new foo(); // foo {}
new bar() // bar{}
new baz.x(); // x {}
//화살표 함수 정의
const arrow = () => {};
new arrow() // arrow is not a constructor
//메서드 정의: ES6의 메서드 축약 표현만 메서드로 인정한다.
const obj = {
x() {}
};
new obj.x(); // is not a constructor
// 생성자 함수로서 정의하지 않은 일반 함수를 new 연산자와 함께 호출한다면
// 함수가 객체를 반환하지 않았으므로 반환문이 무시된다.
// 따라서 빈 객체가 생성되어 반환된다.
function add (x,y){
return x + y;
}
let inst = new add();
console.log(inst); // {}
//객체를 반환하는 일반함수
function createUser(name,role){
return { name, role };
}
//일반 함수를 new 연산자와 함께 호출
inst = new createuser('Lee', 'admin');
//함수가 생성한 객체를 반환한다.
console.log(inst); // {name: 'Lee', role: 'admin'}
//생성자 함수
function Circle(radius){
//이 함수가 new 연산자와 함꼐 호출되지 않는다면 new.target은 undefined다.
if(!new.target){
//new 연산자와 함께 생성자 함수를 재귀 호출하여 생성된 인스턴스를 반환한다.
return new Circle(radius);
}
this.radius = radius;
this.getDiameter = funcion() {
return 2 * this.radius;
};
}
//new 연산자 없이 생성자 함수를 호출하여도 new.target을 통해 생성자 함수로서 호출된다.
const circle = Circle(5);
console.log(circle.getDiameter());
function Circle(radius){
if(!(this instanceof Circle)){
return new Circle(radius);
}
this.radius = radius;
this.getDiameter = function () {
return 2 * this.radius;
};
};
//new 연산자 없이 생성자 함수를 호출하여도 생성자 함수로서 호출된다.
const circle = circle(5);
console.log(circle.getdiameter());
new 연산자와 함께 생성자 함수에 의해 생성된 객체(인스턴스)는 프로토타입에 의해 생성자 함수와 연결된다. 이를 이용해 new연산자와 함께 호출되었는지 확인할 수 있다.