FASTCAMPUS ST-FE 3기
Part 1. HTML/CSS/JS로 만드는 스타벅스 웹사이트 - Ch 9. JS 선행
dash-case(kebab-case)
snake_case
camelCase
ParcelCase
js는 특수한 경우를 제외하고, 0부터 숫자를 센다.
let fruits=['apple','banana','cherry'];
console.log(fruits[0]); //apple
console.log(frunits[1]); //banana
console.log(frunits[2]); //cherry
console.log(new Date('2021-01-30').getDay()); //6 ->토요일을 의미
console.log(new Date('2021-01-31').getDay()); //0 ->일요일을 의미
console.log(new Date('2021-02-01').getDay()); //1 ->월요일을 의미
//
한 줄 메모
/**/
여러 줄 메모
//string
//따옴표를 사용
let myName = 'oching';
let email = 'oching.choi@gmail.com';
let hello = `hello ${myName}?`
//보간법 : ``(백틱)을 사용해 중간에 다른 값을 가져옴.
//`${내용}`
console.log(myName); //oching
console.log(email); //oching.choi@gmail.com
console.log(hello); //hello oching?
//Number
//정수 및 부동 소수점 숫자를 나타냄
let number = 123; //정수
let opacity = 1.57; //부동소수점
console.log(number); //123
console.log(opacity); //1.57
//boolean
//true, false 두가지 값으로 표현
let checked = true;
let isShow = false;
console.log(checked); //true
console.log(isShow); //false
//undefined
//값이 할당되지않음
let undef;
let obj = {abc: 123};
console.log(undef); //undefined
console.log(obj, abc) //123
console.log(obj, xyz) //undefined
//Null
//어떤 값이 의도적으로 비어있음.
let empty= null;
console.log(empty) //null
//Object
//여러 데이터를 key:value 형태로 저장. {}
left user={
name:'oching',
age:'28',
isValid:true
};
console.log(user.name) //oching
console.log(user.age) //28
console.log(user.isValid) //true
//Array
//여러데이터를 순차적으로 저장. []
let fruits=['apple','banana','cherry'];
console.log(fruits[0]); //apple
console.log(frunits[1]); //banana
console.log(frunits[2]); //cherry
데이터를 저장하고 참조(사용)하는 데이터의 이름
var let const
//변수 선언과 할당
let a = 2;
let b = 5;
console.log(a+b); // 7
console.log(a-b); //-3
console.log(a*b); //10
console.log(a/b); //0.4
//let 재할당 가능
let a = 12;
console.log(a); //12
a=999;
console.log(a) //999
//const 재할당 불가
const a = 12;
cosole.log(a); //12
a = 999;
console.log(a); //TypeError:Assignment to constant variable.
특별한 의미를 가지고 있어 변수나 함수이름등으로 사용할 수 없는 단어
let this = 'hello'; //SyntaxError
let if = 123; //SyntaxError
let break = true; //SyntaxError
특정 동작(기능)을 수행하는 일부코드의 집합(부분)
//함수선언
function helloFunc(){
//실행코드
console.log(1234);
}
//함수호출
helloFunc(); //1234
//return 반환
function returnFunc(){
return 123;
}
let a = returnFunc(); //변수에 함수를 할당
console.log(a); //123
function sum(a,b){ //a,b는 매개변수(parameters)
return a+b;
}
//재사용
let a = sum(1,2); //1과 2는 인수또는 인자(arguments)
let b = sum(7,12);
let c = sum(2,4);
console.log(a,b,c) // 3,19,6
//함수선언
//기명(이름이 있는)함수
function hello(){
console.log('hello~');
}
//힘수표현
//익명(이름이 없는)함수
let world = function(){
console.log('world~');
}
//함수호출
hello(); //hello~
world(); //world~
함수를 하나의 속성으로 사용할 수 있다.
이는 객체 안에 저장해 값처럼 사용할 수 있다는 뜻이 된다.
//객체데이터
const oching = {
name : 'oching',
age : 28,
//메소드(method) - 속성부분에 함수가 할당되어있으면 메소드라고 한다.
getName : function(){
return this.name;
}
};
const HerName = oching.getName();
console.log(herName); //oching
//혹은
console.log(oching.getName()); //oching
조건의 결과 (truthy, falsy)에 따라 다른 코드를 실행하는 구분
if, else
let isShow = true;
let checkted = false;
if(isShow){
console.log('show') //show 출력됨.
}
if(checked){
console.log('checked') //출력되지않음.
}
let isShow = true;
if(isShow){
console.log('show')
}else{
console.log('hide')
}
//show 출력됨
Document Object Model, Application Programming Interface
브라우저 환경에서 실행할 수 있게 하는 명령들
//html요소 찾기
let boxEl = document.querySelector('.box');
console.log(boxEl); //html의 .box요소를 가져온다.
boxEl.addEventListener('click',function(){
console.log('click');
})
//html 요소 찾기
const boxEl = document.querySelector('.box');
//요소의 클래스 정보 객체 활용
boxEl.classList.add('active'); //클래스리스트를 가져와서 active 추가해줘
let isContains = boxEl.classList.contains('active'); //active를 클래스로 갖고있니?
console.log(isContains); //true
boxEl.classList.remove('active'); //클래스리스트를 가져와서 active를 삭제해줘
isContains = boxEl.classList.contains('active'); //active를 클래스로 갖고있니?
console.log(isContains); //false
let boxEl = documemt.querySelector('.box'); //html요소 중 .box요소를 찾아오고
console.log(boxEl); //console창에 요소 출력
boxEl.addEventListener('click',function(){ //.box요소를 눌렀을 때 함수실행
console.log('click'); //click이라는 메세지 console창에 출력
boxEl.classList.add('active'); //.box요소에 active 클래스 추가
console.log(
boxEl.classList.contains('active') //true출력
);
boxEl.classList.remove('active')' //.box요소에 active 클래스 제거
console.log(
boxEl.classList.contains('active') //false
);
})
//html요소 모두 찾기
cost boxEls = document.querySelectorAll('.box');
//출력
boxEls.forEach(function(boxEl, index){ //빈복되는요소, 반복중인 번호
boxEls.classList.add(`order-${index + 1}`); //0부터 numbering되니까 +1해줌
console.log(index.boxEl);
})
const boxEl = document.querySelector('.box');
//값을 얻는 용도
console.log(boxEl.textContent);
//값을 지정하는 용도
boxEl.textContent = "oching";
console.log(boxEl.textContent); //oching
메소드를 연결
const a = 'hello~';
//split 문자를 인수기준으로 쪼개 배열로 반환
//reverse 배열을 뒤집기
//join 배열을 인수 기준으로 문자로 병합해 반환
const b = a.split('').reverse().join('');
console.log(a); //hello~
console.log(b); //~olleh
theQuickBrownFow
let fruits = ['apple','banana','cherry'];
console,log( fruits[1] );
false
null
객체
let obj= {abc:123};
console.log(obj.xyz);
undefined
obj객체의 xyz로 접근해도 정의된 값이 없으니
undefined가 된다.
const
return
sum(2,4);
인수 arguments
function sum(a,b){
return a+b;
}
매개변수 parameters
익명함수 anonumous function
const hello = function(){}; hello();
이름으로서 함수를 선언하지않은 익명함수.
따라서 함수 표현으로서 변수에 담아 정의되고 호출된다.
const user={
getName:function()[]
}
메서드
if(true){};
defer
<div class="box">box</div>
let boxEl = document.querySelector('.box'); console.log(boxEl.textcontent)
let
const boxEl = document.querySelector('.box')
boxEl.addEventListener('click',function(){ console.log('hello'); })
<div>1<div>
<div>2</div>
const divEls = document.querySelectorAll('div'); divEls.forEach(function(divEl){ divEl.classList.add('hello'); })
'oching'.split('').reverse().join('');
메서드체이닝
const boxEl = document.querySelector('.box');
if( boxEl.classList.contains('active') ){ console.log('포함'); }