<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
const listener = (event) => {
const length = event.target.value.length; // textarea의 글자의 길이 값을 가져오자.
const h1 = document.querySelector('h1')
h1.textContent = `글자 수 : ${length}`
}
document.addEventListener('DOMContentLoaded', () => {
const textarea = document.querySelector('textarea')
textarea.addEventListener('keyup', listener)
})
// 이벤트 리스너가 외부로 분리되었습니다.
</script>
</head>
<body>
<h1></h1>
<textarea></textarea>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
const listener = function(event) {
const length = this.value.length; // textarea의 글자의 길이 값을 가져오자.
const h1 = document.querySelector('h1')
h1.textContent = `글자 수 : ${length}`
}
document.addEventListener('DOMContentLoaded', () => {
const textarea = document.querySelector('textarea')
textarea.addEventListener('keyup', listener)
})
// 이벤트 리스너가 외부로 분리되었습니다.
</script>
</head>
<body>
<h1></h1>
<textarea></textarea>
</body>
</html>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
document.addEventListener('DOMContentLoaded', () => {
const email = document.querySelector('input[type="email"]');
const message = document.querySelector('h2');
email.addEventListener('keyup', function(){
const email = this.value;
const isVaild = checkEmail(email);
if(isVaild){
message.textContent = '올바른 이메일 형식입니다.';
message.style.color = 'green';
}else{
message.textContent = '올바른 이메일 형식이 아닙니다.';
message.style.color = 'red';
}
});
});
function checkEmail(email){
const pattern = /^[a-zA-Z0-9._]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
return pattern.test(email);
}
</script>
</head>
<body>
<input type="email" placeholder="이메일을 입력하세요">
<h2></h2>
</body>
</html>
const message = document.getElementsByTagName('h2')[0]; 이렇게 사용해줬어야 한다.정규 표현식 마지막 부분은 최상위 도메인을 나타내는 부분으로, 영문 알파벳 대소문자가 포함될 수 있고, 최소 두 글자 이상이어야 한다.{2,} + $는 문자열의 끝을 나타낸다. + ^는 문자열의 시작
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
document.addEventListener('DOMContentLoaded', () => {
// 1부터 10까지의 숫자를 담은 배열 생성
const numbers = [];
for (let i = 1; i <= 10; i++) {
numbers.push(i);
}
// 알파벳 a부터 z까지의 문자를 담은 배열 생성
const alphabets = [];
for (let i = 97; i <= 122; i++) {
alphabets.push(String.fromCharCode(i));
}
document.getElementsByTagName('p')[0].innerHTML = numbers.join(", ");
document.getElementsByTagName('p')[1].innerHTML = alphabets.join(", ");
});
</script>
</head>
<body>
<p></p>
<p></p>
</body>
</html>

script를 head에서 쓸거면 body요소가 아직 브라우저에서 로드되지 않은 상태일 수 있으므로 DOMContetntLoaded 이벤트를 사용하여 문서가 완전히 로드된 후 스크립트를 실행해야 한다.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
document.addEventListener('DOMContentLoaded', () => {
let [timer, timerID] = [0,0];
const h1 = document.querySelector('h1');
const checkbox = document.querySelector('input');
checkbox.addEventListener('change', (event) => {
if(event.target.checked){
timerId = setInterval(() => {
timer += 1;
h1.textContent = `${timer}초`
}, 1000);
}else{
clearInterval(timerId);
}
});
});
</script>
</head>
<body>
<input type="checkbox">
<span>타이머 활성화</span>
<h1></h1>
</body>
</html>
화살표 함수로 event처리 할때 event.target은 실제로 이벤트를 발생시킨 요소를 가리킨다. >> 보통 하나의 요소만 가리
event.currentTarget은 현재 이벤트 핸들러가 연결된 요소를 가리킨다. >> 여러 요소 중 하나일 수 있다.이벤트가 해당 요소의 자식 요소에서 발생했을 떄는 위와 값이 다를 수 있음

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
document.addEventListener('DOMContentLoaded', () => {
let status = false;
const checkbox = document.querySelector('input');
checkbox.addEventListener('change', (event) => {
status = event.target.checked;
});
const link = document.querySelector('a');
link.addEventListener('click', (event) => {
if(!status){
event.preventDefault();
}
});
});
</script>
</head>
<body>
<input type="checkbox">
<span>링크 활성화</span>
<br>
<a href="https://www.naver.com">네이버</a>
</body>
</html>
preventDefault() 함수는 이벤트의 기본 동작을 취소한다. a태그의 클릭 이벤트를 처리할 때, 브라우저는 해당 링크의 주소로 이동하는 기본 동작을 수행하는데, 이러한 기본 동작을 원치 않을 때 호출해 중지시킬 수 있다. 
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>할 일 목록</title>
<script>
document.addEventListener('DOMContentLoaded', () => {
const text = document.querySelector('input[type="text"]');
const submit = document.querySelector('input[type="submit"]');
const todo = document.querySelector('#todoList');
submit.addEventListener('click', addTodoList);
text.addEventListener('keypress', (event) => {
if(event.key === 'Enter'){
event.preventDefault();
addTodoList();
}
});
function addTodoList(){
if(text.value.trim() === ""){
alert("할 일을 입력해주세요.");
return;
}
makeList(text.value);
}
function makeList(text){
const listItem = document.createElement("li");
const span = document.createElement("span");
const removeButton = document.createElement("button");
removeButton.textContent = '✖';
removeButton.style.marginLeft = '10px';
removeButton.style.cursor = 'pointer';
removeButton.style.border = 'none';
span.textContent = "✔";
span.style.cursor = 'pointer';
span.addEventListener('mouseenter', function() {
this.style.color = '#ccc';
});
span.addEventListener('mouseleave', function() {
this.style.color = '#222';
});
removeButton.addEventListener('mouseenter', function() {
this.style.color = '#ccc';
});
removeButton.addEventListener('mouseleave', function() {
this.style.color = '#222';
});
removeButton.addEventListener('click', () => {
todo.removeChild(listItem);
});
span.addEventListener('click', (event) => {
if(event.target === span){
if(listItem.classList.contains('checked')){
listItem.classList.remove('checked');
listItem.style.color = ''; // 원래 색으로
listItem.style.textDecoration = '';
removeButton.style.color = '';
// event.target.style.color = '#222';
} else{
listItem.classList.add('checked');
listItem.style.color = '#ccc';
listItem.style.textDecoration = 'line-through';
removeButton.style.color = '#ccc';
// event.target.style.color = '#ccc';
}
}
});
listItem.appendChild(span);
listItem.appendChild(document.createTextNode(" " + text));
listItem.appendChild(removeButton);
todo.appendChild(listItem);
text.value = "";
}
});
</script>
</head>
<body>
<h1>할 일 목록</h1>
<div id="container">
<input type="text">
<input type="submit" value="추가하기">
</div>
<ul id="todoList" style="list-style: none; font-size: 20px;"></ul>
</body>
</html>

문제점
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>할 일 목록</title>
<script>
document.addEventListener('DOMContentLoaded', () => {
const text = document.querySelector('input[type="text"]');
const submit = document.querySelector('input[type="submit"]');
const todo = document.querySelector('#todoList');
submit.addEventListener('click', addTodoList);
text.addEventListener('keypress', (event) => {
if(event.key === 'Enter'){
event.preventDefault();
addTodoList();
}
});
function addTodoList(){
if(text.value.trim() === ""){
alert("할 일을 입력해주세요.");
return;
}
makeList(text.value);
}
function makeList(text){
const listItem = document.createElement("li");
const span = document.createElement("span");
const removeButton = document.createElement("button");
removeButton.textContent = '✖';
removeButton.style.marginLeft = '10px';
removeButton.style.cursor = 'pointer';
removeButton.style.border = 'none';
span.textContent = "✔";
span.style.cursor = 'pointer';
// span.addEventListener('mouseenter', function() {
// this.style.color = '#ccc';
// });
// span.addEventListener('mouseleave', function() {
// this.style.color = '#222';
// });
removeButton.addEventListener('mouseenter', function() {
this.style.color = '#ccc';
});
removeButton.addEventListener('mouseleave', function() {
this.style.color = '#222';
});
removeButton.addEventListener('click', () => {
todo.removeChild(listItem);
});
span.addEventListener('click', (event) => {
console.log(event.target);
console.log(event.target === span);
if(event.target === span){
if(listItem.classList.contains('checked')){
listItem.classList.remove('checked');
listItem.style.color = ''; // 원래 색으로
listItem.style.textDecoration = '';
removeButton.style.color = '';
event.target.style.color = '#222';
} else{
listItem.classList.add('checked');
listItem.style.color = '#ccc';
listItem.style.textDecoration = 'line-through';
removeButton.style.color = '#ccc';
event.target.style.color = '#ccc';
}
}
});
listItem.appendChild(span);
listItem.appendChild(document.createTextNode(" " + text));
listItem.appendChild(removeButton);
todo.appendChild(listItem);
text.value = "";
}
});
</script>
</head>
<body>
<h1>할 일 목록</h1>
<div id="container">
<input type="text">
<input type="submit" value="추가하기">
</div>
<ul id="todoList" style="list-style: none; font-size: 20px;"></ul>
</body>
</html>
해결책
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>할 일 목록</title>
<style>
.checked {
color: grey;
}
.unchecked {
color: black;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', () => {
const text = document.querySelector('input[type="text"]');
const submit = document.querySelector('input[type="submit"]');
const todo = document.querySelector('#todoList');
submit.addEventListener('click', addTodoList);
text.addEventListener('keypress', (event) => {
if(event.key === 'Enter'){
event.preventDefault();
addTodoList();
}
});
function addTodoList(){
if(text.value.trim() === ""){
alert("할 일을 입력해주세요.");
return;
}
makeList(text.value);
text.value = "";
}
function makeList(todoText){
const listItem = document.createElement("li");
const span = document.createElement("span");
const removeButton = document.createElement("button");
removeButton.textContent = '✖';
removeButton.style.marginLeft = '10px';
removeButton.style.cursor = 'pointer';
removeButton.style.border = 'none';
span.textContent = "✔";
span.classList.add('unchecked'); // 초기 상태는 unchecked
span.style.cursor = 'pointer';
span.addEventListener('mouseenter', function() {
this.style.color = '#ccc';
});
span.addEventListener('mouseleave', function() {
this.classList.contains('checked') ? this.style.color = 'grey' : this.style.color = 'black';
});
removeButton.addEventListener('mouseenter', function() {
this.style.color = '#ccc';
});
removeButton.addEventListener('mouseleave', function() {
this.style.color = '#222';
});
removeButton.addEventListener('click', () => {
todo.removeChild(listItem);
});
span.addEventListener('click', () => {
if(listItem.classList.contains('checked')){
listItem.classList.remove('checked');
listItem.style.textDecoration = 'none';
span.classList.remove('checked');
span.classList.add('unchecked');
} else {
listItem.classList.add('checked');
listItem.style.textDecoration = 'line-through';
span.classList.add('checked');
span.classList.remove('unchecked');
}
});
listItem.appendChild(span);
listItem.appendChild(document.createTextNode(" " + todoText));
listItem.appendChild(removeButton);
todo.appendChild(listItem);
}
});
</script>
</head>
<body>
<h1>할 일 목록</h1>
<div id="container">
<input type="text">
<input type="submit" value="추가하기">
</div>
<ul id="todoList" style="list-style: none; font-size: 20px;"></ul>
</body>
</html>
위 코드로 하면 내가 원하고자 했던것으로 실행 가능
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.blue{
color:blue;
}
</style>
</head>
<body>
<h1>
<span>난 span 태그야</span>
</h1>
<script>
let $title = document.getElementsByTagName('h1')[0];
let p = document.createElement('p');
p.className = 'blue';
// p.classList.add('blue');
p.innerHTML = "난 JS로 추가된 p태그야";
$title.appendChild(p);
</script>
</body>
</html>
classListTest.html을 열고 f12를 눌러서 console창을 연다.
콘솔창에 다음 내용을 순서대로 타이핑하면서 결과를 본다.
p
p.className
p.className = 'red'
p
p.className = 'blue'
p
p.className = 'red blue'
p
p.className = 'blue'
p
p.classList
p.classList.add('red') // 클래스 추가
p
p.classList.item(0) // 0번째 클래스
p.classList.item(1) // 1번째 클래스
p.classList.item(2) // 2번째 클래스
p.classList.remove('red') // red 클래스 제거
p
p.classList.contains('blue') // 클래스 blue가 있으면 true 없으면 false
p.classList.contains('red')
p.classList.toggle('blue') // 클래스 blue가 있으면 클래스 blue를 삭제하고 없으면 추가한다.
p
p.classList.toggle('blue')
p
p.classList.replace('blue', 'yellow') // 클래스 blue를 yellow로 교체
p
console.log('Cat' && 'Dog');
console.log('Cat' || 'Dog');
console.log(true || 'anything');
console.log(false || 'anything');
console.log(true && 'anything');
console.log(false && 'anything');
var done = true;
var message = '';
if (done) message = '완료';
message = done && '완료';
console.log(message);
var done = true;
var message = '';
if (done) message = '완료'
else message = '미완료'
console.log(message);
message = done ? '완료' : '미완료';
console.log(message);
12-1
// ES6의 매개변수의 기본값 설정
function getStringLength(str = '') {
return str.length
}
javascipt는 head태그 안에 선언하는 것 보다 body태그의 끝에 선언하는게 더 좋은 방법이다.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>주문하기</title>
<script>
document.addEventListener('DOMContentLoaded', () => {
const order = document.querySelector('h2');
const submit = document.querySelector('input');
submit.style.cursor = 'pointer';
submit.addEventListener('click', () => {
const cart = document.getElementsByTagName('p')[2];
cart.textContent = order.textContent;
cart.style.color = 'blue';
submit.disabled = true; //한번 클릭하면 submit버튼 비활성화
});
});
</script>
</head>
<body>
<h1>상품 설명</h1>
<h2>HTML + CSS + 자바스크립트 웹 표준의 정석</h2>
<p>한 권으로 끝내는 웹 기본 교과서</p>
<p>코딩 왕초보도 OK! 기초부터 활용까지 완전 정복</p>
<input type="submit" value="주문하기">
<hr>
<p></p>
</body>
</html>

선생님 풀이
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>노드 추가하기</title>
<style>
#container {
border-bottom: 1px dashed #222;
margin-bottom: 20px;
}
h1,
h2,
button,
p {
margin-bottom: 20px;
}
#cart {
padding: 20px;
}
</style>
</head>
<body>
<div id="container">
<h1>상품 설명</h1>
<h2>HTML+CSS+자바스크립트 웹 표준의 정석</h2>
<p>한 권으로 끝내는 웹 기본 교과서 </p>
<p>코딩 완초보도 OK! 기초부터 활용까지 완전정복</p>
<button id="order">주문하기</button>
</div>
<div id="orderInfo"></div>
<script>
const orderInfo = document.querySelector("#orderInfo"); // 주문 정보 영역
const orderButton = document.querySelector("#order"); // 주문하기 버튼
const title = document.querySelector("#container > h2"); // 책 제목 요소
orderButton.addEventListener("click", () => {
let newP = document.createElement("p"); // 새로운 p 요소 생성
let textNode = document.createTextNode(title.innerText); // title의 텍스트 부분만 가져와 텍스트 노드로 생성
newP.appendChild(textNode); // 텍스트 노드를 새로운 p 요소에 추가
newP.style.fontSize = "0.8em";
newP.style.color = "blue";
orderInfo.appendChild(newP); // 새로운 p 요소를 orderInfo 영역에 추가
}, { once: true });
// click 이벤트가 여러 번 발생해도 한 번만 실행된다.
</script>
</body>
</html>
{once: ture} : click이벤트를 한번말 수용하게끔 한다.<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>참가자 명단</title>
<style>
table {
border-collapse: collapse; /* 테이블 셀의 테두리를 겹치도록 설정 */
}
th, td {
border: 1px solid rgba(0, 0, 0, 0.2); /* 테이블 셀의 테두리 스타일 설정 */
padding: 10px 50px; /* 셀 내부 여백 설정 */
text-align: center; /* 셀 안의 텍스트 정렬 설정 */
}
</style>
<script>
document.addEventListener('DOMContentLoaded', () => {
const submit = document.querySelector('input[type="submit"]');
const nameInput = document.getElementById('name');
const majorInput = document.getElementById('major');
const body = document.getElementById('participantList');
submit.addEventListener('click', (event) => {
event.preventDefault();
const name = nameInput.value.trim();
const major = majorInput.value.trim();
if(name === '' || major === ''){
alert('이름과 전공을 올바르게 입력하세요.');
return;
}
const newRow = document.createElement('tr');
const nameCell = document.createElement('td');
const majorCell = document.createElement('td');
majorCell.textContent = major;
nameCell.textContent = name;
newRow.appendChild(nameCell);
newRow.appendChild(majorCell);
body.appendChild(newRow);
nameInput.value = '';
majorInput.value = '';
nameInput.focus(); //입력이 끝나면 포커스가 다시 이름 입력 칸에 가게끔
});
});
</script>
</head>
<body>
<h1>참가자 명단</h1>
<div id="info">
<p style="margin-left: 50px;">이 름
<input type="text" id="name" placeholder="이름을 입력하세요.">
</p>
<p style="margin-left: 50px;">전 공
<input type="text" id="major" placeholder="본인의 전공을 입력하세요.">
</p>
<input type="submit" value="등록하기" style="padding: 10px 105px;">
<table style="margin-top: 10px;">
<thead>
<tr>
<th>이름</th>
<th>전공</th>
</tr>
</thead>
<tbody id = "participantList">
</tbody>
</table>
</div>
</body>
</html>

선생님 풀이
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>연습문제 1</title>
<style>
#container {
width: 400px;
margin: 10px;
}
#userInput {
list-style: none;
width: 400px;
}
#userInput label {
width: 50px;
margin-right: 10px;
line-height: 30px;
}
#userInput input {
width: 200px;
height: 30px;
margin-bottom: 10px;
}
button {
width: 300px;
padding: 15px;
}
#attendant {
margin-top: 20px;
}
table {
border: 1px solid #ccc;
border-collapse: collapse;
}
th,
td {
border: 1px solid #ccc;
padding: 10px;
}
th:first-of-type {
width: 150px;
}
th:last-of-type {
width: 250px;
}
</style>
</head>
<body>
<div id="container">
<h1>참가자 명단</h1>
<form>
<ul id="userInput">
<li>
<label for="username">이 름</label>
<input type="text" id="username">
</li>
<li>
<label for="major">전 공</label>
<input type="text" id="major">
</li>
</ul>
<button>등록하기</button>
</form>
<table id="attendant">
<thead>
<tr>
<th>이 름</th>
<th>전 공</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script>
const username = document.querySelector("#username");
const major = document.querySelector("#major");
const bttn = document.querySelector("form > button");
bttn.addEventListener("click", (e) => {
e.preventDefault();
let tbody = document.querySelector("#attendant > tbody");
let newTr = document.createElement("tr");
// 첫번째 셀에 입력한 이름 넣기
let nameTd = document.createElement("td");
nameTd.innerText = username.value;
username.value = "";
// 두번째 셀에 입력한 전공 넣기
let majorTd = document.createElement("td");
majorTd.innerText = major.value;
major.value = "";
// 두 셀을 새 줄에 추가하기
newTr.appendChild(nameTd);
newTr.appendChild(majorTd);
// 새 줄을 tbody에 추가하기
tbody.appendChild(newTr);
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
box-sizing: border-box;
}
body {
display: flex;
margin: 0;
min-height: 100vh;
justify-content: center;
align-items: center;
position: relative;
}
button {
background-color: #ddd;
padding: 1rem 3rem;
border: 1px solid #222;
border-radius: 5px;
box-shadow: 1px 1px 2px #222;
}
/* noti-box로 우측 하단에 고정해 놓고, .noti를 그 안에 넣을 것! */
#noti-box {
position: fixed; /*고정 */
top: 10px;
right: 20px;
}
.noti {
margin: 1rem;
padding: 1rem;
background-color: #eee;
border-left: 3px solid #222;
}
</style>
</head>
<body>
<div id="noti-box"></div>
<button id="btn">클릭!</button>
<script>
const btn = document.querySelector('#btn');
const notiBox = document.querySelector('#noti-box');
btn.addEventListener('click', () => {
const noti = document.createElement('div');
noti.classList.add('noti');//class가 noti인 div가 생김
noti.innerText = '알림 내용이 표시됩니다.';
notiBox.appendChild(noti);
setTimeout(() => { //timeInterval을 계속 반복 setTimeout은 지정된 시간동안 한번 실행
noti.remove();//remove는 부모에서만 가능
}, 3000);
});
</script>
</body>
</html>
</script>
</body>
</html>
setInterval : 지정된 시간 간격마다 함수를 반복적으로 실행 >> 명시된 시간마다 함수가 계속 호출
setTimeout : 지정된 시간이 경과한 후에 한 번만 함수를 실행
clearTimout : 명시적으로 호출해 실행을 중지해야함
비동기적인 작업을 수행할 때 예상치 못한 결과를 피하기위해 명시적으로 중지하는 것이 좋다.
설정하지 않으면 이전에 설정한 타이머가 여전히 대기상태에 있고, 실행이 끝난 후에 함수가 예기치 못하게 호출될 수 있다.

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
box-sizing: border-box;
}
p{
display: inline-block;
background-color: black;
border-radius: 8px;
color: white;
padding: 10px 20px;
margin-left: 15px;
}
img{
margin-left: 15px;
}
</style>
</head>
<body>
<p>ENJOY YOUR LIFE</p> <br>
<img src="" alt="현재 시간에 맞는 사진" id = "img">
<script>
const now = new Date();
const hours = now.getHours();
const p = document.querySelector('p');
const img = document.querySelector('img');
img.src = (hours < 12) ? "morning.jpg" : "afternoon.jpg";
</script>
</body>
</html>

선생님 풀이
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>노드 추가하기</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
</style>
<style>
h1 {
padding: 10px 20px;
margin-bottom: 20px;
border-radius: 8px;
background-color: #222;
color: #fff;
text-transform: uppercase;
}
</style>
</head>
<body>
<div id="container">
<h1>Enjoy your life</h1>
</div>
<script>
const today = new Date(); // 현재 날짜와 시간 정보를 담은 today 객체
const hrs = today.getHours(); // 현재 시간 중 시(hour) 정보 가져오기
const container = document.querySelector("#container");
let newImg = document.createElement("img");
newImg.src = (hrs < 12) ? "images/morning.jpg" : "images/afternoon.jpg";
container.appendChild(newImg);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 변수를 선언합니다.
const pet = {
name: '구름',
eat: function (food) {
alert(this.name + '은/는 ' + food + '을/를 먹습니다.')
}
}
// 메소드를 호출합니다.
pet.eat('밥')
</script>
</head>
<body></body>
</html>
객체: 이름(name)과 값(value)로 구성된 속성(property)을 가진 자바스크립트의 기본 데이터 타입
배열은 객체를 기반으로 만들어 유사하지만, 배열은 요소에 접근할 때 인덱스를 사용하고, 객체는 키(key)를 사용한다.
위 코드에서는 pet객체가 name이라는 속성과 eat이라는 메소드를 가지고있고, food라는 매개변수를 받아들여 alert창을 띄우는 역할을 한다.
<script>
const product = {
제품명 : '7D 건조 망고', // 키와 값 뒤에 쉼표(,)를 넣어 구분합니다.
유형: '당절임',
성분: '망고, 설탕, 메타중아황산나트륨, 치자황색소',
원산지 : '필리핀'
}
</script>
객체 뒤에 대괄호 [...]를 사용하고 키를 입력하면 객체의 요소에 접근할 수 있다. >> 대괄호로만 접근 가능
product['제품명'] : 7D 건조 망고 반환 >> product.제품명 이렇게도 접근 가능
product['유형'] : 당절임 반환 >> product.유형
객체는 위와 같이 대괄호[...]를 사용하는 방법 이외에 온점(.)을 사용할 수 있다.
배열 내부에 있는 값을 요소(element)다.
객체 내부에 있는 값은 속성(property)다.
const object = {
number : 273,
string : '구름',
boolean : true,
array : [52, 273, 103, 32],
method : function () {}
}
<script>
const person = {
name : '구름',
eat : function(food) {}
}
// 메소드를 호출합니다.
person.eat()
<script>
기본적으로 화살표 함수는 메소드로 사용하지 않는다.
<script>
// 변수를 선언합니다.
const pet = {
name : '구름',
eat : function(food) {
alert(this.name + '은/는 ' + food + '을/를 먹습니다.')
}
}
pet.eat('밥');
</script>
추가하기
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 객체를 선언합니다.
const student = {}
student.이름 = '윤인성'
student.취미 = '악기'
student.장래희망 = '생명공학자'
// 출력합니다.
console.log(JSON.stringify(student, null, 2))
</script>
</head>
<body></body>
</html>
JSON.stringify() : JSON 문자열로 변환해 콘솔에 출력한다.
제거하기
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 객체를 선언합니다.
const student = {}
student.이름 = '윤인성'
student.취미 = '악기'
student.장래희망 = '생명공학자'
// 객체의 속성을 제거합니다.
delete student.장래희망
// 출력합니다.
console.log(JSON.stringify(student, null, 2))
</script>
</head>
<body></body>
</html>
<!--
동적으로 객체 속성 제거하기
delete 객체.속성
-->
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 변수를 선언합니다.
const test = {
a: function () { // 키를 주고 익명함수로 선언 >> test객체
console.log(this)
},
b: () => { // 화살표 함수로 선언 >> window객체
console.log(this)
}
}
// 메소드를 호출합니다.
test.a()
test.b()
</script>
</head>
<body></body>
</html>
메소드 내부에서 this 키워들를 사용할 때 의미가 달라지므로 화살표 함수를 메소드로 사용하지 않는 편이다.
화살표 함수에서의 this는 window객체를 나타낸다.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
const pet = {
name: '구름',
eat: function (food) {
alert(this.name + '은/는 ' + food + '을/를 먹습니다.')
}
}
pet.eat('밥');
</script>
</head>
<body>
</body>
</html>

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
const student = {
이름 : '윤인성',
취미 : '악기',
장래희망 : '생명공학자'
}
console.log(JSON.stringify(student, null, 2))
</script>
</head>
<body>
</body>
</html>

이렇게 객체명 명시해서도 가능
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 객체를 선언합니다.
const student = {}
student.이름 = '윤인성'
student.취미 = '악기'
student.장래희망 = '생명공학자'
// 출력합니다.
console.log(JSON.stringify(student, null, 2))
</script>
</head>
<body>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
const student = {
이름 : '윤인성',
취미 : '악기',
장래희망 : '생명공학자'
}
delete student.장래희망;
console.log(JSON.stringify(student, null, 2))
</script>
</head>
<body>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 객체를 선언합니다.
const pet = {
name: '구름',
eat (food) {
alert(this.name + '은/는 ' + food + '을/를 먹습니다.')
}
}
// 메소드를 호출합니다.
pet.eat('밥')
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
const test = {
a : function() {
console.log(this);
}
,b : () => {
console.log(this);
}
}
test.a();
test.b();
</script>
</head>
<body>
</body>
</html>

let input = 일자,달러,엔,유로 02,1141.8,1097.46,1262.37 03,1148.7,1111.36,1274.65 04,1140.6,1107.81,1266.58 07,1143.4,1099.58,1267.8 08,1141.6,1091.97,1261.07;
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
let input = `
일자,달러,엔,유로
02,1141.8,1097.46,1262.37
03,1148.7,1111.36,1274.65
04,1140.6,1107.81,1266.58
07,1143.4,1099.58,1267.8
08,1141.6,1091.97,1261.07
`;
let rows = input.trim().split('\n').map(a => a.trim().split(','));
console.log(rows);
</script>
</head>
<body>
</body>
</html>

처음에 trim()을 이용해, 공백을 제거해주고, split('\n') 을 이용해 개행을 기준으로 배열로 반환 후
map함수 이용해 다시 trim()과 split(',')을 이용해 쉼표를 기준으로 배열을 생성해 반환한다.
최종적으로 2차원 배열이 되며, 각 행은 쉼표를 기준으로 분할된 값을 가지게 되는 것이다.
아래에서 한 prototype을 이용해 함수 추가한 예제
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// power() 메소드를 추가합니다.
Number.prototype.power = function (n = 2) {//매개변수를 안넘겨 주면 n은 2로
return this.valueOf() ** n
}
// Number 객체의 power() 메소드를 사용합니다.
const a = 12
console.log('a.power():', a.power())
console.log('a.power(3):', a.power(3))
console.log('a.power(4):', a.power(4))
</script>
</head>
<body></body>
</html>
객체 자료형
//배열 속성 확인
const a = []
//undefined
a.sample = 10
//10
a.sample
//10
//함수 속성 확인
function b() {}
//undefined
b.sample = 10
//10
b.sample
//10
typeof연산자를 사용해 배열의 자료형을 확인해보면 "object"객체가 출력된다.
함수는 '실행이 가능한 객체'라는 특이한 자료로 typeof 연산자를 사용해서 자료형을 확인하면 "function"을 출력한다.
기본 자료형
숫자, 문자열, 불이 바로 기본 자료형이다.
new를 사용하지 않고 기본 자료형뒤에 온점을 찍고 속성을 넣으면 자바스크립트에서 일시적으로 객체로 승급시킨다. 그러나 실제로는 undefined로, 실질적으로 해당 속성을 유지할 수 없다. 그러므로 속성과 메소드를 추가로 가질 수 없다 >> new를 사용하지 않는다면의 예시
const 객체 = new 객체 자료형 이름() >> 이렇게 사용하면 기본 자료형으로 객체 생성 가능
new Number(10)
new String('안녕하세요')
new Boolean(true)
위와같이 하면 기본 자료형이 아닌 객체이므로 이전과 다른 속성을 가진다.
const f = new Number(273)
//undefined
typeof f
// "object"
f.sample = 10
// 10
f.sample
// 10
// 속성을 가질 수 있습니다.
f
// Number{273, sample: 10}
// 콘솔에서 단순하게 f를 출력하면 객체 형태로 출력합니다.
f + 0
// 273
f.valueOf()
// 273
// 숫자와 똑같이 활용할 수 있고 valueof 메소드를 사용해서 값을 추출할 수도 있습니다.
new 키워드를 사용하지 않을 때 주의할 점
const g = Number(273)
//undefined >> new를 안쓰면 그냥 형변환 시키는
typeof g // 객체가 아닙니다.
//"number"
prototype : 기본 자료형의 승급 때 일시적으로 입는 옷을 아예 새 옷으로 변경시키는 속성
prototype 객체에 속성과 메소드를 추가하면 모든 객체에서 해당 속성과 메소드를 사용할 수 있다. >> 기본 자료형 포함
객체 자료형 이름.prototype.메소드 이름 = function() {}
Number.prototype.sample = 10
//10
const i = 273
//undefined
i.sample
//10
// sample 속성을 갖고 있습니다.
//제곱 연산자 **
2 ** 2
//4
2 ** 3
//8
2 ** 4
//16
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// prototype을 통해 contain() 메소드를 추가합니다.
String.prototype.contain = function (data) {
return this.indexOf(data) >= 0 //data 가 배열에 존재하는지 확인
}
Array.prototype.contain = function (data) {
return this.indexOf(data) >= 0
}
// String 객체의 contain() 메소드를 사용합니다.
const a = '안녕하세요'
console.log('안녕 in 안녕하세요:', a.contain('안녕'))//true
console.log('없는데 in 안녕하세요', a.contain('없는데'))//false
// Array 객체의 contain() 메소드를 사용합니다.
const b = [273, 32, 103, 57, 52]
console.log('273 in [273, 32, 103, 57, 52]:', b.contain(273))//true
console.log('0 in [273, 32, 103, 57, 52]:', b.contain(0))//false
</script>
</head>
<body></body>
</html>

const l = 123.456789
//undefined
l.toFixed(2)
//"123.46"
l.toFixed(3)
//"123.457"
l.toFixed(4)
//"123.4568"
// NaN(Not a Number)은 '숫자가 아니다'라는 의미로 자바스크립트에서 숫자로 나타낼 수 없는
숫자를 의미한다.
const m = Number('숫로 변환할 수 없는 경우')
//undefined
m
//NAN
m === NaN
//false
// NaN과 비교해서는 NaN인지 확인할 수 없다.
Number.isNaN(m)
//true
//infinity : 숫자를 0으로 나누는 것과 같이 무한대 숫자를 의미한다.
const n = 10 / 0
//undefined
n
//Infinity // 양의 무한대를 생성한다.
const o = -10 / 0
//undefined
o
//-Infinity // 음의 무한대를 생성한다.
Number.isFinite(n)
//false
Number.isFinite(o)
//false
Number.isFinite(1)
//true
Number.isFinite(10)
//true
n === Infinity || n === -Infinity >> trueJSON(JavaScript Object Notation) : 서버와 클라이언트 간에 자료를 주고받기위해 양쪽 컴퓨터 모두 이해할 수 있는 표준형식 문자열
JSON 형식
JSON은 중괄호 { 와 } 사이에 '이름(name)'과 '값(value)'으로 구성되고 쉼표로 구분해서 여러개의 쌍을 나열할 수 있다.
기본형 {
"이름" : 값,
...
}
JSON에서는 '이름' 부분에 반드시 큰따옴표를 붙여야 한다.
JSON 이름에는 공백(space)이나 하이픈(-), 언더바(_)를 함께 사용할 수 있다.
//객체로 표현할 때
{
name : "도레미",
major : "컴퓨터 공학",
grade : 2
}
//JSON으로 표현할 때
{
"name" : "도레미",
"major" : "컴퓨터 공학",
"grade" : 2
}
//맞게 사용한 예
{ "name" : "도레미"}
//잘못 사용한 예
{'name' : "도레미"}
{ name : "도레미"}
//이름에 공백, 하이픈, 언더바 사용 가능 >> 근데 언더바 사용이 좋음
{ "full name" : "도레미" }
{ "full-name" : "도레미" }
{ "full_name" : "도레미" } //이게 좋음
JSON의 값
JSON 자료의 값에서는 수(number)와 문자열(string), 논리값(boolean), null과 같은 기본 자료형과 배열, 객체와 같은 복합 자료형을 사용할 수 있다.
숫자형 : JSON에서는 정수와 실수 모두 사용할 수 있지만, 8진수나 16진수를 사용한 표기법은 지원하지 않는다. >> 48, -72, 25.8, -8.4, 2.3e4
문자열 : JSON 문자열은 항상 큰따옴표("")로 묶어야 한다. >> 작은 따옴표 사용 x
"자바스크립트", "HTML5"논리값과 null : true 또는 false 값을 가지는 논리형을 사용할 수도 있고 null 유형도 사용할 수 있다.
JSON 문자열에는 또 다른 JSON 문자열이나 배열을 값으로 사용할 수 있다. 대괄호 사용
//배열 사용해 3개의 값 지정
{
"name" : "도레미",
"major" : "컴퓨터 공학",
"grade" : 2,
"course" : ["웹 기초", "자바스크립트", "인공지능"]
}
//JSON문자열 안에 또 다른 JSON 문자열 지정
{
"name" : "도레미",
"major" : "컴퓨터 공학",
"grade" : 2,
"course" : {
"title" : "웹 기초",
"timePerWeek" : 3
}
}
서버로 자료를 보내야 할 경우에는 객체 형식을 JSON 형식으로 변환해야한다. >> '직렬화(stringify)'
let student = {name:"도레미", major:"컴퓨터 공학", grade:2}//student객체 생성
let json = JSON.stringify(student); //JSON형식으로 변경해 json 변수에 할당
//'{"name" : "백두산", "age" : 30, "hobby" : "swimming"}'` 이런 형식으로 json에 저장되어있다.
let member = '{"name" : "백두산", "age" : 30, "hobby" : "swimming"}'<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 자료를 생성합니다.
const data = [{
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
},{
name: 'HTML5 웹 프로그래밍 입문',
price: 26000,
publisher: '한빛아카데미'
}]
// 자료를 JSON으로 변환합니다.
const json = JSON.stringify(data)
console.log(json)
// JSON 문자열을 다시 자바스크립트 객체로 변환합니다.
console.log(JSON.parse(json))
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
const num = Math.random()
console.log('# 랜덤한 숫자')
console.log('0~1 사이의 랜덤한 숫자:', num)
console.log('')
console.log('# 랜덤한 숫자 범위 확대')
console.log('0~10 사이의 랜덤한 숫자:', num * 10)
console.log('0~50 사이의 랜덤한 숫자:', num * 50)
console.log('')
console.log('# 랜덤한 숫자 범위 이동')
console.log('-5~5 사이의 랜덤한 숫자:', num * 10 - 5)
console.log('-25~25 사이의 랜덤한 숫자:', num * 50 -25)
console.log('')
console.log('# 랜덤한 정수 숫자')
console.log('-5~5 사이의 랜덤한 정수 숫자:', Math.floor(num * 10 - 5))
console.log('-25~25 사이의 랜덤한 정수 숫자:', Math.floor(num * 50 - 25))
</script>
</head>
<body></body>
</html>
<script src="https://cdn.jsdelivr.net/npm/lodash/lodash.min.js"></script> 이거 head에 넣어야 Loadash사용 가능구글에서 "popular javascript library 2023"으로 검색하면 다양한 라이브러리들이 나오므로 살펴보기 바란다.
underscore는 _ 기호를 의미한다. Loadash는 Low Dash를 줄인 것이며, 마찬가지로
_기호를 의미하는 말이다.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js">
</script>
<script>
// 데이터를 생성합니다.
const books = [{
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}, {
name: 'HTML5 웹 프로그래밍 입문',
price: 26000,
publisher: '한빛아카데미'
}, {
name: '머신러닝 딥러닝 실전 개발 입문',
prie: 30000,
publisher: '위키북스'
}, {
name: '딥러닝을 위한 수학',
price: 25000,
publisher: '위키북스'
}]
// 가격으로 정렬한 뒤 출력합니다.underscore를 이용
const output = _.sortBy(books, (book) => book.price)
console.log(JSON.stringify(output, null,2))
</script>
</head>
<body></body>
</html>

확인 문제
<script>
const num = 52000
num.원 = function() {
return this.valueOf() + '원'
}
console.log(num.원())
</script>
<script>
function printLang(code){
return printLang._lang[code]
}
printLang._lang = {
ko: '한국어',
en: '영어',
ja: '일본어',
fr: '프랑스어',
es: '스페인어'
}
console.log('printLang("ko"):', printLang('ko'))
console.log('printLang("en"):', printLang('en'))
</script>
① classProp ② prototype ③sample ④frame
<script>
// 데이터를 생성합니다.
const books = [{
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}, {
name: 'HTML5 웹 프로그래밍 입문',
price: 26000,
publisher: '한빛아카데미'
}, {
name: '머신러닝 딥러닝 실전 개발 입문',
prie: 30000,
publisher: '위키북스'
}, {
name: '딥러닝을 위한 수학',
price: 25000,
publisher: '위키북스'
}]
</script>
풀이
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 데이터를 생성합니다.
const books = [{
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}, {
name: 'HTML5 웹 프로그래밍 입문',
price: 26000,
publisher: '한빛아카데미'
}, {
name: '머신러닝 딥러닝 실전 개발 입문',
prie: 30000,
publisher: '위키북스'
}, {
name: '딥러닝을 위한 수학',
price: 25000,
publisher: '위키북스'
}]
const output = _.orderBy(books, 'name', 'asc');//name을 기준으 오름차순으로
console.log(JSON.stringify(output, null, 2));//보기좋게 출
</script>
</body>
</html>
크롬 콘솔 창에서 빈 배열 하나를 만들고 거기에 sample이라는 속성을 동적으로 하나 만들고 값에 10을 주자. 그런 후에 그 배열에 sample이라는 속성 값을 출력해 보자.
let arr = [];
arr.sample = 10;
arr.sample;//object
function b() {}
b.sample = 10
b.sample
//10
Array.isArray()const c = 273
c.sample = 10
c.sample
undefined // 속성을 만들 수 있는 것처럼 보이지만 실제로 속성이 만들어지지 않습니다.
자바스크립트는 사용의 편리성을 위해서 기본 자료형의 속성과 메소드를 호출할 때(기본 자료형 뒤에 온점을 찍고 무언가 하려고 하면) 일시적으로 기본 자료형을 객체로 승급시킵니다. 그래서 속성과 메소드를 사용할 수 있는 것입니다.
숫자 전체 객체 전부에 sample이라는 속성을 추가하고 10 이라는 값을 넣자.
그리고 그것을 크롬 콘솔 창에서 확인하자.
//Number은 기본 자료형으로 prototype을 사용해 속성을 추가해야한다.
//안한 예
Number.sample = 20;
(5).sample; //undefined
//prototype 사용 예
Number.prototype.sample = 30;
(3).sample; //30
Number.prototype.power = function (n = 2) {
return this.valueOf() ** n
}
// Number 객체의 power() 메소드를 사용합니다.
const a = 12;
a.power();
a.power(3);
a.power(4);
//Math.pow(); 로도 사용 가능
Math.pow(2, 2); //4
**기호를 사용한다.String.prototype.contain = function (data) {
return this.indexOf(data) >= 0
}
const a = '안녕하세요'
a.contain('하세요'); // true
a.contain('수고'); // false
Array.prototype.contain = function (data) {
return this.indexOf(data) >= 0
}
const b = [273, 32, 103, 57, 52]
b.contain(32); // true
b.contain(123); // false
toFixed() : 소수점 이하 몇 자리까지만 출력하고 싶을 때 사용let number = 123.456789;
let roundedNumber = number.toFixed(2); // 소수점 둘째 자리까지 반올림
console.log(roundedNumber); // 출력: "123.46"
.isFinite() : 양의 무한대, 음의 무한대라면 false >> 비교 연산자로 비교 가능.isNaN() : 비교값 === NaN 하면 무조건 false가 나오므로 이 함수를 이용해 확인해야한다.const m = Number('숫자로 변환할 수 없는 경우')
m
//NAN
m === NaN //?
isNaN()함수를 사용해야 한다// 데이터를 생성합니다.
const books = [{
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}, {
name: 'HTML5 웹 프로그래밍 입문',
price: 26000,
publisher: '한빛아카데미'
}, {
name: '머신러닝 딥러닝 실전 개발 입문',
prie: 30000,
publisher: '위키북스'
}, {
name: '딥러닝을 위한 수학',
price: 25000,
publisher: '위키북스'
}]
풀이
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js">
</script>
<script>
// 데이터를 생성합니다.
const books = [{
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}, {
name: 'HTML5 웹 프로그래밍 입문',
price: 26000,
publisher: '한빛아카데미'
}, {
name: '머신러닝 딥러닝 실전 개발 입문',
prie: 30000,
publisher: '위키북스'
}, {
name: '딥러닝을 위한 수학',
price: 25000,
publisher: '위키북스'
}]
// 가격으로 정렬한 뒤 출력합니다.
const output = _.sortBy(books, (book) => book.price, 'asc');
console.log(JSON.stringify(output, null,2))
</script>
</head>
<body></body>
</html>
const book ={
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
};
const temp = JSON.stringify(book);
console.log(temp);
{"name":"혼자 공부하는 파이썬","price":18000,"publisher":"한빛미디어"}<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 객체를 생성합니다.
const object = {
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}
// 객체 내부에 속성이 있는지 확인합니다.
if (object.name !== undefined) {
console.log('name 속성이 있습니다.')
} else {
console.log('name 속성이 없습니다.')
}
if (object.author !== undefined) {
console.log('author 속성이 있습니다.')
} else {
console.log('author 속성이 없습니다.')
}
//object.name || console.log('name 속성이 없습니다.') 이렇게도 표현 가능
//object.author || console.log('author 속성이 없습니다.')
</script>
</head>
<body></body>
</html>
이렇게도 가능
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 객체를 생성합니다.
const object = {
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}
// 객체의 기본 속성을 지정합니다.
object.name = object.name !== undefined ? object.name : '제목 미정'
object.author = object.author !== undefined ? object.author : '저자 미상'
// 객체의 기본 속성을 지정합니다.
//object.name = object.name || '제목 미정'
//object.author = object.author || '저자 미상' 이렇게도 표현 가능
// 객체를 출력합니다.
console.log(JSON.stringify(object, null, 2))
</script>
</head>
<body></body>
</html>
{ 속성 이름, 속성 이름 } = 객체
{ 식별자 = 속성 이름, 식별자 = 속성 이름 } = 객체
객체 다중 할당에서는 변수 이름과 객체의 속성 이름이 일치해야 한다.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 객체를 생성합니다.
const object = {
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}
// 객체에서 변수를 추출합니다.
const { name, price } = object // name에 object.name을 price에 object.price를
console.log('# 속성 이름 그대로 꺼내서 출력하기')
console.log(name, price) //혼자 공부하는 파이썬 18000
console.log('')
const { a=name, b=price } = object // a에 object.name을 b에 object.price을
console.log('# 다른 이름으로 속성 꺼내서 출력하기')
console.log(a, b)//혼자 공부하는 파이썬 18000
</script>
</head>
<body></body>
</html>
let [a, b] = [1, 2] // a = 1, b = 2가 할당됩니다.
//undefined
console.log(a, b)
// 1, 2
[a, b] = [b, a] // a에 b가 할당되고, b에 a가 할당되므로 값이 서로 교환됩니다.
// [2, 1]
console.log(a, b)
// 2, 1
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 사야 하는 물건 목록
const 물건_200301 = ['우유', '식빵']
const 물건_200302 = 물건_200301
물건_200302.push('고구마')
물건_200302.push('토마토')
// 출력
console.log(물건_200301)
console.log(물건_200302)
</script>
</head>
<body></body>
</html>
깊은 복사 예시
전개 연산자를 사용한 배열 요소 추가 : [...배열, 자료, 자료, 자료]
배열을 여러 번 전개도 가능하다. 다른 2개 이상의 배열을 붙일 때도 활용 가능하다.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 사야 하는 물건 목록
const 물건_200301 = ['우유', '식빵']
const 물건_200302 = [...물건_200301] // 전개 연산자를 사용해서 배열을 복사합니다.
물건_200302.push('고구마')
물건_200302.push('토마토')
// 출력
console.log(물건_200301)
console.log(물건_200302)
</script>
</head>
<body></body>
</html>
2개 이상 배열
const a = ['우유', '식빵']
//undefined
const b = ['고구마', '토마토']
//undefined
[...a, ...b]
// (4) ["우유", "식빵", "고구마", "토마토"]
[...b, ...a]
// (4) ["고구마", "토마토", "우유", "식빵"]
얕은복사 예시
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
const 구름 = {
이름: '구름',
나이: 6,
종족: '강아지'
}
const 별 = 구름 //얕은 복사
별.이름 = '별'
별.나이 = 1
console.log(JSON.stringify(구름))
console.log(JSON.stringify(별));
// 얕은 복사
</script>
</head>
<body></body>
</html>
{"이름":"별","나이":1,"종족":"강아지"} 를 출력한다.깊은 복사 예시
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
const 구름 = {
이름: '구름',
나이: 6,
종족: '강아지'
}
const 별 = {...구름}
별.이름 = '별'
별.나이 = 1
console.log(JSON.stringify(구름))
console.log(JSON.stringify(별))
// 깊은 복사
</script>
</head>
<body></body>
</html>
{"이름":"구름","나이":6,"종족":"강아지"}{"이름":"별","나이":1,"종족":"강아지"}전개라는 이름처럼 전개한 부분에 객체가 펼쳐진다.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
const 구름 = {
이름: '구름',
나이: 6,
종족: '강아지'
}
const 별 = {
...구름,
이름: '별', // 기존의 속성 덮어 쓰기
나이: 1, // 기존의 속성 덮어 쓰기
예방접종: true
}
console.log(JSON.stringify(구름))
console.log(JSON.stringify(별))
</script>
</head>
<body></body>
</html>
구름 : {"이름":"구름","나이":6,"종족":"강아지"}
별 : {"이름":"별","나이":1,"종족":"강아지","예방접종":true}
뒤에 있는 이름과 나이가 앞에 있는 이름과 나이를 덮어씀
const 별 = {
...구름,
이름 : '별',
나이: 1,
예방접종: true
}
// 위는 다음과 같은 의미이다.
const 별 = {
이름 : '구름',
나이 : 6,
종족 : '강아지',
이름 : '별',
나이 : 1,
예방접종 : true
}
전개를 뒤에 한다면 뒤에서 전개된다. 뒤에 있는 이름과 나이가 앞에 있는 이름과 나이를 덮어쓴다. >> 전개 연산자를 마지막에 사용해 종족 속성이 별의 제일 마지막에 있는 것을 알 수 있다.
const 별 = {
이름 : '별',
나이 : 1,
예방접종 : true,
...구름
}
// 위는 다음과 같은 의미이다.
const 별 = {
이름 : '별',
나이 : 1,
예방접종 : true,
이름 : '구름',
나이 : 6,
종족 : '강아지'
}
전개를 어떤 위치에서 하는지에 따라 결과가 달라진다.
const object = {
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}
풀이
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js">
</script>
<script>
const object = {
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}
// name속성 있는지 체크
if(object.name){
console.log('name 속성이 있습니다.')
} else {
console.log('name 속성이 없습니다.')
}
//author 속성 있는지 체크 >> false값이 들어오지 않는다는 전제 하에
object.author || console.log('author 속성이 없습니다.')
</script>
</head>
<body></body>
</html>
const object = {
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}
풀이
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js">
</script>
<script>
const object = {
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}
object.name = object.name === undefined ? '제목 미정' : object.name;
object.author = object.author === undefined ? '저자 미상' : object.author;
console.log(JSON.stringify(object, null, 2))
</script>
</head>
<body></body>
</html>
name에는 원래 존재하던 혼자 공부하는 파이썬이 들어가있고, author속성이 추가되어 저자 미상이라는 값이 들어가 있다.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
let [a, b] = [1, 2];
console.log(a, b);// 1 2
[a, b] = [b, a];
console.log(a, b); // 2 1
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
let arrayA = [1, 2, 3, 4, 5];
let [a, b, c] = [arrayA[0], arrayA[1], arrayA[2]];
//let [a, b, c] = arrayA; 이렇게 해도 앞에있는 값 먼저 할당
console.log(a, b, c); //1 2 3
</script>
</head>
<body></body>
</html>
const object = {
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}
풀이
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
// 객체를 생성합니다.
const object = {
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}
// 객체에서 변수를 추출합니다.
const { name, price } = object // name에 object.name을 price에 object.price를
console.log('# 속성 이름 그대로 꺼내서 출력하기')
console.log(name, price) //혼자 공부하는 파이썬 18000
console.log('')
const { a=name, b=price } = object // a에 object.name을 b에 object.price을
console.log('# 다른 이름으로 속성 꺼내서 출력하기')
console.log(a, b)//혼자 공부하는 파이썬 18000
</script>
</head>
<body></body>
</html>
const object = {
name: '혼자 공부하는 파이썬',
price: 18000,
publisher: '한빛미디어'
}
// 사야 하는 물건 목록
const 물건_200301 = ['우유', '식빵']
const 물건_200302 = 물건_200301
물건_200302.push('고구마')
물건_200302.push('토마토')
// 출력
console.log(물건_200301)
console.log(물건_200302)
[...배열, 자료, 자료, 자료] 이렇게 가능 <!DOCTYPE html>
<html>
<head>
<title></title>
<script>
const 물건_200301 = ['우유', '식빵']
const 물건_200302 = [...물건_200301]; //전개 연산자 사용해 배열 복사
물건_200302.push('고구마')
물건_200302.push('토마토')
// 출력
console.log(물건_200301) //우유 식빵
console.log(물건_200302) //우유 식빵 고구마 토마토 출력
</script>
</head>
<body></body>
</html>
// 사야 하는 물건 목록
const 물건_200301 = ['우유', '식빵']
const 물건_200302 = ( )
// 출력
console.log(물건_200301)
console.log(물건_200302)
풀이
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
const 물건_200301 = ['우유', '식빵']
const 물건_200302 = ['고구마', ...물건_200301, '토마토'];
// 출력
console.log(물건_200301)
console.log(물건_200302)
</script>
</head>
<body></body>
</html>
const a = ['우유', '식빵']
const b = ['고구마', '토마토']
실행결과
(4) ['우유', '식빵', '고구마', '토마토']
(4) ['고구마', '토마토', '우유', '식빵']
풀이
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
const a = ['우유', '식빵']
const b = ['고구마', '토마토']
const a2 = [...a, ...b];
const b2 = [...b, ...a];
console.log(a2);
console.log(b2);
</script>
</head>
<body></body>
</html>
const 구름 = {
이름: '구름',
나이: 6,
종족: '강아지'
}
const 별 = 구름
별.이름 = '별'
별.나이 = 1
console.log(JSON.stringify(구름))
console.log(JSON.stringify(별))
{"이름":"별","나이":1,"종족":"강아지"} 2개의 출력 모두 이렇게 출력 {...객체}<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
const 구름 = {
이름: '구름',
나이: 6,
종족: '강아지'
}
const 별 = {...구름};
별.이름 = '별';
별.나이 = 1;
console.log(JSON.stringify(구름)); //{"이름":"구름","나이":6,"종족":"강아지"}
console.log(JSON.stringify(별)); //{"이름":"별","나이":1,"종족":"강아지"}
</script>
</head>
<body></body>
</html>
다음의 출력결과를 유추하여 적어보자.
const 구름 = {
이름: '구름',
나이: 6,
종족: '강아지'
}
const 별 = {
...구름,
이름: '별', // 기존의 속성 덮어 쓰기
나이: 1, // 기존의 속성 덮어 쓰기
예방접종: true
}
console.log(JSON.stringify(구름))
console.log(JSON.stringify(별))
{"이름":"구름","나이":6,"종족":"강아지"}{"이름":"별","나이":1,"종족":"강아지","예방접종":true} >> 구름의 속성들을 가져와 종족 속성도 존재하는 것이다.다음의 출력결과를 유추하여 적어보자
const 구름 = {
이름: '구름',
나이: 6,
종족: '강아지'
}
const 별 = {
이름: '별',
나이: 1,
예방접종: true,
...구름
}
console.log(JSON.stringify(구름))
console.log(JSON.stringify(별))
{"이름":"구름","나이":6,"종족":"강아지"}{"이름":"구름","나이":6,"예방접종":true,"종족":"강아지"} >> 객체 전개는 순서가 중요하므로 전개를 뒤에서 하니 종족이 뒤에 오는 것을 알 수 있다.