본문 바로가기

javascript93

new 연산자 new 연산자 => 첫 문자는 대문자 일반함수와 생성자 함수의 특별한 형식적 차이는 없음. new 연산자와 함께 함수를 호출하면 해당 함수는 생성자함수로 동작함. 함수 객체 내부 메서드 [[Call]]이 호출되는 것이 아닌 [[Constructor]]가 호출됨. new 연산자와 함께 호출하는 함수는 non-constructor가 아닌 constructor여야 함. function Circle(radius) { this.radius = radius; this.getDiameter = function () { return 2 * this.radius; }; } const circle = Circle(5); //new연산자 없이 호출하면 일반함수로 호출. console.log(circle); //undefin.. 2022. 2. 27.
Prototype method vs Object method Object에서 생성시점에 Method를 선언하는 방법. 1. 객체 생성자 내부에서 this.func = function () {} 2. obj.prototype.func = function () {} 과 같이 prototype을 이용하는것. 1. Prototype 상속(inheritance)은 객체지향 프로그래밍의 핵심 개념으로, 어떤 객체의 프로퍼티 또는 메서드를 상속받아 그대로 사용할 수 있는것 자바스크립트는 프로토타입을 기반으로 상속을 구현하여 불필요한 중복을 제거함. 중복을 제거하는 방법은 기존의 코드를 적극적으로 재사용하는것. function Circle(radius) { this.radius = radius; this.getArea = function () { return Math.PI * .. 2022. 2. 27.
if..else / if.. else if if : 조건이 true인 경우 statement1 실행 if (condition) { // block of code to be executed if the condition is true } if..else : 조건이 true인 경우 statement1 실행. false일 경우 statement2실행 if (condition) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false } if..else if : 조건1이 true인 경우 statement1실행. 조건1이 false이고 조건2가 true인 경우 실행 if (condition.. 2022. 2. 27.
Early Return early return이란 무엇인가? 중첩되는 조건문들을 막고 가독성이 높은 코드를 구현하기 위해 사용됨. 왜쓰는가? 1. 중첩된 조건문은 가독성이 좋지 않다. 2. if부분이 길게되면 오류처리할 때 혼란스러움. 3. else부분에서 예외가 나타남. BinarySearchTree.prototype.insert = function (value) { const newNode = new BinarySearchTree(value); if (this.value > value) { if (this.left == null) { this.left = newNode; } else { this.left.insert(value); } } else { //root.value와 파라미터로 입력된 value의 값이 같을 때는 추.. 2022. 2. 27.