js20 [JS] ES6 Classes 앞서 알아봤던 생성자 함수를 class라는 키워드를 통해서 간편하게 바꾸는 코드를 알아 볼 것이다. // function User(first, last) { // this.firstName = first // this.lastName = last // } // User.prototype.getFullName = function () { // return `${this.firstName} ${this.lastName}` // } class User { constructor(first, last) { this.firstName = first this.lastName = last } getFullName() { return `${this.firstName} ${this.lastName}` } } const f.. 2022. 4. 15. [JS] this 이번 글에서는 this의 사용법에 대해서 알아볼 것이다. 일반 함수(normal)는 호출 위치에 따라 this 정의 화살표 함수(Arrow)는 자신이 선언된 함수 범위에서 this 정의! const flyda = { name : 'flyda', normal : function () { console.log(this.name) //호출 위치에서 this 정의 }, arrow : () => { console.log(this.name) // 자신이 선언된 함수 범위에서 this 정의, 지금 안에서 무엇을 지칭하는지 정확하게 알 수 없음. } } flyda.normal() // flyda flyda.arrow()//undefined const amy = { name : 'Amy', normal : flyda.n.. 2022. 4. 15. [JS] 클래스 - 생성자 함수 (prototype) 다음과 같이 여러 번 비슷한 코드가 반복되는 경우 생성자 함수를 사용할 수 있다. 생성자 함수란 new키워드를 사용하는 함수이다. const flyda = { firstName: 'Flyda', lastName: 'Yang', getFullName : function () { return `${this.firstName} ${this.lastName}` } } console.log(flyda.getFullName()) const amy = { firstName: 'Amy', lastName: 'Hwang', getFullName : function () { return `${this.firstName} ${this.lastName}` } } console.log(amy.getFullName()) cons.. 2022. 4. 15. [JS] 데이터 타입 확인 함수 실습 내용 프로젝트에서 데이터 타입을 확인하는 함수를 js파일로 작성한 뒤 import로 가져와서 사용하는 방법을 실습한다. 코드는 다음과 같다. html 파일 hello!! main.js 파일 console.log(typeof "hello dada") console.log(typeof 123) console.log(typeof true) console.log(typeof undefined) console.log(typeof null) console.log(typeof {}) console.log(typeof []) 크롬의 개발자 도구 콘솔 창을 살표보면, 먼저 main.js 파일에서 typeof를 통해서 각 데이터의 타입을 확인할 수 있다. 하지만. typeof의 경우 null, object, arra.. 2022. 4. 14. 이전 1 2 3 4 5 다음