-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/learning-with/learning-js
- Loading branch information
Showing
2 changed files
with
106 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# 22 this | ||
|
||
객체는 상태를 나타내는 프로퍼티와 동작을 나타내는 메서드를 하나의 논리적인 단위로 묶은 복합적인 자료구조. | ||
이때 동작을 나타내는 메서드는 자신이 속한 객체의 상태(= 프로퍼티)를 참조하고 변경할 수 있어야함. | ||
|
||
```javascript | ||
const circle = { | ||
radius: 5, | ||
getDiameter() { | ||
return 2 * circle.radius; | ||
}, | ||
}; | ||
|
||
console.log(circle.getDiameter()); // 10 | ||
``` | ||
|
||
생성자 함수를 정의하는 시점에서 아직 인스턴스를 생성하기 이전이므로 생성자 함수가 생성할 인스턴스를 가리키는 식별자를 알 수 없음. 따라서 자신이 속한 객체 또는 자신이 생성할 인스턴스를 가리키는 특수한 식별자가 필요해서 등장한 this. this는 어디에서든 참조 가능하고, 암묵적으로 자바스크립트 엔진에서 생성됨. 기본적으로 this는 전역 객체가 바인딩됨. 호출방식에 따라 this 바인딩이 다르게 결정됨. | ||
|
||
## 함수 호출 방식에 따라 달라지는 this | ||
|
||
new 연산자를 씀에 의해서 새로 생성한 인스턴스 객체가 this가 되어서 this가 인스턴스 자신이됨. | ||
|
||
### 1. 일반함수 호출 - 전역객체 window를 뜻함 | ||
|
||
```javascript | ||
const foo = function () { | ||
console.dir(this); | ||
}; | ||
|
||
foo(); // window | ||
``` | ||
|
||
### 2. 메서드 호출 - 메서드를 호출하는 object 객체 | ||
|
||
```javascript | ||
const obj = { foo }; | ||
obj.foo(); // obj | ||
``` | ||
|
||
### 3. 생성자 함수 호출 - 생성자 함수가 생성한 인스턴스 자신 | ||
|
||
```javascript | ||
new foo(); // foo { } | ||
``` | ||
|
||
### 4. Function.prototype.apply/call/bind 메서드에 의한 간접 호출 - 인수에 의해 결정 | ||
|
||
```javascript | ||
const bar = { name: "bar" }; | ||
|
||
foo.call(bar); // bar | ||
foo.apply(bar); // bar | ||
foo.bind(bar)(); // bar | ||
``` | ||
|
||
### 단, 일반 함수일 경우 use strict를 쓰면 undefined 출력됨. | ||
|
||
## 일반 함수로 호출된 모든 함수(중첩 함수, 콜백 함수 포함) 내부의 this에는 전역 객체가 바인딩된다. | ||
|
||
- 모두는 아님. 349p | ||
- 콜백 함수의 경우 원칙은 전역 객체를 바인딩하지만 그렇지 않은 경우도 있다. | ||
|
||
```javascript | ||
[1, 2, 3].forEach( | ||
function () { | ||
console.log(this); | ||
}, | ||
[10, 20, 30] | ||
); | ||
``` | ||
|
||
# 23 실행 컨텍스트 | ||
|
||
실행 컨텐스트는 코드를 실행하는데 필요한 환경을 제공하는 객체. 식별자 결정을 더욱 효율적으로 하기 위한 수단. | ||
자바스크립트 엔진이 코드를 스캔하며 변수와 함수를 블록별로 렉시컬 실행 컨텍스트 안에 저장함. | ||
|
||
- [[Environment]] 내부 슬롯에 함수의 상위 스코프를 저장함. | ||
|
||
## 선언방법에 따른 실행 방법 기억해두기 | ||
|
||
- 함수 표현식 function expression VS 함수 선언문 function declaration | ||
- var `{study : undefinde}`, let/const `{study: }`, `{study : f{}}` | ||
|
||
### 생성 단계 | ||
|
||
- 실행 컨텐스트 생성 | ||
- 선언문만 실행해서 Enviroment Record에 기록 | ||
|
||
### 실행 단계 | ||
|
||
- 선언문 외 나머지 코드 순차적 실행 | ||
- Environment Record 참조하거나 업데이트 |