function fnc1(){ console.log(this);//일반함수 안의 this는 window를 가르킴 }
const item ={ name:'luna', hi:function(){ console.log('Hi, ' + this.name)//Hi, luna } } item.hi();//메서드 호출
function people(name,age){ this.name='tammy', this.age=30 }//people 생성자 , this는 새로 생기는 object(instance)를 가르킴 const p1 = new people(); console.log(p1);//age: 30 name: "tammy"
const btn = document.getElementById('btn') btn.addEventListener('click', function(e){ console.log(e.currentTarget); console.log(this);//위와 같은 결과(btn) })
const btn = document.getElementById('btn') btn.addEventListener('click', function(e){ var arr1=['2020', '2021', '2022']; console.log(this);//btn 출력 arr1.forEach(function(a){ console.log(this);//일반함수로 취급 -> window 출력 }) })
btn.addEventListener('click',(e)=>{ const arr1=['2020', '2021', '2022']; console.log(this);//window(새롭게 정의되지 않고 상위 요소로 this 가져옴) console.log(e.currentTarget);//화살표 함수로 쓰려면 currentTarget으로 출력 arr1.forEach((a)=>{ console.log(this);//window, 여기만 화살표 함수로 바꾸면 button 출력 }) })
function(){} 의 형식이 ()=>{} 이렇게 바뀜; (매개변수) => {실행될 코드}
// function fnc1(num1){ // return num * 2; // } const fnc1= (num)=> num * 2;//화살표 함수 console.log(fnc1(5));//10
const dayList = `월요일` const txt = `오늘은 ${dayList} 입니다.` console.log(txt)
const txt2 ='Ellen'; function txtBox(a,b){ console.log(a[0]);//Hi, console.log(b) } txtBox `Hi, ${txt2} nice to meet you.`;//백틱 내용이 txtBox 함수의 매개변수로 들어감(a는 배열로 문자열을 받고 b는 변수를 받음, 변수가 여러 개라면 그 만큼 인자 추가해야함)
const arr1=['월요일','화요일','수요일','목요일','금요일']; console.log(arr1[0])//월요일 console.log(...arr1)// 월요일 화요일 수요일 목요일 금요일, 배열을 펼쳐서 호출(배열 요소 단위로) var txt1 = '오늘은 월요일 입니다.' console.log(...txt1[1])//늘, 문자열 펼쳐서 호출(철자 단위로)
const arr1=[1,2,3,4,5] //var arr2=arr1; //값이 독립적이지 않고 공유됨 const arr2=[...arr1] // 공유 x, 값을 복사할 때 유용 arr1[5]=6; arr1[6]=7; console.log(arr1)//[1, 2, 3, 4, 5, 6, 7] console.log(arr2)//[1, 2, 3, 4, 5]