목록자바 스크립트 (JavaScript)/Object Oriented JavaScript - ES5&ES2015 (7)
종우의 컴퓨터 공간
- ES6 신텍스에서 상속하는 방법이다. - 자바 언어처럼 extends 키워드를 통해서 상속한다. 예제 class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } greeting() { return `Hello there ${this.firstName} ${this.lastName}`; } } class Customer extends Person { constructor(firstName, lastName, phone, membership) { super(firstName, lastName); this.phone = phone; this.membership = members..
- 자바, 씨언어와 같은 오브젝트 오리엔티트 언어에서 사용하는 클래스와 신텍스가 비슷하다. 예제 class Person { constructor(firstName, lastName, dob) { this.firstName = firstName; this.lastName = lastName; this.birthday = new Date(dob); } greeting() { return `Hello there ${this.firstName} ${this.lastName}`; } calculateAge() { const diff = Date.now() - this.birthday.getTime(); const ageDate = new Date(diff); return Math.abs(ageDate.getUTC..
- this is the alternative way to create object with Object.create() method - Object.create() takes the first parameter of prototype 예제 const personPrototypes = { greeeting: function() { return `Hello there ${this.firstName} ${this.lastName}`; }, getsMarried: function(newLastName) { this.lastName = newLastName; } } const mary = Object.create(personPrototypes); // adding properties mary.firstName ..
예제 // Person constructor function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } // Greeting in prototype Person.prototype.greeting = function() { return `Hello there ${this.firstName} ${this.lastName}`; } // Create Person const person1 = new Person('John', 'Doe'); console.log(person1); // Customer constructor function Customer(firstName, lastName, phone, m..

프로토타입 이란? - 자바스크립트는 프로토타입 기반 언어이다. - 자바스크립트에서 기본 데이터 타입(string, number, boolean, null, undefined)을 제외한 모든 것은 객체이다. - 객체가 만들어지기 위해서는 자신을 만드는데 사용된 원형인 프로토타입 객체를 이용하여 객체를 만든다. - 원형이라는 뜻으로 프로토타입 객체와 프로토타입 링크로 나눌 수 있다. - protytype 속성은 함수만 가지고 있던 것과 달리 [[Protytype]] 속성은 모든 객체가 빠짐없이 가지고 있는 속성이다. 그 말은 함수로 통해 만들어진 객체만 프로토타입 객체가 존재하는 것이고 함수가 아닌 걸로 만든 객체는 프로토타입 객체가 존재하지 않는다. 다만 new를 통해서 기존에 존재하던 함수로 인해 복사한..
String const name1 = 'Jeff'; const name2 = new String('Jeff'); // String as an object name2.foo = 'bar'; // can add property since it is an object console.log(name1); console.log(name2); console.log(typeof name1); // name1: string console.log(typeof name2); // name2: object - Object이기 때문에 property를 추가할 수 있다. Number const num1 = 5; const num2 = new Number(5); console.log(num1); console.log(num2);..
'this' keyword - when 'this' is used inside of a constructor, it refers to the current object - when 'this' is used in global scope, it pertains the 'window' object 예제 function Person(name, dob) { this.name = name; this.birthday = new Date(dob); this.calculateAge = function() { const diff = Date.now() - this.birthday.getTime(); const ageDate = new Date(diff); console.log(ageDate.getFullYear()); ..