console.log(typeof "hello world") // string 출력
console.log(typeof 123) // number 출력
console.log(typeof true) // boolean 출력
console.log(typeof undefined) // undefined 출력
console.log(typeof null) // object 출력
console.log(typeof {}) // object 출력
console.log(typeof []) // object 출력
typeof 를 사용하면 어떤 타입인지 나오지만 뭉덩이로 object 라고 표시가 될 수 있음
>> 타입을 반환하는 함수 만들기
function getType(data){
return Object.prototype.toString.call(data).slice(8, -1)
// slice > '[object'와 끝']' 삭제
}
! 함수를 사용한 후 console.log 변화
console.log(getType(123)) // Number 출력
console.log(getType(false)) // Boolean 출력
console.log(getType(null)) // Null 출력
console.log(getType({})) // Object 출력
console.log(getType([])) // Array 출력
const a = 1, const b = 1 로 할당했을 때 console.log(a === b ) // ture 출력
type도 똑같아야 true를 출력함
< 비교 연산자 함수 >
function isEqual(x, y){
return x === y
}
console.log(isEqual(1,2)) // false 출력
!== : 서로가 다른 것인지 확인해주는 연산자
a < b : a가 b보다 작을 때 true 출력
a >= b : a가 b보다 같거나 클 때 true 출력
= 기호 앞에 꺽새 표시가 있으면 문법 오류
&& : 그리고 AND, 나열된 것이 모두 true 인 경우에만 true 출력.
하나라도 false 인 경우 false 출력
|| : 또는 OR, 나열된 것들 중 true 가 하나라도 있으면 true 출력.
나열된 값 모두가 false인 경우에만 false 출력
! : 부정 NOT 연산자. 값의 밥ㄴ대되는 것이 출력이 됨
export default function random(){
return Math.floor(Math.random() * 10)
}
<조건문 if else>
const a = random()
if(a === 0){
console.log('a is 0')
} else if(a === 2){
console.log('a is 2')
} else if(a === 4){
console.log('a is 4')
} else {
console.log('rest...')
}
<조건문 switch>
switch(a){
case 0:
console.log('a is 0')
break
case 2:
console.log('a is 2')
break
case 4:
console.log('a is 4')
break
default: // if 문의 else 문, break도 사용하지 않아도 됨
console.log('rest...')
}
! 조건을 사용하려고 할 때 특정한 값으로 분기처리를 하고 있을 땐
if문 보다는 switch문이 훨씬 더 직관적으로 보일 수 있다
for(let i = 0; i < 3; i += 1){
/*
시작조건: let i = 0 (통상적으로 권장되는 변수 이름 i)
종료조건: i가 3보다 작지 않은 경우 종료가 됨
변화조건: i에 계속 +1을 할당함
*/
console.log(i) // 0,1,2 출력
}
function sum(x,y){ // x, y 매개 변수
return x + y
}
const a = sum(1,3) // 1, 3 인수
console.log(a) // 4 출력
console.log(sum(1, 3)) // 4 출력
const double = function(x){
return x * 2
}
console.log('double:', double(7))
const doubleArrow = x => x * 2
console.log('doubleArrow:', doubleArrow(7))
const a = 7
function double(){
console.log(a * 2)
}
double(); // 즉시 실행함수를 실행할 땐 앞 함수에 세미클론 넣어주어야 함
(function(){
console.log(a * 2)
})(); // 소괄호 위치 확인, 14 출력
(function(){
console.log(a * 2)
}()); // 소괄호 위치 확인, 14 출력
setTimeout(function (){
console.log("hello!")
}, 3000) // 3초 뒤에 console에 헬로우 출력
const timer = setInterval(() => {
console.log("화살표 함수")
}, 3000)
const h1El = document.querySelector("h1")
h1El.addEventListener('click', () => {
clearInterval(timer)
})
function timeout(cb){
setTimeout(() => {
console.log("hello")
cb()
}, 3000)
}
timeout(() => {
console.log("done!")
})
const heropy = {
firstName : 'heropy',
lastName : 'Park',
getFullName: function(){
return `${this.firstName} ${this.lastName}`
}
}
console.log(heropy.getFullName())
const amy = {
firstName: "Amy",
lastName: "clarke",
getFullName: function(){
return `${this.firstName} ${this.lastName}`
}
}
console.log(amy.getFullName())
const neo = {
firstName: "Neo",
lastName: "Smith",
getFullName: function(){
return `${this.firstName} ${this.lastName}`
}
}
console.log(neo.getFullName())
function User(first, last){ // 파스칼 케이스
this.firstName = first
this.lastName = last
}
User.prototype.getFullName = function (){
return `${this.firstName} ${this.lastName}`
}
const heropy = new User("heropy", "park") // 변수 > 인스턴스
const amy = new User("Amy", "clarke")
const neo= new User("neo", 'smith')
console.log(heropy.getFullName()) // user라는 객체데이터 출력
console.log(amy.getFullName())
console.log(neo.getFullName())
const hreopy = {} >> 리터럴 방식
const heropy = {
name: 'heropy',
normal: function (){
console.log(this.name)
},
arrow: () => {
console.log(this.name)
}
}
heropy.normal() // heropy 출력
heropy.arrow() // undefined 출력
const amy = {
name: 'Amy',
normal: heropy.normal, // ()가 없음 > 호출하는 것이 아님
arrow: heropy.arrow // heropy의 데이터 자체가 할당이 되는 것을 뜻함
}
amy.normal() // amy 출력
amy.arrow() // undefined 출력
function User(name){
this.name = name
}
User.prototype.normal = function(){
console.log(this.name)
}
User.prototype.arrow = () => {
console.log(this.name)
}
const jeonghyun = new User('jeonghyun')
jeonghyun.normal() // jeonghyun 출력
jeonghyun.arrow() // undefined 출력
const timer = {
name: "name",
timeout: function (){
setTimeout(() => {
console.log(this.name)
}, 2000)
}
}
timer.timeout() // name 출력
const heropy = {
name : 'Heropy',
normal/*function*/(){
/*
function 생략 가능
*/
console.log(this.name)
},
arrow: () => {
console.log(this.name)
}
}
heropy.normal()
heropy.arrow()
function User(first, last){
this.firstName = first
this.lastName = last
}
User.prototype.getFullName = funciton(){
return `${this.firstName} ${this.lastName}`
}
class User {
constructor(first, last){
this.firstName = first
this.lastName = last
}
getFullName(){
return `${this.firstName} ${this.lastName}`
}
}
class Vehicle {
constructor(name, wheel){
this.name = name
this.wheel = wheel
}
}
const myVehicle = new Vehicle('운송수단', 2)
console.log(myVehicle)
class Bicycle extends Vehicle{
constructor(name, wheel){
super(name, wheel)
}
}
const myBicycle = new Bicycle('삼천리', 2)
const daughtersBicycle = new Bicycle('세발', 3)
console.log(myBicycle)
console.log(daughtersBicycle)
class Car extends Vehicle{
constructor(name, wheel, license){
super(name, wheel)
this.license = license /* 기존에 있는 내용에 확장하여 추가 */
}
}
const myCar = new Car('벤츠', 4, true)
const daughtersCar = new Car('포르쉐', 4, false)
console.log(myCar)
console.log(daughtersCar)