day11

μƒμ€πŸ‘ΈΒ·2023λ…„ 10μ›” 23일
1

λšœλ²…λšœλ²… λ‘λ²ˆμ§Έ

λͺ©λ‘ 보기
3/13
post-thumbnail
post-custom-banner

πŸ“– js

1. λ³€μˆ˜

2. μžλ£Œν˜•

3. μ—°μ‚°μž

(1) λΉ„κ΅μ—°μ‚°μž

== κ°™λ‹€ (κ°’λ§Œ νŒλ‹¨!)
=== κ°™λ‹€ (+μžλ£Œν˜• κΉŒμ§€ νŒλ‹¨)

πŸ€™

let v1 = 10;
let v2 = 10;
let v3 = "10";

console.log(v1==v2); => true (κ°’ κ°™μ•„)
console.log(v1==v3); => true (κ°’ κ°™μ•„)

console.log(v1===v2); => true (κ°’, μžλ£Œν˜• κ°™μ•„)
console.log(v1===v3); => false (κ°’ κ°™μ•„, μžλ£Œν˜• 달라)

!= λ‹€λ₯΄λ‹€ (κ°’λ§Œ νŒλ‹¨!)
!== λ‹€λ₯΄λ‹€ (+μžλ£Œν˜• κΉŒμ§€ νŒλ‹¨)

πŸ€™

console.log(v1!=v2); => false (λ‹€λ₯΄λƒ? μ•„λ‹ˆ κ°’ κ°™μ•„)
console.log(v1!==v3); => true (λ‹€λ₯΄λƒ? 응 μžλ£Œν˜• 달라)

πŸ‘€ν˜•λ³€ν™˜

Number() : μˆ«μžν˜•μœΌλ‘œ λ³€ν™˜

String() : λ¬Έμžν˜•μœΌλ‘œ λ³€ν™˜

πŸ€™

console.log(Number('123')); => 숫자 123
console.log(10 + String(20)); => 10 + '20' = 1020

4. iteration (반볡문)

β€»javaμ—μ„œ ν–₯μƒλœ forλ¬Έβ€»

for(int n : arr) {

}

πŸ“Œjsμ—μ„œ ν–₯μƒλœ forλ¬ΈπŸ“Œ

(1) for in : ν‚€

for(λ³€μˆ˜ in λ°°μ—΄O / 객체O) {
	λ³€μˆ˜ : ν‚€
}

(2) for of : κ°’

for(λ³€μˆ˜ of λ°°μ—΄O / 객체X) {		문법상 λͺ»μ“°κ²Œν•¨,,
	λ³€μˆ˜ : κ°’
}

πŸ€™ 배열인 경우!

let arr = ['A', 'B', 'C'];

for(key in arr) {
	console.log(key); => λ³€μˆ˜ : ν‚€ (0 1 2)
    console.log(arr[key]); => arr[0 1 2]의 κ°’
}

=>
0
A
1
B
2
C

for(value of arr) {
	console.log(value); => λ³€μˆ˜ : κ°’ (A B C)
}

=>
A
B
C

πŸ€™ 객체인 경우!

let desk = {
	size: 300,
    brand: 'bestdest',
    price: 15000
};

for(key in desk) {
	console.log(key); => λ³€μˆ˜ : ν‚€ (size brand price)
    console.log(desk[key]); => desk[size brand price]의 κ°’
}

=>
size
300
brand
bestdesk
price
15000

🚨🚨🚨

for(value of desk) {
	console.log(value); => λ³€μˆ˜ : κ°’  
}
=> 였λ₯˜λœ¬λ‹€!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

πŸ€™ λ°°μ—΄ + 객체인 경우!

let myPhone = [
	{ //0
    	brand: 'μ‚Όμ„±',
        modelName: 'λ…ΈνŠΈ9',
       	price: 200000
        discount: 50000
    };
    { //1
    	brand: 'μ• ν”Œ',
        modelName: '아이폰13',
       	price: 500000
    };
    { //2
    	brand: 'μ• ν”Œ',
        modelName: '아이폰15',
       	price: 600000
    };
    { //3
    	brand: 'μ‚Όμ„±',
        modelName: 'κ°€λŸ­μ‹œ23',
       	price: 800000
        option: 'μš©λŸ‰μΆ”κ°€'
    };
    
];

for(let key in myPhone) {
	console.log(key); //ν‚€(인덱슀) : 0 1 2 3 
}

for(let item of myPhone) {
	console.log(item); // κ°’ : 0 1 2 3의 κ°’
    console.log(item.modelName); // 0 1 2 3의 κ°’ 쀑에 modelName의 κ°’!
}

=>
{ brand: 'μ‚Όμ„±', modelName: 'λ…ΈνŠΈ9', price: 200000, discount: 50000 }
λ…ΈνŠΈ9
{ brand: 'μ• ν”Œ', modelName: '아이폰13', price: 500000 }
아이폰13
{ brand: 'μ• ν”Œ', modelName: '아이폰15', price: 600000 }
아이폰15
{ brand: 'μ‚Όμ„±', modelName: 'κ°€λŸ­μ‹œ23', price: 800000, option: 'μš©λŸ‰μΆ”κ°€' }
κ°€λŸ­μ‹œ23

πŸ’Ž myPhone λ°°μ—΄ -> λΈŒλžœλ“œκ°€ 삼성인 μ• λ“€λ§Œ 전체 정보λ₯Ό 좜λ ₯! - for in

for(let key in myPhone) {
	if(myPhone[key].brand == 'μ‚Όμ„±') { 
    	console.log(myPhone[key].brand);
        console.log(myPhone[key].modelName);
        console.log(myPhone[key].price);
        // 삼성인 μ• λ“€μ˜ brand, modelName, price 좜λ ₯!
    }
}

=>
μ‚Όμ„±
λ…ΈνŠΈ9
200000
μ‚Όμ„±
κ°€λŸ­μ‹œ23
800000

πŸ’Ž myPhone λ°°μ—΄ -> λΈŒλžœλ“œκ°€ 삼성인 μ• λ“€λ§Œ 전체 정보λ₯Ό 좜λ ₯! - for of

for(let item of myPhone) {
	if (item.brand == 'μ‚Όμ„±') {
          console.log(item.brand);
          console.log(item.modelName);
          console.log(item.price);
          console.log(item.discount);
          console.log(item.option);
    }
}

=>
μ‚Όμ„±
λ…ΈνŠΈ9
200000
50000
undefined
μ‚Όμ„±
κ°€λŸ­μ‹œ23
800000
undefined
μš©λŸ‰μΆ”κ°€

=> λΈŒλžœλ“œκ°€ 삼성인 값을 λΆˆλŸ¬μ˜€λŠ”λ° μ—†λŠ” ν•­λͺ©μ΄ undefined둜 뜨게 λœλ‹€

for(let item of myPhone) {
	if (item.brand == 'μ‚Όμ„±') {
          for(let key in item) {
	//item이 0 1 2 3의 κ°’ , κ·Έμ€‘μ—μ„œ brandκ°€ μ‚Όμ„±μΈκ²ƒλ§Œ λ‚˜μ—΄
				console.log(key + ':' + item[key]);
          }
    }
}

=>
brand : μ‚Όμ„±
modelName : λ…ΈνŠΈ9
price : 200000
discount : 50000
brand : μ‚Όμ„±
modelName : κ°€λŸ­μ‹œ23
price : 800000
option : μš©λŸ‰μΆ”κ°€

=> μ΄λŸ°μ‹μœΌλ‘œ undefined없이 λ‹€ λ‚˜μ—΄λœλ‹€!

5. function (ν•¨μˆ˜, λ©”μ†Œλ“œ)

μžλ°”μŠ€ν¬λ¦½νŠΈ ν•¨μˆ˜ μ •μ˜ 방법!

(1) 일반 ν•¨μˆ˜ @

function func1() {
	console.log('μΌλ°˜ν•¨μˆ˜');
}

func1(); //호좜 ν•΄μ€˜μ•Ό 좜λ ₯!!!

=> μΌλ°˜ν•¨μˆ˜

(2) 읡λͺ… ν•¨μˆ˜ //λ’€μͺ½μ— μ΄λ¦„μ—†λŠ” *** λ³€μˆ˜μ— ν•¨μˆ˜λ„ μ €μž₯ κ°€λŠ₯!! // const(μƒμˆ˜)

const func2 = function() {
	console.log('읡λͺ…ν•¨μˆ˜');
}

func2(); //호좜 ν•΄μ€˜μ•Ό 좜λ ₯!!!

=> 읡λͺ…ν•¨μˆ˜

(3) 넀이밍 ν•¨μˆ˜ //λ’€μͺ½μ— μ΄λ¦„μžˆλŠ” *** λ³€μˆ˜μ— ν•¨μˆ˜λ„ μ €μž₯ κ°€λŠ₯!! // const(μƒμˆ˜)

const func3 = function func3name() {
	console.log('λ„€μ΄λ°ν•¨μˆ˜');
}
 
func3(); //호좜 ν•΄μ€˜μ•Ό 좜λ ₯!!!

=> λ„€μ΄λ°ν•¨μˆ˜

(4) ν™”μ‚΄ν‘œλ°©μ‹ ν•¨μˆ˜ // *** λ³€μˆ˜μ— ν•¨μˆ˜λ„ μ €μž₯ κ°€λŠ₯!! // const(μƒμˆ˜)

const func4 = () => {
	console.log('ν™”μ‚΄ν‘œλ°©μ‹ ν•¨μˆ˜');
}

func4(); //호좜 ν•΄μ€˜μ•Ό 좜λ ₯!!!

=> ν™”μ‚΄ν‘œλ°©μ‹ ν•¨μˆ˜

λ§€κ°œλ³€μˆ˜

function func5(str) {
	console.log('μΌλ°˜ν•¨μˆ˜' + str);
}

func5('μΌλ°˜λ§€κ°œλ³€μˆ˜');

=>
μΌλ°˜ν•¨μˆ˜μΌλ°˜λ§€κ°œλ³€μˆ˜

return

function func6(n1, n2) {
	let result = n1 + n2;
    return result;
}

console.log(func6(1,2));

=> 3

πŸ“– html + js

1. μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ λ…Έλ“œ μ„ νƒν•˜κΈ°

πŸ“Œ <script></script> μ•ˆμ—μ„œ!!!! μ‚¬μš©!!!!!!

(1) getElementById

	document.getElementById('aid')
    =>idκ°€ aid인 것

(2) getElementsByClassName

	document.getElementsByClassName('box')[0]
    =>ν΄λž˜μŠ€μ΄λ¦„μ΄ box인 것쀑에 0인덱슀

(3) getElementsTagName

	document.getElementsByTagName('div')
    =>νƒœκ·Έκ°€ div인 것듀

(4) querySelector

	 document.querySelector('#aid')
     =>idκ°€ aid인 것 인데 μ„ νƒμž(#aid) μ‚¬μš©!

(5) querySelectorAll

	document.querySelectorAll('.box')
    =>ν΄λž˜μŠ€μ΄λ¦„μ΄ box인 것 인데 μ„ νƒμž(.box)μ‚¬μš©!

2. λ…Έλ“œ / 속성 μ‘°μž‘ν•˜κΈ°

(1) 컨텐츠 μ‘°μž‘ν•˜κΈ° (λ‚΄λΆ€ λ‚΄μš©)

textContent : λͺ¨λ“  ν…μŠ€νŠΈ
innerText : 화면에 λ³΄μ΄λŠ” ν…μŠ€νŠΈ
innetHTML : λͺ¨λ“  ν…μŠ€νŠΈ (html포함)

πŸ€™


<p id="p1"></p>
<p id="p2"></p>
<p id="p3"></p>
document.getElementById('p1').textContent = '<i>textContent</i>μΆ”κ°€';
//λ‹¨μˆœνžˆ ν…μŠ€νŠΈλ§Œ μΆ”κ°€
document.getElementById('p2').innerText = '<i>innerText</i>μΆ”κ°€';

document.getElementById('p3').innerHTML = '<i>innerHTML</i>μΆ”κ°€'; 
//ν…μŠ€νŠΈ + html / i:이타릭체 적용o

(2) μŠ€νƒ€μΌ μ‘°μž‘ν•˜κΈ°

μš”μ†Œ.style.CSS속성 = κ°’;

document.querySelector('#p1').style.color = red;
document.querySelector('#p2').style.color = "#0000FF"; //blue

=> πŸ’»

(3) 클래슀 μ‘°μž‘ν•˜κΈ°β­

μš”μ†Œ.classList.add('class이름'); : μΆ”κ°€ -> μ›λž˜ 있던 속성 + μΆ”κ°€ν•˜λŠ” 속성
μš”μ†Œ.classList.remove('class이름'); : μ‚­μ œ
μš”μ†Œ.classList.toggle('class이름'); : On/Off

(4) 속성 μ‘°μž‘ν•˜κΈ°

getAttribute('속성') => 속성값 가지고 μ˜€λŠ”
setAttribute('속성', '속성값') => 속성 μ„ΈνŒ… -> μ›λž˜ 있던 속성 λ‹€ μ—†μ• κ³  μ„ΈνŒ…ν•œ μ†μ„±λ§Œ λ“€μ–΄κ°„λ‹€!
getAttribute('속성') => 속성을 μ‚­μ œν•˜λŠ”

profile
λ’€μ£½λ°•μ£½ 벨둜그
post-custom-banner

0개의 λŒ“κΈ€