2021/02 54

ES6 Class + Class 상속 (Prototype을 쉽게 이용하기)

class문법을 사용하면 상속을 해야하는상황에서 훨씬 더 쉽게 이용할 수 있습니다. class Animal{ constructor(type, name, sound){ this.type = type; this.name = name; this.sound = sound; } say(){ console.log(this.sound); } } class Dog extends Animal{ constructor(type, name, sound){ super(type, name, sound); } } class Cat extends Animal{ constructor(name, sound){ super('고양이', name, sound); } } const dog = new Dog('개', '멍멍이', '멍멍'); c..

객체 생성자 상속하기 Prototype

function Animal(type, name, sound){ this.type = type; this.name = name; this.sound = sound; } Animal.prototype.say = function(){ console.log(this.sound); } function Dog(name, sound){ Animal.call(this, '개', name, sound); } function Cat(name, sound){ Animal.call(this, '고양이', name, sound); } Dog.prototype = Animal.prototype; Cat.prototype = Animal.prototype; const dog = new Dog('멍멍이', '멍멍'); const ..

객체 생성자 Prototype

주로 객체를 선언할때는 대문자부터 시작하고, new라는 키워드를 이용합니다. function Animal(type, name, sound){ this.type = type; this.name = name; this.sound = sound; this.say = function(){ console.log(this.sound); } } const dog = new Animal('개', '멍멍이', '멍멍'); const cat = new Animal('고양이', '야옹이', '야옹'); dog.say(); cat.say(); 여기서 비효율적인거는, 새로운 Animal을 만들때마다 새로운 함수를 만든다는 겁니다. 함수를 보면 내용이 똑같은걸 알 수 있습니다. sound와 똑같은 데이터를 사용하는데 두번이나 선..

Javascript 배열 내장함수(reduce) 숫자와 문자 다루기

1. 배열의 총 합 구하기 (숫자) 0은 초기 accumulator가 됩니다. accumulator는 누적된 값을 의미합니다. const numbers = [1,2,3,4,5]; const sum = numbers.reduce((accumulator, current)=> accumulator+current , 0); console.log(sum); //출력값 15 소스코드 해석: accumulator은 초기값이 0 으로시작하고 current는 배열의 그 다음값을 의미합니다. 그래서 0+1은 1이되고, accumulator값은 1이 됩니다. 다음 accumulator은 1이되고, current는 2가 되어서 1+2 =3 이됩니다. 다음 accumulator은 3이되고, current는 3이 되어서 3+3 ..

Javascript/배열 2021.02.01