TIL025_210419

JIYOONΒ·2021λ…„ 4μ›” 20일
0

TIL

λͺ©λ‘ 보기
25/42
post-thumbnail

🍊 감상

πŸ“™ μ—΄ν’ˆνƒ€ μ½”λ”© μ‹œκ°„ 5hour
πŸ‘πŸΌ -
πŸ‘ŽπŸΌ -

πŸš€ λͺ©ν‘œ

  • Udemyμ—μ„œ Javascript κ°•μ’Œ μˆ˜κ°•ν•˜κΈ° (365/682)
  • 개인 ν”„λ‘œμ νŠΈ 진행

πŸ“£ The Web Developer Bootcamp 2021

29. Prototypes, classes & OOP

295. Factory function

function rgb(r, g, b) {
  return `rgb(${r},${g},${b})`;
}

function makeColor(r, g, b) {
  //building an object
  const color = {}; //make empty object
  color.r = r; //property
  color.g = g;
  color.b = b;
  color.rgb = function () {
    //method
    //console.log(this); //{r:35, g:355, b:150, rgb:f}
    const { r, g, b } = this; //refer to color object
    return `rgb(${r},${g},${b})`;
  };
  color.hex = function () {
    const { r, g, b } = this; //refer to color object
    return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
  };
  return color;
}
const firstColor = makeColor(35, 355, 150);
firstColor.rgb();
firstColor.hex();
// in this method, the value of this will refer to the object to the left of the dot

296. Constructor functions

why the factory function is not commonly used, instead why we commonly use constructor pattern or constructor function

new is an operator

new XMLHttpRequest();
//this makes us a new object following a pattern

const firstColor = makeColor(35, 355, 150);
firstColor.rgb();
firstColor.hex();
const black = makeColor(0, 0, 0);
firstColor.rgb();
firstColor.hex();

console.log(black.hex === firstColor.hex) //false
console.log("hi".slice === "bye".slice) //true

What's happening here?
slice method is not defined on every single string -> they are defined on the prototype
instead of having their own method copy, they are referencing one function and it's located on the prototype

function Color(r,g,b) {
  this.r = r;
  this.g = g;
  console.log(this); //window -> global scope
}

ν•˜μ§€λ§Œ new μ‚¬μš©ν•˜λ©΄ new objectλ₯Ό λ§Œλ“€μ–΄μ€Œ based off of this pattern

  • links (sets the constructor of) this object to another object
    (now we can add the method not to the individual objects, not to the instances, to the prototype)

new keyword -> makes new object, returns the object`

function Color(r,g,b) {
  this.r = r;
  this.g = g;
  console.log(this); //window -> global scope
}

Color(255,0,0) //window {...}
new Color(255,0,0) //Color{r:255,g:0,b:0}

μ½”λ“œ new Foo(...)κ°€ 싀행될 λ•Œ λ‹€μŒκ³Ό 같은 일이 λ°œμƒν•œλ‹€:

  1. Foo.prototype을 μƒμ†ν•˜λŠ” μƒˆλ‘œμš΄ 객체가 ν•˜λ‚˜ μƒμ„±λœλ‹€.
    Creates a blank, plain JavaScript object.
  2. λͺ…μ‹œλœ 인자 그리고 μƒˆλ‘­κ²Œ μƒμ„±λœ 객체에 λ°”μΈλ“œλœ this와 ν•¨κ»˜ μƒμ„±μž ν•¨μˆ˜ Fooκ°€ ν˜ΈμΆœλœλ‹€.new FooλŠ” new Foo()와 λ™μΌν•˜λ‹€. 즉 μΈμžκ°€ λͺ…μ‹œλ˜μ§€ μ•Šμ€ 경우, 인자 없이 Fooκ°€ ν˜ΈμΆœλœλ‹€.
    Adds a property to the new object (__proto__)

that links to the constructor function's prototype object
Properties/objects added to the construction function prototype are therefore accessible to all instances created from the constructor function (using new).
3. μƒμ„±μž ν•¨μˆ˜μ— μ˜ν•΄ λ¦¬ν„΄λœ κ°μ²΄λŠ” 전체 new 호좜 κ²°κ³Όκ°€ λœλ‹€. λ§Œμ•½ μƒμ„±μž ν•¨μˆ˜κ°€ λͺ…μ‹œμ μœΌλ‘œ 객체λ₯Ό λ¦¬ν„΄ν•˜μ§€ μ•ŠλŠ” 경우, 첫 번째 λ‹¨κ³„μ—μ„œ μƒμ„±λœ 객체가 λŒ€μ‹  μ‚¬μš©λœλ‹€.(일반적으둜 μƒμ„±μžλŠ” 값을 λ¦¬ν„΄ν•˜μ§€ μ•ŠλŠ”λ‹€. κ·ΈλŸ¬λ‚˜ 일반적인 객체 생성을 μž¬μ •μ˜(override)ν•˜κΈ° μ›ν•œλ‹€λ©΄ κ·Έλ ‡κ²Œ ν•˜λ„λ‘ 선택할 수 μžˆλ‹€.)
Binds the newly created object instance as the this context (i.e. all references to this in the constructor function now refer to the object created in the first step).
Returns this if the function doesn't return an object.

function Color(r, g, b) {
  this.r = r;
  this.g = g;
  this.b = b;
}

Color.prototype.rgb = function () {
  const { r, g, b } = this;
  return `rgb(${r},${g},${b})`;
};

Color.prototype.hex = function () {
  const { r, g, b } = this;
  return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
};

Color.prototype.rgba = function (a = 1.0) {
  const { r, g, b } = this;
  return `rgba(${r},${g},${b},${a})`;
};

const color1 = new Color(40, 50, 60);
const color2 = new Color(0, 0, 0);

color1.hex(); //color1 will be the value of 'this'
console.log(color1.hex === color2.hex); //they are pointing to the same thing
//they are on the shared proototype of object rather than uniquely defined on each instance

document.body.style.backgroundColor = color1.rgba();

Javascript Classes

syntactic sugar

class Color {
  constructor(r, g, b) {
    //constructor is a function that will execute immediately
    //whenever a new color is created
    //whenever we instantiate a new instance of Color
    console.log(r, g, b, name);
    this.r = r;
    this.g = g;
    this.b = b;
    this.name = name;
  }
  greet() {
    return `hello ${this.name}`;
  }
  innerRGB() {
    const { r, g, b } = this;
    return `${r},${g},${b}`; //'255,255,255'
  }
  rgb() {
    return `rgb(${this.innerRGB()})`;
  }
  rgba(a = 1.0) {
    return `rgba(${this.innerRGB()},${a})`;
  }
}
const white = new Color(255, 255, 255, 'white');
const red = new Color(255, 255, 255, 'white');
white.greet();
console.log(red.greet === white.greet); //true, compare the refrences
//they are the same function because they are on the porotype
//they are not in the individual instance

define a class with the class keyword, capitalize the name
construnction function will run automatically whenever you instantiate a new instance of the class (we don't call it manually)

keyword this will refer to the individual object
assign properties to each Color (not to the prototype)
methods will added to the prototype automatically
define class and define pattern for every Color
methods look up to the one prototype template object

298. More Classes practice

When talking about OOP, we can have the Class, that is, the 'mold' that we can use to create several copies that will follow that pattern, like cake molds. Taking cakes as an analogy, each cake baked in that mold is an instance of that mold, and it will inherit its default characteristic; for the cake, it's mostly the shape, but for a Class instance, it's the default properties that are inherited, but that instance can have additional properties (the same way we can bake different cake flavors using the same mold).

299. Extends and Super keywords

class Pet {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  eat() {
    return `${this.name} is eating`;
  }
}

class Cat extends Pet {
  constructor(name, age, livesLeft = 9) {
    //super is going to reference the class that we are extending from
    //reuse functionality from construnctor
    //super is referenct to the super clss
    super(name, age);
    this.livesLeft = livesLeft;
  }
  meow() {
    return 'meow';
  }
} //use constructor in Pet

class Dog extends Pet {
  bark() {
    return 'woof';
  }
  eat() {
    return 'kung';
  }
}

0개의 λŒ“κΈ€