boseok-note
  • README
  • javascript
    • 함수형 자바스크립트
    • core
      • 이벤트루프 (Event Loop)
      • boxing, unboxing
      • prototype
      • 실행컨텍스트
      • hoisting
      • 클로져(Closure)
      • arguments
      • Scope - 스코프 - 유효범위
    • api
      • Promise
      • Fetch API
      • worker-api
      • Web API
    • lib
      • rxjs
        • rxjs
    • 호스트객체?
    • es6
      • var-let-const
      • template-literals
      • async-await
    • tip
      • array-like
      • Object literal, new Object
    • dom-api
      • HTML Element get Text
      • get-size
      • scrollToBottom
    • for in, for of 차이
    • storage
    • Constructor (생성자)
    • Javascript
    • module
  • front-end
    • vuejs
      • vue-rx
      • README
        • 1-start
        • 2-basic-component
        • 3-add-component
        • 4-advanced-component
        • 5-dynamic-methods-event
        • 6-internal-process
      • next-tick
      • compare-react
      • v-model-with-props
      • v-for
      • vuejs
      • vuex
    • browser
      • dom
      • cssom
      • reflow, repaint
      • 렌더링 트리(Rendering Tree)
      • web-standard
    • css
      • text-ellipse
      • white-space
      • css layout
    • Pre-render
    • react-native
      • react-native
    • 최적화 관련
    • reactjs
      • performance
      • reactjs
    • CSR, SSR
  • server-side
    • letsencrypt
    • auto-deploy
    • nlb
    • Certbot, aws https setting with wildcard
    • mysql
    • node.js
      • npm
        • npm install
      • node-dynamodb
      • node에서 간편하게 letsencrypt를 사용하여 https 구현하는 방법
    • Docker
    • nginx
    • Amazon Web Service
    • nginx https 설정
  • design-pattern
    • observer
    • flux
    • README
  • coop
    • git
      • gitignore
      • remote
      • ssh key 사용하기
      • password
      • credential
      • git
  • cs
    • port
    • data-structure
      • tree
      • binary-tree
    • network
      • home-router
      • tcp-udp
      • http
    • process
  • etc
    • sync-async
    • seo
      • seo
      • SEO Check Point
    • rest
    • unity
      • methods
Powered by GitBook
On this page
  • 호이스팅(Hoisting)
  • 오해
  • 호이스팅
  1. javascript
  2. core

hoisting

호이스팅(Hoisting)

console.log(a); // undefined
var a = 1;

일반적인 언어에서 위의 예제코드를 실행한다면 에러가 날것이다.

js에서는 특이하게도 선언전에 변수를 참조해도 에러가 아니다. (var한정)

ES6에서는 이런 문제를 해결하기위해 let 사용을 권장하고 있다.

console.log(b); // ref error - not defined
let b = 1;

오해

위의 설명에서 보는것과 같이 선언전에 참조할 수 있으므로

변수 선언이나 함수가 코드 상단으로 끌어올려지는 것처럼 보인다.

하지만 끌어올려지는게 아니다.

코드는 그 위치에 그대로 있다.

호이스팅

컴파일 단계에서 스코프가 생성되고 변수나 함수가 차지할 메모리를 즉시 할당한다.

이것이 호이스팅이다.

그래서 스코프내에서 선언 전에 참조할수있고, 선언이 끌어올려지는 것처럼 보인다.

그래서 위의 예제같은 코드가 있으면 변수선언의 경우 undefined가 뜨지만,

함수표현식이 아닌 함수선언식으로하면, 함수가 작동한다.

참고

함수표현식

const a = function() {
  //...some code
}

함수 선언식

function a() {
  //...some code
}
Previous실행컨텍스트Next클로져(Closure)

Last updated 5 years ago