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λŒ€ κ΅¬μ„±μš”μ†Œ

  1. BOM (Browser Object Model)

  2. DOM (Documenet Object Model)

    • DOM μ‘°μž‘

    • JavaScript의 쑴재 이유

  3. 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. λ³€μˆ˜μ— μ €μž₯ν•  수 μžˆλ‹€.

  2. ν•¨μˆ˜μ˜ 리턴값이 될 수 μžˆλ‹€.

  3. ν•¨μˆ˜μ˜ μΈμžκ°€ 될 수 μžˆλ‹€.

DOM (Document Object Model) μ‘°μž‘

  • String 인 HTML을 Object둜 λ°”κΎΈκ³ 

  • μ΄ν•΄ν•˜κ³  ν•΄μ„ν•΄μ„œ

  • DOM Treeλ₯Ό λ§Œλ“€κ³ 

  • 화면에 rendering (κ·Έλ¦Ό 그리기)

정리:

  • μ„Έμƒμ˜ dataλŠ” μ „λΆ€ String (HTML)

  • 이것을 ν•΄μ„ν•˜λŠ” parse/pasing 과정을 거쳐야 ν•œλ‹€ (DOM이 해석)

Selecting

: querySelector , querySelectorAll

Create

: .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λ₯Ό λ§Œλ“ λ‹€!!

    1. function일 경우

    2. μ œμ–΄λ¬Έ (if / while / switch (control)) λ“±5 일 경우

  • var functionμ—μ„œλ§Œ scope둜 λ¬Άμ˜€μŒ

  • let 은 μ€‘κ΄„ν˜Έκ°€ μžˆμ„ 경우 (scope) λ™μΌν•˜κ²Œ μž‘λ™

II. var, let, const

  • var

    • ν• λ‹Ή 자유

    • μ„ μ–Έ 자유

    • ν•¨μˆ˜ μŠ€μ½”ν”„ (μ ˆλŒ€ 쓰지 μ•ŠλŠ”λ‹€.)

  • let

    • ν• λ‹Ή 자유

    • 선언은 ν•œλ²ˆ

    • 블둝 μŠ€μ½”ν”„

  • const

    • ν• λ‹Ή ν•œλ²ˆ

    • μ„ μ–Έ ν•œλ²ˆ

    • 블둝 μŠ€μ½”ν”„

Identifier (μ‹λ³„μž) Convention

  • λ³€μˆ˜, ν•¨μˆ˜, 객체: 캐멀 μΌ€μ΄μŠ€ (camelCase)

  • class, μƒμ„±μž: 파슀칼 μΌ€μ΄μŠ€ (PascalCase)

  • μƒμˆ˜: λŒ€λΆ„μž μŠ€μΌ€μ΄ν¬ μΌ€μ΄μŠ€ (SNAKE_CASE)

III. Hoisting (ν˜Έμ΄μŠ€νŒ…)

  1. varλ₯Ό 쓰지 μ•ŠλŠ”λ‹€

  2. 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) ν•¨μˆ˜κ°€ μ•„λ‹Œ κ³³μ—μ„œλ§Œ μ‚¬μš©

  1. Class μ •μ˜μ—μ„œλŠ” arrow ν•¨μˆ˜ μ‚¬μš©ν•˜μ§€ 말자!

    • why?

      • this, arguments, super, new.target 등을 binding ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έ

  2. μƒμ„±μžλ‘œ μ‚¬μš© X

  3. 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