일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- CSS
- es6
- Review
- eas build
- javascript
- ES5
- 앱 마케팅 전략
- innerHTML
- Dom
- UI
- snyk
- HTML
- nodeValue
- Event
- yet
- textContent
- expo
- node.js
- js
- a11y
- 커스텀 테마
- react
- innerText
- modal
- addEventListener
- node
- Empty
- eas
- 앱 시장 조사
- 빌드 전 점검
- Today
- Total
the murmurous sea
JS ES6: Functions hoisting check 본문
After
//1. var console.log('typeof minus:', typeof minus) console.log('minus(5,1):', minus(5,1)) var minus = (a,b) => a - b; //typeof minus: undefined //Uncaught TypeError: minus is not a function at check.js:4 //(anonymous) @ check.js:4 //2. const console.log('typeof minus:', typeof minus) console.log('minus(5,1):', minus(5,1)) const minus = (a,b) => a - b; //Uncaught ReferenceError: Cannot access 'minus' before initialization at check.js:13 //(anonymous) @ check.js:13 //3. let console.log('typeof minus:', typeof minus) console.log('minus(5,1):', minus(5,1)) let minus = (a,b) => a - b; //Uncaught ReferenceError: Cannot access 'minus' before initialization at check.js:22 //(anonymous) @ check.js:22 //4. function console.log('typeof minus:', typeof minus) console.log('minus(5,1):', minus(5,1)) function minus(a,b){ return a - b }; //typeof minus: function //minus(5,1): 4 //5. arrow function without console.log('typeof minus:', typeof minus) console.log('minus(5,1):', minus(5,1)) minus = (a,b) => a - b; //typeof minus: undefined //Uncaught ReferenceError: minus is not defined at check.js:41 //(anonymous) @ check.js:41
ReferenceError & TypeError ▼
The ReferenceError object represents an error when a non-existent variable is referenced.
The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type.
1. var
Uncaught TypeError: minus is not a function.
: minus is declared but before defined.
: when there was an attempt to call a value from a function, but the value is not actually a function.
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function)
Why it's not actually a function?
'minus' is added under the window but it's 'undefined' yet. I guess it gives this error because it's declared but as undefined, which is one of the primitive data types. Not object(object, array or function)
2.3. const, let
Uncaught ReferenceError: Cannot access 'minus' before initialization
: Temporal dead zone
: when a lexical variable was accessed before it was initialized. This happens within any block statement, when let or const declarations are accessed before they are defined.
4. function
: no error
5. arrow without
Uncaught ReferenceError: minus is not defined
: when there is a non-existent variable referenced somewhere.
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_defined)
[Comparison] Before
//1. var var minus = (a,b) => a - b; console.log('typeof minus:', typeof minus) console.log('minus(5,1):', minus(5,1)) //typeof minus: function //minus(5,1): 4 //2. const const minus = (a,b) => a - b; console.log('typeof minus:', typeof minus) console.log('minus(5,1):', minus(5,1)) //typeof minus: function //minus(5,1): 4 //3. let let minus = (a,b) => a - b; console.log('typeof minus:', typeof minus) console.log('minus(5,1):', minus(5,1)) //typeof minus: function //minus(5,1): 4 //4. function function minus(a,b){ return a - b }; console.log('typeof minus:', typeof minus) console.log('minus(5,1):', minus(5,1)) //typeof minus: function //minus(5,1): 4 //5. arrow function without minus = (a,b) => a - b; console.log('typeof minus:', typeof minus) console.log('minus(5,1):', minus(5,1)) //typeof minus: function //minus(5,1): 4
'#dev > 개념정리' 카테고리의 다른 글
JS: about Event (0) | 2020.06.03 |
---|---|
JS: EventTarget.addEventListener() (empty) (0) | 2020.06.03 |
JS: Literal (incompleted) (0) | 2020.05.27 |
Git : 생활코딩 요약 (0) | 2020.05.22 |
Progressive Web App (PWA) (0) | 2020.05.22 |