๊ด€๋ฆฌ ๋ฉ”๋‰ด

C-log

section29-ํ”„๋กœํ† ํƒ€์ž…, ํด๋ž˜์Šค, ๊ทธ๋ฆฌ๊ณ  OOP(๋ฐฑ์—”๋“œ๋ฅผ ๋“ฃ๊ณ  ๋‚˜์ค‘์— ๋‹ค์‹œ ๋“ค์–ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค) ๋ณธ๋ฌธ

๐Ÿ“’JS/โšกver.0

section29-ํ”„๋กœํ† ํƒ€์ž…, ํด๋ž˜์Šค, ๊ทธ๋ฆฌ๊ณ  OOP(๋ฐฑ์—”๋“œ๋ฅผ ๋“ฃ๊ณ  ๋‚˜์ค‘์— ๋‹ค์‹œ ๋“ค์–ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค)

4:Bee 2023. 4. 4. 22:49
728x90
Topic explain class
Prototype prototype/__proto__ ํ”„๋กœํ† ํƒ€์ž…์€ ์ฒญ์‚ฌ์ง„๊ณผ ๊ฐ™๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด arr.includes(2)๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ includes()์˜ ํ•จ์ˆ˜๋Š” ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด์—์„œ ์ •์˜๋œ๋‹ค. ํ”„๋กœํ† ํƒ€์ž…์ด๋ž€?
String.prototype.yell = function () {
  return `OMG!! ${this.toUpperCase()}!!!!`
}

Array.prototype.pop = function() {
  return 'SORRY I WANT THAT ELEMENT, I WILL NEVER POP IT OFF!';
}
 
์—ฌ๊ธฐ์„œ prototpye์€ ๋ฉ”์„œ๋“œ๋‚˜ ํŠน์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋Š” ์‹ค์ œ ๊ฐ์ฒด์ด๋‹ค. ํ…œํ”Œ๋ฆฟ ๊ฐ์ฒด, ์ฆ‰ ํ”„๋กœํ† ํƒ€์ž…์ด๋‹ค.  

 
์—ฌ๊ธฐ์„œ [[prototpye]]/(__proto__)์€ ์ฐธ์กฐ์ด๋‹ค. [[prototpye]]/(__proto__)์€ ์ด ๋ฐฐ์—ด์ด๋‚˜ ๋ฌธ์ž์—ด์˜ ํŠน์„ฑ ์ด๋ฆ„์ด๋‹ค.  
OOP์˜ ๊ฐœ์š” - ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐœ์š”
ํŒฉํ† ๋ฆฌํ•จ์ˆ˜   ํŒฉํ† ๋ฆฌ๋ผ๋Š” ์šฉ์–ด๋Š” ์–ด๋–ค ๊ฐ’์„ ์ „๋‹ฌํ•˜๋ฉด ์ด ํŒฉํ† ๋ฆฌ๊ฐ€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ๋งˆ์ง€๋ง‰์— ๋ฐ˜ํ™˜ํ•˜์—ฌ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.  
์ดํ•ด๊ฐ€ ์ž˜์•ˆ๊ฐ”๋‹ค. ๋‹ค์‹œ ๋“ค์–ด๋ณด๊ณ  ๋‹ค์‹œ ๋ณด๊ธฐ
function makeColor(r,g,b){
  const color = {};
  color.r = r;
  color.g = g;
  color.b = b;
  return color;
}
 
ํŒฉํ† ๋ฆฌ ํ•จ์ˆ˜
  - ์ƒ์„ฑ์ž ํ•จ์ˆ˜
  ํด๋ž˜์Šค   JavaScriptํด๋ž˜์Šค
 
class Color {
  constructor(r, g, b, name) {
    this.r = r;
    this.g = g;
    this.b = b;
    this.name = name;
    // console.log("constructor");
    // console.log(r, g, b);
  }
  greet() {
    return `Hello From A Color!! ${this.name}`
  }
}

const c1 = new Color(32, 56, 78, 'idk');
       
       
       
       
       
728x90
Comments