JavaScript Basic Syntax
JavaScript ๋?
Browser๋ฅผ ์กฐ์ํ๊ธฐ ์ํ ์ธ์ด
๋ธ๋ผ์ฐ์ ์
๋ธ๋ผ์ฐ์ ์ ์ํ
๋ธ๋ผ์ฐ์ ๋ฅผ ์ํ
JavaScript๋ ํ์ค๊ณผ์ ์ ์์ด๋ค!
JQuery
JQuery๋ฅผ ์ฌ์ฉํ๋ฉด ์๋๋ ์ฝ๊ฐ ๋๋ ค์ง ์ ์์ง๋ง
cross-browsing
issue๋ฅผ ์์ ์ค๋ค
Vanilla JS
: ์ฐํ์ฃฝ์ ์๊ฒจ๋๋ JavaScript Library ๊ทธ๋ง์ฐ์! ๋ ์บ ํ์ธ
Vanilla == '์์ '
์๋ฌด๋ฐ library๋ ์ค์นํ์ง ์๊ณ JavaScript๋ฅผ ์ฐ๋ ๊ฒ!
JavaScript
์ต์๋จ์
window
๊ฐ ์์
window.document

write ํ๊ธฐ
window.document.write()

์ต์๋จ์ ํญ์
window
์ด๊ธฐ ๋๋ฌธ์ ์๋ต ๊ฐ๋ฅํ๋ค

ASI (Automatic Semicolon Insertion)
: The principle of the feature is to provide a little leniency when evaluating the syntax of a JavaScript program by conceptually inserting missing semicolons
3๋ ๊ตฌ์ฑ์์
BOM (Browser Object Model)
DOM (Documenet Object Model)
DOM ์กฐ์
JavaScript์ ์กด์ฌ ์ด์
ES (EcmaScript)
ES (EcmaScript)
let vs const
let
: ์ฌ ํ ๋น์ด ๊ฐ๋ฅํ๋ค
const
: ์ฌ ํ ๋น์ด ๋ถ๊ฐ๋ฅํ๋ค !== ๊ฐ์ด ๋ฐ๋์ง ์๋๋ค
Naming Convention
lower cammel case ์ฌ์ฉํ๊ธฐ!
snake case ์๋๋ค!
const addOneToNumber // good const add_one_to_number //bad
์ ๋ ๋์จํ ์ผ์น ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ง ์๋๋ค
==
,!=
=> X===
,!==
=> O
Function => 1๊ธ ๊ฐ์ฒด
1๊ธ ๊ฐ์ฒด
๋ณ์์ ์ ์ฅํ ์ ์๋ค.
ํจ์์ ๋ฆฌํด๊ฐ์ด ๋ ์ ์๋ค.
ํจ์์ ์ธ์๊ฐ ๋ ์ ์๋ค.
DOM (Document Object Model) ์กฐ์
String ์ธ HTML์ Object๋ก ๋ฐ๊พธ๊ณ
์ดํดํ๊ณ ํด์ํด์
DOM Tree๋ฅผ ๋ง๋ค๊ณ
ํ๋ฉด์ rendering (๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ)
์ ๋ฆฌ:
์ธ์์ data๋ ์ ๋ถ String (HTML)
์ด๊ฒ์ ํด์ํ๋ parse/pasing ๊ณผ์ ์ ๊ฑฐ์ณ์ผ ํ๋ค (DOM์ด ํด์)
Selecting
: querySelector
, querySelectorAll
querySelector
, querySelectorAll
Create
: .classList.add()
.classList.add()
const myP = document.createElement('p')
myP.innerText = 'Greater'
const myPStr = '<p>Greate</p>'
myPStr.classList // undifined -> string์ด๋๊น
myP.classList.add('wow') // object๋ ๊ฐ๋ฅ
I. Scope
์ค2๋ณ ํํค์ฝ๋ชจ๋ฆฌ ๋ง๋ด๋์ ๋ฒ์น
์ค๊ดํธ๊ฐ ์์ ๊ฒฝ์ฐ, Scope๋ฅผ ๋ง๋ ๋ค!!
function์ผ ๊ฒฝ์ฐ
์ ์ด๋ฌธ (if / while / switch (control)) ๋ฑ5 ์ผ ๊ฒฝ์ฐ
var
function์์๋ง scope๋ก ๋ฌถ์์let
์ ์ค๊ดํธ๊ฐ ์์ ๊ฒฝ์ฐ (scope) ๋์ผํ๊ฒ ์๋
II. var
, let
, const
var
, let
, const
var
ํ ๋น ์์
์ ์ธ ์์
ํจ์ ์ค์ฝํ (์ ๋ ์ฐ์ง ์๋๋ค.)
let
ํ ๋น ์์
์ ์ธ์ ํ๋ฒ
๋ธ๋ก ์ค์ฝํ
const
ํ ๋น ํ๋ฒ
์ ์ธ ํ๋ฒ
๋ธ๋ก ์ค์ฝํ
Identifier (์๋ณ์) Convention
๋ณ์, ํจ์, ๊ฐ์ฒด: ์บ๋ฉ ์ผ์ด์ค (
camelCase
)class, ์์ฑ์: ํ์ค์นผ ์ผ์ด์ค (
PascalCase
)์์: ๋๋ถ์ ์ค์ผ์ดํฌ ์ผ์ด์ค (
SNAKE_CASE
)
III. Hoisting (ํธ์ด์คํ
)
var๋ฅผ ์ฐ์ง ์๋๋ค
function ์ ์ธํ์ผ๋ก ์ฐ์ง ์๋๋ค
์ ์ธํ์ผ๋ก ์ฐ๋ฉด ์๊ฐ๋๋ก ์ฝ๋๊ฐ ์์ง์ด์ง ์์
why?
์คํ ์ ์ ํ ๋ฒ ํ์ด์, ์ ์ธ ์ด์ ์ ์ฐธ์กฐ๊ฐ ๊ฐ๋ฅํ๊ฒ ํจ
// ์ ์ธํ
function hello() {}
// ํํ์
const hello = function() {}
V. ์๋ฃํ
(1) ์ซ์
const num1 = 123
const num2 = -2.88
const a = NaN // Not a Number
const b = Infinity
const c = -Infinity
(2) ๊ธ์
ํฉ์ฒด (concatenation)
+
์์ (interpolation)
template literal ${}
const firstName = 'Chloe'
const lastName = 'Kim'
const middleName = 'Juhyun'
const fullName = firstName + lastName
const greeting = `Hello ${middleName}`
(3) Boolean
๋ญ๊ฐ ์ฐธ์ ๊ฐ๊น๊ณ , ๋ญ๊ฐ ๊ฑฐ์ง์ ๊ฐ๊น๋
true
,false
(์๋ฌธ์)
1. Truthy
Falsy ๊ฐ ์๋ ๋ชจ๋ ๊ฐ๋ค
[]
- emptyArray{}
- emptyObject
2. Falsy
์๊ฑฐ๋, ์ ์๋์ง ์์ ๊ฒ
null
undefined
""
0
[] == true
![]
VI. ์๋ฃ๊ตฌ์กฐ
์ด๋ป๊ฒ ์ ์ฅํ๊ณ , ์กฐ์ํ๋์ง (CRUD) => method๋ฅผ ๋ณด์
Array (
list
)array helper method
Object (
dict
)
VII. ํจ์
์ ์ธ์ (statement)
ํํ์ (expression)
Arrow Function
(Class) Method(OOP) ํจ์๊ฐ ์๋ ๊ณณ์์๋ง ์ฌ์ฉ
Class ์ ์์์๋ arrow ํจ์ ์ฌ์ฉํ์ง ๋ง์!
why?
this
,arguments
,super
,new.target
๋ฑ์ binding ํ์ง ์๊ธฐ ๋๋ฌธ
์์ฑ์๋ก ์ฌ์ฉ X
Event Listener Callback function์ผ๋ก ์ฌ์ฉ X
ex)
// 1. ์ ์ธ์
function add (num1, num2){
return num1 + num2
}
add(1,2)
// def add(num1, num2):
// 2. ํํ์
const sub = function(num1, num2){
return num1 - num2
}
sub(2,1)
// sub = lambda ์
๋ ฅ๊ฐ: ์ถ๋ ฅ๊ฐ
// 3. Arrow Function
const arrow = function(name){
return `hello ${name}`
}
const arrow = (name) => {
return `hello!! ${name}`
}
// ์ธ์๊ฐ ํ๋์ผ ๋
const arrow = name => {
return `hello!!! ${name}`
}
// ํ์ค๋ก ์ค์ฌ๋ผ
const arrow = name => {return `hello!{name}`}
//arrow function ๋ํ์
// (1) ์ธ์๋ 1๊ฐ
// (2) return์ด ์กด์ฌ & expression
const arrow = name => `hello!!!! ${name}`
VIII. OOP
๋งค์ฐ ์ ์ฐํ ๊ฐ์ฒด๋ฅผ ํตํ OOP
Class๋ฅผ ์ ์ ์์ด๋ผ!
Prototypal Inheritance
ES6+ ๋ถํฐ
class
๋์
ex)
const chloe = {
'name': 'Chloe',
birth: '1995',
'greeting': function(){
return `Hi, This is ${name}`
},
farewell(){
return `Bye~~ -${this.name}-`
}
}
class Person {
constructor(name, birth){
this.name = name
this.birth = birth
}
greeting() {
return `Hi, This is ${this.name}`
}
}
const camila = new Person('Camila', 1995)
const bella = new Person('Bella', 1993)
console.log(camila.name)
console.log(camila.greeting())
console.log(bella.name)
console.log(bella.greeting())
// ์ค๊ฐ์ ์ถ๊ฐ ๊ฐ๋ฅ!! -> ์ ์ฐํ๋ค
bella.money = 100000000
console.log(bella.money)
/*
Python ์์๋..
class Person:
def __init__(self, name, birth):
self.name = name
self.birth = brith
def greeting(self):
return f'Hi, This is {self.name}'
*/
console.log(chloe.name)
console.log(chloe['birth'])
console.log(chloe.greeting())
console.log(chloe.farewell())
Last updated
Was this helpful?