📁
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • 콜백함수 (Callback function)
    • 함수의 호출
    • 자료구조 new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • 객체
    • Arguments
    • higher order function
    • operators-function-control-flow
    • 객체 생성 패턴 3가지
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • 데이터 타입
    • Object 객체
    • 표준 내장 객체의 확장
    • 참조
    • 함수
    • 상속(Inheritance)
    • this - 'this'를 사용하는 해당 함수를 '어떻게' 실행하느냐에 따른 4가지 this 정의
    • 전역객체(Global object)
    • 객체 지향 프로그래밍
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - 자연수, 정수, 10진수, 2진수, 부동소수점, 실수
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • 함수형 컴포넌트와 클래스, 어떤 차이가 존재할까?
    • Hooks과 useEffect 사용해 보기
    • Route
    • async wait 사용하기
    • Hooks API Reference
    • context
    • npm uninstall 하는법
    • test 만들기
  • tip
    • 클린코드
    • BxSlider로 텍스트 흐르는 효과 만들기
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • 우아한 타입스크립트 2부
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • 우아한 타입스크립트
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • 머지 후 branch 삭제하기
    • 지옥에서 온 Git
    • reset-revert
    • develop branch 를 pull 하고 싶을때
  • conference-and-seminar
    • 모던 프론트엔드 개발환경의 이해
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • 알고리즘 코드리뷰
    • 스스로 공부하는 법
    • 바닐라코딩 수강 후기
    • async 과제 후기 - 비동기, 동기, 클로저, 배열과 객체
    • 유용한 사이트
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars 이용자 솔루션 모음
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • 타입과 추상화
      • 객체 지도
      • 이상한 나라의 객체
      • 추상화 기법
      • 05. 책임과 메시지
      • 07.함께 모으기
      • 04. 역할, 책임, 협력
      • 협력하는 객체들의 공동체
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • 웹 브라우저에 URL을 입력했을 때 어떻게 웹 페이지가 보여질까?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex 해버렸지 뭐야
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd 명령어 모음
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN 설정하기
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans 오답노트
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • 코드 스피츠 - CSS Rendering 1회차 2/2
    • 코드 스피츠 - CSS Rendering 1회차 1/2
  • you-dont-know-js
    • 타입
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • 생성자(constructor)
  • 객체 리터럴
  • 생성자의 역할 - 초기화(init, initialize)

Was this helpful?

  1. javascript

객체

Prototype-based programming 프로토타입기반 프로그래밍

객체지향프로그래밍이란 연관된 것들(변수와 메소드)을 그룹핑한다. 카테고라이징 한다. 하나하나가 독립성을 가지게 된다. -> 부품으로 사용

비어있는 상자라하자. 그 비어있는 상자엔 프로퍼티(속성)와 메소드를 담을 수 있다.

var person = {};
person.name = 'sla';
person.introduce = function(){
    return 'My name is '+this.name;
}
console.log(person.introduce());

이렇게 써놓는건 중간에 다른 코드가 들어 갈 수도 있고 객체가 분산되어 있다. 이를 합치자

var person = {
    'name': 'sla',
    'introduce': function(){
        return `My name is `+this.name;
    }
}
console.log(person.introduce());

훨씬 가독성이 좋다.

person을 여러명 만든다면

var person1 = {
    'name': 'sla',
    'introduce': function(){
        return `My name is `+this.name;
    }
}
var person2 = {
    'name': 'sla',
    'introduce': function(){
        return `My name is `+this.name;
    }
}

중복되는 부분이 많다.

생성자(constructor)

객체를 만드는 역할을 하는 함수다. 객체를 만드는 창조자

function Person(){} //함수를 정의한다.
var p0 = Person();
console.log(p0); //undefined가뜬다.

new를 추가해 정의한다면,

function Person(){} //함수를 정의한다.
//new를 붙이고 함수를 호출하면 호출한 함수를 생성자 라고 한다.
var p = new Person(); 
console.log(p); // Person {} 비어있는 객체

new를 붙이고 호출한 함수 Person()은 생성자 라고 한다. 그럼 뭐를 생성하는가. 객체의 생성자! Person은 new로 인해서 비여있는 {}객체를 만들고 그 객체를 반환하기 때문에 p에는 비어있는 객체가 만들어 진다.

var p = {} 이와 비슷하다고 할 수 있다.

이렇게 함수는 객체를 창조할 수 있는 창조자가 된다.

객체 리터럴

function Person(){} //함수를 정의한다.
var p = new Person(); 
p.name = 'sla';
p.introduce = function(){
    return 'My name is '+this.name;
};
console.log(p.introduce());

만약 person을 1..2..3...늘린다면..

function Person(){} //함수를 정의한다.
var p1 = new Person(); 
p1.name = 'sla';
p1.introduce = function(){
    return 'My name is '+this.name;
};
console.log(p1.introduce());

var p2 = new Person(); 
p2.name = 'sla2';
p2.introduce = function(){
    return 'My name is '+this.name;
};
console.log(p2.introduce());

중복되는 코드가 많다. 개선되는게 없다.

생성자의 역할 - 초기화(init, initialize)

function Person(name){
    this.name = name;
    this.introduce = function(){
        return 'My name is '+this.name;
    }
}
var p1 = new Person('sla');
var p2 = new Person('sla2');
console.log(p1.introduce()); 
console.log(p2.introduce());
  1. person이라는 함수를 정의하였다.

  2. p1에 Person 앞에 new를 붙여 더이상 함수가 아닌 객체의 생성자가 된다.

  3. 인자로 'sla'를 넣었다

  4. this.name = 'sla' 가 되어 name이라는 프로퍼티는 'sla'가 된다.

  5. 모든작업이 다 끝난 다음에 최종으로 p1에 이 모든게 담긴다.

초기화란 객체에 대해 초기화한다. Person이라는 생성자를 만든 이 빈 객체가 {} 어떠한 프로퍼티를 가져야하고 어떠한 메서드를 가져야하는지 생성자함수안에다 기술을 하는것을 통해서 객체가 가진 정보, 객체가 할 수 있는 일을 세팅을 해준다. -> 초기화

덕분에 코드의 재사용성이 대폭 높아졌다.

Previousarray-objectNextArguments

Last updated 5 years ago

Was this helpful?