ν°μ€ν 리 λ·°
[JavaScript] νκΈ°λ², μλ£ν, DOM API, λ°μ΄ν° νμ νμΈ
devOhzl 2023. 11. 6. 11:29νκΈ°λ²
π dash-case (kebab-case)
[ HTML, CSS ] λ¨μ΄μ λ¨μ΄ μ¬μ΄μ λμ¬ '-' λ₯Ό μ¬μ©νλ€.
ex) the-quick-brown-fox
π snake_case
[ HTML, CSS ] λ¨μ΄μ λ¨μ΄ μ¬μ΄μ μΈλλ° '_' λ₯Ό μ¬μ©νλ€.
ex) the_quick_brown_fox
π camelCase
[ js ] μ μΌ μμ 첫κΈμλ μλ¬Έμμ΄λ©° λ€μ 첫κΈμλΆν°λ λλ¬Έμλ‘ μμνλ€.
ex) theQuickBrownFox
π PascalCAse
[ js ] λͺ¨λ 첫κΈμλ λλ¬Έμλ‘ μμνλ€.
ex) TheQuickBrownFox
π Zero-based Numbering
0 κΈ°λ° λ²νΈ λ§€κΈ°κΈ° π νΉμν κ²½μ°λ₯Ό μ μΈνκ³ 0λΆν° μ«μλ₯Ό μμν©λλ€.
μ£Όμ
// ν μ€
/*
μ¬λ¬ μ€
*/
/**
* μ¬λ¬ μ€
* λ©λͺ¨1
* λ©λͺ¨2
*/
μλ£ν
π Undefined
κ°μ΄ ν λΉλμ§ μμ μν
π Null
μ΄λ€ κ°μ΄ μλμ μΌλ‘ λΉμ΄μμ
λ³μ
: λ°μ΄ν°λ₯Ό μ μ₯νκ³ μ°Έμ‘°(μ¬μ©)νλ λ°μ΄ν°μ μ΄λ¦
π let
1οΈβ£ μ¬μ¬μ©μ΄ κ°λ₯
let a = 2;
let b = 5;
console.log(a+b); // 7
console.log(a-b); // -3
2οΈβ£ κ°(λ°μ΄ν°)μ μ¬ν λΉ κ°λ₯
let a = 12;
console.log(a); // 12
a = 999;
console.log(a); // 999
π const
1οΈβ£ κ°(λ°μ΄ν°)μ μ¬ν λΉ λΆκ°
const a = 12;
console.log(a); // 12
a = 999;
console.log(a); // TypeError : Assignment to constant variable
μμ½μ΄
: νΉλ³ν μλ―Έλ₯Ό κ°μ§κ³ μμ΄, λ³μλ ν¨μ μ΄λ¦ λ±μΌλ‘ μ¬μ©ν μ μλ λ¨μ΄
DOM API ( Document Object Model, Application Programming Interface )
π DOM API
μλ°μ€ν¬λ¦½νΈλ‘ HTMLμ μ μ΄νλ λͺ λ Ήλ€
DOM : HTML λ΄μ© , API : λͺ λ Ή
λΈλΌμ°μ λ μ½λλ₯Ό μμμ μλλ‘ λ΄λ € μ½μ΄κ°λ€.
script νκ·Έλ₯Ό title νκ·Έ μλμ μ°κ² λλ©΄, μλμ body λ΄μ©μ μ½μ§ λͺ»νλ€.
<script defer src="./main.js"></script>
script νκ·Έμ defer μμ±μ μΆκ° π HTML μ½λλ₯Ό λ€ μ½κ³ main.js λ₯Ό μ€νν¨
1οΈβ£
β ClassList.add(' ')
μ νν μμμ ν΄λμ€λ₯Ό μΆκ°νλ€.
β ClassList.contains(' ')
μ νν μμμ ν΄λμ€κ° μ‘΄μ¬νλμ§ νμΈνλ€.
β ClassList.remove(' ')
μ νν μμμ ν΄λμ€λ₯Ό μ κ±°νλ€.
<div class="box"></div>
let boxEl = document.querySelector('.box');
boxEl.addEventListener('click', function() {
boxEl.classList.add('active');
console.log( boxEl.classList.contains('active'); // true
boxEl.classList.remove('active');
console.log( boxEl.classList.contains('active'); // false
})
2οΈβ£
<div class="box"> 1 </div>
<div class="box"> 2 </div>
<div class="box"> 3 </div>
<div class="box"> 4 </div>
const boxEls = document.querySelectorAll('box');
boxEls.forEach(function(boxEl,index) {
boxEl.classList.add(`order-${index+1}`); // μμμ 'order-@' ν΄λμ€κ° μΆκ°λ¨
});
3οΈβ£
λ©μλ 체μ΄λ
split : λ¬Έμλ₯Ό μΈμ κΈ°μ€μΌλ‘ μͺΌκ°μ λ°°μ΄λ‘ λ°ν
reverse : λ°°μ΄μ λ€μ§κΈ°
join : λ°°μ΄μ μΈμ κΈ°μ€μΌλ‘ λ¬Έμλ‘ λ³ν©ν΄ λ°ν
const a = 'Hello~';
const b = a.split('').reverse().join(''); // λ©μλ 체μ΄λ
console.log(a); // Hello~
console.log(b); // ~olleH
λ°μ΄ν° νμ νμΈνκΈ°
1οΈβ£ typeof
console.log(typeof 'Hello') // 'string'
console.log(typeof 123) // 'number'
console.log(typeof undefined) // 'undefined'
console.log(typeof null) // 'object'
console.log(typeof {}) // 'object'
console.log(typeof []) // 'object'
null , Object , Arrayλ 'object'λ‘ λ¦¬ν΄λλ―λ‘ νμΈ λΆκ°λ₯νλ€.
2οΈβ£ constructor
console.log([].constructor) // Array()
console.log({}.constructor) // Object()
console.log(null.constructor) // TypeError: Cannot read properties of null (reading 'constructor')
nullμ νμΈ λΆκ°λ₯νλ€.
3οΈβ£ Object.prototype.toString.call
console.log(Object.prototype.toString.call(null).slice(8,-1)) // 'Null'
β νμ 체ν¬νλ ν¨μ λ§λ€κΈ°
function checkType(data) {
return Object.prototype.toString.call(data).slice(8,-1)
}
console.log(checkType('Hello')) //'String'
console.log(checkType(123)) // 'Number'
console.log(checkType(undefined)) // 'Undefined'
console.log(checkType([])) // 'Array'
console.log(checkType({})) // 'Object'
console.log(checkType(null)) // 'Null'
μΆμ²
νλ‘ νΈμλ μΉ κ°λ°μ λͺ¨λ κ² μ΄κ²©μ°¨ ν¨ν€μ§
'WEB > JavaScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JavaScript] μ°μ°μμ ꡬ문 (0) | 2023.12.12 |
---|---|
[JavaScript] μμμ κ°μμ± κ΄μ°° (IntersectionObserver) (0) | 2023.11.29 |
[JavaScript] λ°°μ΄μμ κ°μ²΄ key μμ±μΌλ‘ μ€λ¦μ°¨μ μ λ ¬νκΈ° (0) | 2023.08.21 |
[ JavaScript ] iframe μμμ λ°κΉ₯μ urlμ λ³κ²½νκΈ° (0) | 2023.06.21 |
[ JavaScript ] κΉμ볡μ¬(Deep Copy)μ μμ볡μ¬(Shallow Copy) (0) | 2023.06.19 |
- Total
- Today
- Yesterday
- echarts
- νκ²½μ€μ
- package-lock
- frontend
- chartjs
- Legend
- Figma κΈ°μ΄
- κ°μ²΄λ³΅μ¬
- web
- κΉμ볡μ¬
- BarChart
- νλ‘ νΈμλ
- Figma Style
- vscode
- figma
- piechart
- Vscodeλ¨μΆν€
- κ°μ²΄
- SASS
- package
- Location
- Figma λ²νΌ
- μμ볡μ¬
- npm
- Chart
- xμΆμ€ν¬λ‘€
- npm install
- SCSS
- javascript
- VUE
μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |