목록HTML & CSS & JS/JS (16)
가자미의 개발이야기
prototype : 함수를 개체로 선언할 때 함수를 매 인스턴스마다 선언하는게 부담스러워서 생긴 기능. 프로토타입을 사용하면 여러 인스턴스를 만들어도 해당 함수는 하나로 모두 통용된다. //prototype function Animal(type, name, sound){//개체 생성자 this.type = type; this.name = name; this.sound = sound; /*function say(){ console.log(this.sound); }*/ } Animal.prototype.say=function(){ console.log(this.sound); }//dog.say=say;cat.say=say;와 같은 의미 const dog = new Animal('개','멍멍이', '멍멍')..
reduce : 콜백을 모든 원소들이 반복하고 그 값을 누적한다. const numbers = [1,2,3,4,5]; const sum = numbers.reduce((accumulator, current)=>accumulator+current, 0) //accumulator 결과값이 누적, current 현재 배열원소 //reduce 활용 const alpahnbet=[a,a,a,b,c,c,d,e]; alphabets.reduce((acc,current)=>{ if(acc[current]){ acc[current]+=1; }else{ acc[current]=1; } return acc; }); reduce 문을 통해 배열 내의 중복된 알파벳 갯수를 반환하는 예제이다.
map : 배열의 원소마다 특정 값으로 바꾸어 반환 indexOf : 해당 인덱스에 맞는 배열의 원소를 반환 //map const array = [1,2,3,4,5]; const array3=array.map(n=>n*n); //indexof const ar1=[1,2,3,4]; const index=ar1.indexOf(2); findIndex : 특정 콜백함수에 적합한 원소의 인덱스 반환 filter : 특정 콜백함수에 적합한 원소를 반환 // findindex const todos=[ { id:1, text :'js intro', done : true }, { id:2, text :'js intro2', done : false } ]; const index = todos.findIndex(todo=..
for of : 배열의 원소들을 하나하나 대입 //for of const nubmers=[1,2,3,4,5]; for (let number of numbers){ console.log(number); } for in : 객체의 멤버들의 키를 하나하나 대입 const dog={ name: 'doggy', age: 3, sound:"bakr!" }; for (let key in dog){ console.log(key); console.log(dog[key])//value출력 }; forEach : 배열의 매 원소들로 콜백 함수 실행 //for each const superHeroes=[ironman, superman, batman]; superHeroes.forEach(hero=>{ console.log(h..
GET 함수 const nubers = { a:1, b:2, get sum(){//get함수는 조회하려고 할 때 사용 console.log('sum func~'); return this.a+this.b; } }; get은 특정값을 조회하려고 할 때 사용한다. SET 함수 const cow={ name:'황소', set name(value){//특정값을 설정할 때 사용. console.log('change name'); this.name=value; } }; cow.name='무친소';//이런식으로 변경가능 set 함수는 매개변수를 받아 특정 변수에 설정할 때 사용한다.
#자바스크립트의 특이한 문법을 정리 화살표함수 const add =(a, b)=> a+b; const sum = add(1,2); console.log(sum); const 선언으로 화살표 함수로 액션을 정의할 수 있다. 객채와 비구조 할당 const dog ={ name : '멍멍이', age : 2, } const {dogName}=dog; console.log(dogName); 객체 안에 함수 넣기 const cat ={ name='kitty', sound='yao', say: function(){//화살표함수 사용하면 this가 뭔지 모름 console.log (this.sound); } } 객체 안에 함수를 넣을 때 주의해야 할 점은 화살표 함수 안에 this를 활용하는 경우, this가 해당 ..