๐Ÿ“
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

Was this helpful?

  1. javascript

prototype

์›ํ˜• (์›๋ž˜์˜ ํ˜•ํƒœ) prototype์„ ์ด์šฉํ•˜์—ฌ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์ƒ์†๊ฐœ๋…์„ ์ œ๊ณต

(prototype chain)

function Ultra(){};
Ultra.prototype.ultraProp = true;

function Super(){};
Super.prototype = new Ultra();

function Sub(){};
Sub.prototype = new Super();

var o = new Sub();
console.log(o.ultraProp); //true

๋จผ์ € ์ƒ์„ฑ์ž ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉด, ์ƒ์„ฑ์ž๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•จ์ˆ˜์ด๋‹ค. ํ˜ธ์ถœํ•  ๋•Œ ํ•จ์ˆ˜์•ž์— new๋ฅผ ๋ถ™์ด๋ฉด์„œ ํ•จ์ˆ˜๋Š” ์ผ๋ฐ˜ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ƒ์„ฑ์ž๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ๋˜๊ณ  ๊ทธ๋ ‡๊ฒŒ ํ•ด์„œ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ทธ ๊ฐ์ฒด๋ฅผ returnํ•˜๊ณ  o๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ธด๋‹ค.

o๋ผ๋Š” ๋ณ€์ˆ˜์•ˆ์—๋Š” ์ƒ์„ฑ์ž๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๊ฐ€ o ์•ˆ์— ๋“ค์–ด์žˆ๋‹ค.

var o = {}

์ด๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š๊ณ  ์ƒ์„ฑ์ž๋กœ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ์ด์œ ๋Š”.. ? ์šฐ๋ฆฌ๊ฐ€ ์–ด๋– ํ•œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ, ๊ทธ ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•˜๋Š” ์–ด๋–ค ๋ฉ”์†Œ๋“œ๋‚˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ๋ฅผ ๋ฐ”๋ผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋กœ์ง์ด ํ•„์š”ํ•œ๊ฑฐ์ง€, {} ๋น„์–ด์žˆ๋Š” ๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•œ๊ฒŒ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

function Sub(){};
Sub.prototype = new Super();

๊ทธ๋Ÿผ, ์šฐ๋ฆฌ๊ฐ€ ์–ป๊ณ ์ž ํ•˜๋Š” ๊ฐ์ฒด์˜ ์›ํ˜•, ๊ฐ์ฒด๊ฐ€ ์–ด๋–ค๋ฉ”์†Œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋Š” prototype ์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ์— ์ด ๊ณณ์— ๋‹ด๊ธด๋‹ค. Sub๋Š” ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ณ  ๊ทธ ํ”„๋กœํผํ‹ฐ๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์— ์•ฝ์†๋œ prototype๋ฅผ ์“ฐ๋Š”๋ฐ ์ด ์•ˆ์—” ๊ฐ์ฒด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.

var o = new Sub();

๊ทธ๋Ÿผ new๋ฅผ ์ด์šฉํ•ด์„œ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉด, ์ƒ์„ฑ์ž ํ•จ์ˆ˜ Sub์˜ prototype ์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด์„œ ๊ทธ๊ฒƒ์„ returnํ•ด์ค€๋‹ค.

function func(){};

console.log(func.prototype); //func {}

func.pototype.name = 'egoing';

var o = new func();

console.log(o); //func {name: 'sla'};

์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋ฉด prototype์— ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฆฌํ„ด์ด ๋œ๋‹ค.

prototype chain

function Ultra(){};
Ultra.prototype.ultraProp = true;

function Super(){};
Super.prototype = new Ultra();

function Sub(){};
Sub.prototype = new Super();

var o = new Sub();
o.ultraProp = 1;
console.log(o.ultraProp); // 1

์ œ์ผ ์ฒ˜์Œ o๋ผ๋Š” ๊ฐ์ฒด์— ํ”„๋กœํผํ‹ฐ๋กœ ultraProp๊ฐ€ ์žˆ๋Š”์ง€ ์ฐพ๋Š”๋‹ค.

function Ultra(){};
Ultra.prototype.ultraProp = true;

function Super(){};
Super.prototype = new Ultra();

function Sub(){};
Sub.prototype = new Super();
Sub.prototype.ultraProp = 2;

var o = new Sub();
console.log(o.ultraProp); // 2

o์— property๋กœ ultraProp๊ฐ€ ์—†์œผ๋ฏ€๋กœ Sub๋กœ ์˜ฌ๋ผ๊ฐ€ 2๊ฐ€ ๊ฐ’์ด ๋œ๋‹ค.

function Ultra(){};
Ultra.prototype.ultraProp = true;

function Super(){};
Super.prototype = new Ultra();

function Sub(){};
var s = new Super();
s.ultraProp = 3;
Sub.prototype = s;

var o = new Sub();
console.log(o.ultraProp); // 3

o์—๋„ ์—†๊ณ  ๊ทธ๋Ÿผ Sub์˜ prototype์„ ๋’ค์ง€๋Š”๋ฐ Sub์˜ prototype์œผ๋กœ new Super() ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ๊ทธ ๊ฐ์ฒด์˜ ultraProp = 3์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์„œ 3์ด ๋„์ถœ๋œ๋‹ค.

function Ultra(){};
Ultra.prototype.ultraProp = true;

function Super(){};
var t = new Ultra();
t.ultraProp = 4;
Super.prototype = t;

function Sub(){};
var s = new Super();
Sub.prototype = s;

var o = new Sub();

console.log(o.ultraProp); //4
function Ultra(){};
Ultra.prototype.ultraProp = true;

function Super(){};
var t = new Ultra();
Super.prototype = t;

function Sub(){};
var s = new Super();
Sub.prototype = s;

var o = new Sub();

console.log(o.ultraProp); // true

ultraProp๋ฅผ ์ฐพ๊ธฐ์œ„ํ•ด ๊ณ„์† ์ฒด์ธ์ฒ˜๋Ÿผ ์˜ฌ๋ผ๊ฐ„๋‹ค.

function Sub(){}
//Sub.prototype = new Super(); ์ƒ์†๋ฐ›๊ณ ์ž ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋„ฃ์–ด์•ผํ•˜๋Š”๋ฐ 

  // ์ƒ์†๋ฐ›๊ณ ์žํ•˜๋Š” property๋ฅผ ๋„ฃ๋Š”๋‹ค๋ฉด ๋ถ€๋ชจ ๊ฐ์ฒด๊นŒ์ง€ ์˜ํ–ฅ์„ ๋ผ์นœ๋‹ค.
Sub.prototype = Super.prototype

Sub์—๋งŒ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ถ”๊ฐ€ ๋˜๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ. Super์— ๊นŒ์ง€ ์ถ”๊ฐ€๋˜์–ด์„œ ์˜ํ–ฅ์„ ๋ผ์นœ๋‹ค.

์–ด๋– ํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์†๋ฐ›๊ณ ์ž ํ•œ๋‹ค๋ฉด ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ž

PreviousthisNext์ฝœ๋ฐฑํ•จ์ˆ˜ (Callback function)

Last updated 5 years ago

Was this helpful?