constructor 3

class - class 기본 문법

1. class Class를 사용하면 객체 지향 프로그래밍에서 사용되는 다양한 기능을 자바스크립트에서도 사용 할 수 있습니다. class User { constructor({name}){ this.name = name; } welcome(){ console.log(`Welcome ${this.name}`) } } let user1 = new User({name : 'Bob'}); user1.welcome(); // 'Welcome Bob' 클래스를 new 와 함께 호출하면 다음과 같은 일이 일어납니다. 1. 새로운 객체가 생성됩니다. 2. 넘겨받은 인수와 함께 constructor가 자동 실행됩니다. constructor() 는 new에 의해 자동 호출되므로, 별다른 동작없이 객체를 초기화 할 수 있습니..

Prototype - 함수의 prototype 프로퍼티

1. 함수의 prototype 프로퍼티와 인스턴스의 특징 생성자 함수를 사용해 객체를 생성했을 때와 리터럴 방식으로 객체를 생성했을 때 프로토타입의 동작 방식이 다릅니다. 생성자 함수의 new 연산자로 만든 객체(인스턴스)의 [[Prototype]] 은 생성자 함수의 prototype 프로퍼티를 참조합니다. prototype은 생성자 함수의(F)의 '일반 프로퍼티'입니다. (ex. F.prototype) let animal = { walk: true, } function Human(name) { this.name = name } Human.prototype = animal let human1 = new Human('Bob') console.log(human1.walk) // true 위 코드에서 Huma..

객체: 기본 - new , constructor

개발을 하다 보면 유사한 객체를 여러 개 만들어야 할 때가 생기곤 합니다. 이럴 때 사용하는 것이 new연산자와 constructor함수 입니다. constructor ( 생성자 함수 ) 생성자 함수와 일반 함수에 기술적인 차이는 없습니다. 대신 생성자 함수에는 2가지 관례가 있습니다. 1. 함수 이름의 첫 글자는 대문자로 시작합니다. 2. 반드시 'new' 연산자를 붙여 실행합니다. function User(name){ this.name = name; this.isGood = false; } let user1 = new User("Lee"); console.log(user1.name)// Lee console.log(user1.isGood)// false new 함수명( ... ) 을 사용해서 함수를 ..