Class

JavaScript 2022. 5. 26. 22:50

1. 클래스(Class) A. 클래스의 정의 class Rectangle { constructor(height, width) { // 생성자 함수 this.height = height; this.width = width; } get area() { // Getter return this.calcArea(); } calcArea() { // 메서드 return this.height * this.width; } } 객체(instance)를 생성하기 위한 템플릿 '함수'로, 선언식 또는 표현식으로 정의가 가능함. ① Class 선언식 class Rectangle { ... } class 선언식은 호이스팅될 때 초기화가 되지 않으므로 class를 선언한 뒤에 사용할 수 있음에 주의! ② Class 표현식 let..

Article Thumbnail
유사 배열과 Array.prototype.forEach()의 관계 파헤치기

JavaScript 2022. 3. 26. 18:14

2022.03.24 - [Javascript] - (2022. 3. 24.) 유사 배열(Array-like) const divs = document.querySelectorAll('div'); let arr = [] Array.prototype.forEach.call(divs, (element, index) => { arr.push(element.innerText) }) console.log(arr) // ["내용1", "내용2", "내용3", ... ] Function.prototype.call() 메서드를 이용해서 유사 배열에서 배열의 메서드를 사용할 수 있음. 하지만 Array.prototype.forEach() 메서드에도 this를 매개변수(thisArg)로 넣을 수 있는데 두 방법의 차이에 대해..

this

JavaScript 2022. 3. 24. 21:43

1. This Javascript는 함수를 호출할 때 함수가 어떻게 호출되었는지에 따라 this에 바인딩할 객체를 동적으로 결정함. 동일한 함수라 하더라도 함수를 호출할 때마다 달라질 수 있음. this 비고 전역에서 선언된 변수 window 함수 window 내부함수 지정되지 않음 외부함수가 일반, 메서드, 콜백이든 상관없이 객체 메서드 해당 객체 new를 이용한 생성자 함수 호출 생성자 함수로 생성된 객체 화살표 함수 상위 스코프의 this A. 일반 함수, 객체 메서드 var age = 100; let ken = { age: 35, foo: function bar() { console.log(this.age); } }; let wan = { age: 31, foo: ken.foo } let foo ..

Article Thumbnail