π μ΄νν μ½λ© μκ° 5hour
ππΌ -
ππΌ -
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
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
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(...)κ° μ€νλ λ λ€μκ³Ό κ°μ μΌμ΄ λ°μνλ€:
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();
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
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).
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';
}
}