๐Ÿ“
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • ์ฝœ๋ฐฑํ•จ์ˆ˜ (Callback function)
    • ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ
    • ์ž๋ฃŒ๊ตฌ์กฐ new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • ๊ฐ์ฒด
    • Arguments
    • higher order function
    • operators-function-control-flow
    • ๊ฐ์ฒด ์ƒ์„ฑ ํŒจํ„ด 3๊ฐ€์ง€
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • ๋ฐ์ดํ„ฐ ํƒ€์ž…
    • Object ๊ฐ์ฒด
    • ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด์˜ ํ™•์žฅ
    • ์ฐธ์กฐ
    • ํ•จ์ˆ˜
    • ์ƒ์†(Inheritance)
    • this - 'this'๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ '์–ด๋–ป๊ฒŒ' ์‹คํ–‰ํ•˜๋А๋ƒ์— ๋”ฐ๋ฅธ 4๊ฐ€์ง€ this ์ •์˜
    • ์ „์—ญ๊ฐ์ฒด(Global object)
    • ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - ์ž์—ฐ์ˆ˜, ์ •์ˆ˜, 10์ง„์ˆ˜, 2์ง„์ˆ˜, ๋ถ€๋™์†Œ์ˆ˜์ , ์‹ค์ˆ˜
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์™€ ํด๋ž˜์Šค, ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์กด์žฌํ• ๊นŒ?
    • Hooks๊ณผ useEffect ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ
    • Route
    • async wait ์‚ฌ์šฉํ•˜๊ธฐ
    • Hooks API Reference
    • context
    • npm uninstall ํ•˜๋Š”๋ฒ•
    • test ๋งŒ๋“ค๊ธฐ
  • tip
    • ํด๋ฆฐ์ฝ”๋“œ
    • BxSlider๋กœ ํ…์ŠคํŠธ ํ๋ฅด๋Š” ํšจ๊ณผ ๋งŒ๋“ค๊ธฐ
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ 2๋ถ€
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • ์šฐ์•„ํ•œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • ๋จธ์ง€ ํ›„ branch ์‚ญ์ œํ•˜๊ธฐ
    • ์ง€์˜ฅ์—์„œ ์˜จ Git
    • reset-revert
    • develop branch ๋ฅผ pull ํ•˜๊ณ  ์‹ถ์„๋•Œ
  • conference-and-seminar
    • ๋ชจ๋˜ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœํ™˜๊ฒฝ์˜ ์ดํ•ด
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋“œ๋ฆฌ๋ทฐ
    • ์Šค์Šค๋กœ ๊ณต๋ถ€ํ•˜๋Š” ๋ฒ•
    • ๋ฐ”๋‹๋ผ์ฝ”๋”ฉ ์ˆ˜๊ฐ• ํ›„๊ธฐ
    • async ๊ณผ์ œ ํ›„๊ธฐ - ๋น„๋™๊ธฐ, ๋™๊ธฐ, ํด๋กœ์ €, ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด
    • ์œ ์šฉํ•œ ์‚ฌ์ดํŠธ
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars ์ด์šฉ์ž ์†”๋ฃจ์…˜ ๋ชจ์Œ
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • ํƒ€์ž…๊ณผ ์ถ”์ƒํ™”
      • ๊ฐ์ฒด ์ง€๋„
      • ์ด์ƒํ•œ ๋‚˜๋ผ์˜ ๊ฐ์ฒด
      • ์ถ”์ƒํ™” ๊ธฐ๋ฒ•
      • 05. ์ฑ…์ž„๊ณผ ๋ฉ”์‹œ์ง€
      • 07.ํ•จ๊ป˜ ๋ชจ์œผ๊ธฐ
      • 04. ์—ญํ• , ์ฑ…์ž„, ํ˜‘๋ ฅ
      • ํ˜‘๋ ฅํ•˜๋Š” ๊ฐ์ฒด๋“ค์˜ ๊ณต๋™์ฒด
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • ์›น ๋ธŒ๋ผ์šฐ์ €์— URL์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ์›น ํŽ˜์ด์ง€๊ฐ€ ๋ณด์—ฌ์งˆ๊นŒ?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex ํ•ด๋ฒ„๋ ธ์ง€ ๋ญ์•ผ
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd ๋ช…๋ น์–ด ๋ชจ์Œ
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN ์„ค์ •ํ•˜๊ธฐ
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans ์˜ค๋‹ต๋…ธํŠธ
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 2/2
    • ์ฝ”๋“œ ์Šคํ”ผ์ธ  - CSS Rendering 1ํšŒ์ฐจ 1/2
  • you-dont-know-js
    • ํƒ€์ž…
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • IIFE ์ดํ”ผ Immediately Invoked Function Expression ์ฆ‰์‹œ์‹คํ–‰ํ•จ์ˆ˜
  • Global Scope, Global variable, Global Object
  • Scope chain
  • function์•ˆ์— var๋ฅผ ๋ถ™์ด์ง€ ์•Š์€ ๋ณ€์ˆ˜๋Š” Global์—๋‹ค ์ •์˜ํ•œ๋‹ค.

Was this helpful?

  1. javascript

Javascript scopes

Scope determines the accessibility (visibility) of variables.

๋ณ€์ˆ˜์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๋ฒ”์œ„

๋‚ด๋ถ€์—์„œ ์™ธ๋ถ€๋Š” ์ ‘๊ทผ ๊ฐ€๋Šฅ ์™ธ๋ถ€์—์„œ ๋‚ด๋ถ€๋Š” ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ

var a = 1;

function foo() {
    var b = 2;
    console.log(a);
    console.log(b);
}

foo();
console.log(b); // cannot access 'b'

foo() ํ•จ์ˆ˜ ๋‚ด๋ถ€์—๋Š” a์™€ b์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, (๋‚ด๋ถ€์—์„  ์™ธ๋ถ€์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ๋‹ค.) ์™ธ๋ถ€์—์„œ foo() ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์žˆ๋Š” var b์—๋Š” ์ ‘๊ทผ ํ•  ์ˆ˜ ์—†๋‹ค.

๋‚ด๋ถ€ ์™ธ๋ถ€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ๊ธฐ์ค€์€ ํ•จ์ˆ˜ ์ด๋‹ค.

var a = 1;

function foo () {
    var a = 2;
    console.log(a);
}

foo();
console.log(a);

๋งˆ์ง€๋ง‰ ์ค„ console.log(a); ๋Š” 1์ด๋‹ค. ์™ธ๋ถ€์—์„œ ๋‚ด๋ถ€ var a = 2์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์—†๊ณ  ํ˜ธ์ถœ๊ณผ ๋™์‹œ์— var a = 2๋Š” ์‚ฌ๋ผ์ง„๋‹ค. ๋ฟ…

function foo () {
    var a = 10;

    for (var i = 0; i < a; i++){
        console.log(i);
    }
    console.log(i); //๋งˆ์ง€๋ง‰ console.log(i); ์ฐ์€ ๋ถ€๋ถ„
}

foo();

for๋ฌธ์ด ๋๋‚˜๊ณ  ๋งˆ์ง€๋ง‰ console.log(i)๊ฐ€ ์ฐํžŒ ๋ถ€๋ถ„์—๋Š” 10์ด ๋‚˜์˜จ๋‹ค.

์™œ๋ƒํ•˜๋ฉด scope ๋ฒ”์œ„๋Š” ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (var ๊ธฐ์ค€)

IIFE ์ดํ”ผ Immediately Invoked Function Expression ์ฆ‰์‹œ์‹คํ–‰ํ•จ์ˆ˜

(function (){
    var a = 1;

    function foo () {
        var b = 2;
        console.log(a);
        console.log(b);
    }

    //foo();
})();

foo(); //์ฆ‰์‹œ์‹คํ–‰ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์žˆ๋˜ ํ•จ์ˆ˜ ์‹คํ–‰

IIFE๋ฅผ ์ด์šฉํ•ด ๊ณ ์˜๋กœ ๋ฒ”์œ„๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

๋งˆ์ง€๋ง‰ foo(); ํ˜ธ์ถœ์€ error๊ฐ€ ๋œฌ๋‹ค. 'ใ…'/ {} ๋ฒ”์œ„๋ฅผ ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์— 'ใ…' / ์˜ˆ~~

Global Scope, Global variable, Global Object

var a = 1;
function foo () {
    //bar function scope
    function bar() {
        //foo function scope
        console.log(a);
    }
    bar();
}
foo();

Scope chain

bar() -> foo() -> global ์ˆœ์œผ๋กœ ํƒ€๊ณ  ์˜ฌ๋ผ๊ฐ„๋‹ค.

Global Object ๋Š” browser์—์„œ๋Š” window๋‹ค.

function์•ˆ์— var๋ฅผ ๋ถ™์ด์ง€ ์•Š์€ ๋ณ€์ˆ˜๋Š” Global์—๋‹ค ์ •์˜ํ•œ๋‹ค.

function foo () { b = 2 };
foo();

๋ผ๊ณ  ํ–ˆ์„ ๊ฒฝ์šฐ foo ์•ˆ์—๋Š” var๋ฅผ ๋ถ™์ด์ง€ ์•„๋‹ˆํ•˜์—ฌ์„œ ๋ณ€์ˆ˜ b๋Š” global์— ์ •์˜๋œ๋‹ค.

**global object์— key value๋กœ ๋ถ™๋Š”๋‹ค.

console.log(window.b) //2
function foo() {
    a = 1;

    function bar () {
        var a = 2;
        console.log(a);
    }
    bar();
}

foo();
console.log(a);

๊ธ€๋กœ๋ฒŒ ์Šค์ฝ”ํ”„์˜ ๋‹จ์ 

var a = 'sla';
console.log(`[On Load] ${a}`);
setTimeout(function () {
    console.log(`[1 second] ${a}`);
}, 1000);

var a = 'sla2';
console.log(`[On Load] ${a}`);
setTimeout(function () {
    console.log(`[1 second] ${a}`);
}, 1000);

setTimeout ์œ„ ์•„๋ž˜ ์ „๋ถ€ sla2 ๊ฐ€ ๋œฌ๋‹ค. setTimeout์€ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ๊ณ , ๊ธฐ๋‹ค๋ฆฌ๋Š” ์‚ฌ์ด์— ๋ณ€๊ฒฝ๋œ ๊ธ€๋กœ๋ฒŒ ํ•จ์ˆ˜๊ฐ€ ๋œฌ๋‹ค.

๊ธ€๋กœ๋ฒŒ ์Šค์ฝ”ํ”„๋Š” page์— 1๊ฐœ๋‹ค.

solution

function foo () {
    var a = 'sla';
    console.log(`[On Load] ${a}`);
    setTimeout(function () {
        console.log(`foo ํ•จ์ˆ˜์˜ [1 second] ${a}`);
    }, 1000);  
}

foo();

var a = 'sla2';
console.log(`[On Load] ${a}`);
setTimeout(function () {
    console.log(`[1 second] ${a}`);
}, 1000);

์ด๋ ‡๊ฒŒ foo()๋ผ๊ณ  ์“ด๊ฒƒ๋„ ๊ธ€๋กœ๋ฒŒ ์Šค์ฝ”ํ”„์— ํฌํ•จ๋œ๋‹ค.

(function(){
    var a = 'sla';
    console.log(`[On Load] ${a}`);
    setTimeout(function () {
        console.log(`foo ํ•จ์ˆ˜์˜ [1 second] ${a}`);
    }, 1000);    
})()

var a = 'sla2';
console.log(`[On Load] ${a}`);
setTimeout(function () {
    console.log(`[1 second] ${a}`);
}, 1000);

์ฆ‰์‹œ์‹คํ–‰ํ•จ์ˆ˜๋ฅผ ์“ฐ๋Š”๊ฒŒ ๋” ์•ˆ์ „ํ•˜๋‹ค.

Previous๊ฐ์ฒด ์ƒ์„ฑ ํŒจํ„ด 3๊ฐ€์ง€NextFunctional Programming

Last updated 5 years ago

Was this helpful?