A language for manipulating the Browser
- Of the browser
- By the browser
- For the browser
JavaScript is a war with standards!
- Using JQuery may slow things down slightly, but it eliminates
cross-browsingissues
: A campaign saying let's stop using the JavaScript Libraries popping up everywhere!
- Vanilla == 'pure/original'
- Using JavaScript without installing any library!
windowis at the top
- window.document
Writing
- window.document.write()
Since the top is always
window, it can be omitted
: The principle of the feature is to provide a little leniency when evaluating the syntax of a JavaScript program by conceptually inserting missing semicolons
-
- DOM manipulation
- The reason JavaScript exists
: Reassignment is possible
: Reassignment is not possible !== the value doesn't change
-
Use lower camelCase!
-
No snake_case!
const addOneToNumber // good const add_one_to_number //bad
==,!==> X===,!===> O
-
Can be stored in a variable.
-
Can be a return value of a function.
-
Can be an argument to a function.
- Convert String-based HTML to Objects
- Understand and interpret
- Build a DOM Tree
- Render on screen (draw)
Summary:
- All data in the world is String (HTML)
- It must go through a parse/parsing process to be interpreted (DOM interprets)
const myP = document.createElement('p')
myP.innerText = 'Greater'
const myPStr = '<p>Greate</p>'
myPStr.classList // undifined -> because it's a string
myP.classList.add('wow') // object can do thisThe youngest sibling shut-in rule
- When there are curly braces, a Scope is created!!
- In case of a function
- In case of control statements (if / while / switch (control)), etc.
varwas only scoped within functionsletworks the same way when there are curly braces (scope)
var- Free assignment
- Free declaration
- Function scope (never use this.)
let- Free assignment
- Can only be declared once
- Block scope
const- Assign once
- Declare once
- Block scope
- Variables, functions, objects: camelCase (
camelCase) - Classes, constructors: PascalCase (
PascalCase) - Constants: uppercase snake case (
SNAKE_CASE)
- Don't use var
- Don't use function declaration form
- Using declaration form makes code not work as expected
- Why?
- It scans through once before execution, making references possible before declaration
- Why?
// Declaration form
function hello() {}
// Expression form
const hello = function() {}const num1 = 123
const num2 = -2.88
const a = NaN // Not a Number
const b = Infinity
const c = -Infinity- Concatenation
+
- Interpolation
template literal ${}
const firstName = 'Chloe'
const lastName = 'Kim'
const middleName = 'Juhyun'
const fullName = firstName + lastName
const greeting = `Hello ${middleName}`- What is close to true and what is close to false
true,false(lowercase)
All values that are not Falsy
[]- emptyArray{}- emptyObject
Non-existent or undefined things
nullundefined""0[] == true![]
How to store and manipulate (CRUD) => look at methods
- Array (
list)- array helper method
- Object (
dict)
- Declaration (statement)
- Expression
Use only where it is NOT a (Class) Method(OOP) function
- Don't use arrow functions in Class definitions!
- Why?
- Because they don't bind
this,arguments,super,new.target, etc.
- Because they don't bind
- Why?
- Cannot be used as a constructor X
- Cannot be used as an Event Listener Callback function X
ex)
// 1. Declaration
function add (num1, num2){
return num1 + num2
}
add(1,2)
// def add(num1, num2):
// 2. Expression
const sub = function(num1, num2){
return num1 - num2
}
sub(2,1)
// sub = lambda input: output
// 3. Arrow Function
const arrow = function(name){
return `hello ${name}`
}
const arrow = (name) => {
return `hello!! ${name}`
}
// When there is one argument
const arrow = name => {
return `hello!!! ${name}`
}
// Reduce to one line
const arrow = name => {return `hello!{name}`}
// Arrow function ultimate form
// (1) 1 argument
// (2) return exists & expression
const arrow = name => `hello!!!! ${name}`-
Very flexible OOP through objects
-
Forget about Classes for now!
- Prototypal Inheritance
-
classintroduced from ES6+
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())
// Can add in the middle!! -> flexible
bella.money = 100000000
console.log(bella.money)
/*
In 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())

