TIL
TIL 2020.11.03
paigekim29
2020. 11. 3. 09:40
- Primitive Type(์์ ํ์
)
- string
- number
- biginit
- boolean
- undefined
- symboll
- null
- ์ฌํ ๋น ํ๋๋ผ๋, ์ฒ์ ํ ๋นํ๋ ๊ฐ์ด ๋ฐ๋์ง ์์
- Reference Type(์ฐธ์กฐ ํ์
)
- function
- array
- object
- ์ฌํ ๋นํ๋ฉด, ๋ชจ๋ ๊ฒ์ด ์๋์ ์ผ๋ก ๋ค ๋ฐ๊ฟ๋ฒ๋ฆผ
- save inside 'heap'
12console.log([1,2,3] === [1,2,3]; // falseconsole.log({ foo: 'ba' } === { foo: 'ba' }); // false
cs
- SCOPE(๋ฒ์)
- determines the accessibility of variables
- JS receive own scope once function was created, which is lexical scope
- check who is its outer scope
- can put function inside function
- Local Scope vs. Global Scope
- Local Scope
- 'Local Variable'(์ง์ญ๋ณ์) has higher priority than global variable and cannot use it outside function
12345678let greeting = 'Hi';function greet() {let greeting = 'Hello';console.log(greeting); // 'Hello'}console.log(greeting); // 'Hi'greet();console.log(greeting); // 'Hi'
cs 1234function greetSomeone() {let firstName = 'Paige';return greeting + ' ' + firstName;}cs
- 'Local Variable'(์ง์ญ๋ณ์) has higher priority than global variable and cannot use it outside function
- Global Scope
- 'Global Variable'(์ ์ญ๋ณ์) can access everywhere
12345678let greeting = 'Hi';function greetSomeone() {let firstName = 'Paige';return greeting + ' ' + firstName;}greetSomeone(); // => Hi Paige'firstName; // => ReferenceError
cs
- 'Global Variable'(์ ์ญ๋ณ์) can access everywhere
- Local Scope
- Function Scope vs. Block Scope
- Block
- starts and end with curly bracket { }
- var vs. let
- var: gets its own scope (old way)
- connects with window object
- let: only works inside block scope
let const var scope block scope block scope function scope reassignment(์ฌํ ๋น) โญ๏ธ โ โ redeclaration(์ฌ์ ์ธ) โ โ โ
- var: gets its own scope (old way)
- Block
- Global Variable(์ ์ญ๋ณ์) and Window object(์๋์ฐ๊ฐ์ฒด)
- will connect to function that is assigned at global scope and var varible
- do not declare too much in global scope!
12345678var myName = "Paige";console.log(window.myName); // Paigefunction foo() {console.log('bar');}console.log(foo === window.foo); // truecs
- No Declaration(์ ์ธ์์ด ์ด๊ธฐํ๋ ์ ์ญ๋ณ์)
- do not use variable without assignment
- if it does, it becomes global variable(์ ์ญ๋ณ์)
- In order to avoid error, use 'use strict'
1234567function showAge() {age = 24;console.log(age);}showAge(); // 24console.log(age); // 24age === window.agecs
- Local Scope vs. Global Scope
- Spread Syntax(...variable)
- if spread syntax goes inside parameter, it become 'rest parameter(rest syntax)'
1234function pintMaxNums(...args) {console.log(args); //[10, 30, 40]}pintMaxNums(10, 30, 40);
cs
- if spread syntax goes inside parameter, it become 'rest parameter(rest syntax)'