
const person = {};
// μμ±, property
person.name = 'μΌλΆμ΄';
person.age = 10;
// λ©μλ, method
person.introduce = function () {
console.log('μλ
νμΈμ. μ λ μΌλΆμ΄μ΄κ³ λμ΄λ 10μ΄μ΄μμ');
};
person.introduce();
const perseon = {
name: 'μΌλΆμ΄',
age: 10,
introduce: function () {
console.log('μλ
νμΈμ μ λ μΌλΆμ΄μ΄κ³ , λμ΄λ 10μ΄μ΄μμ');
},
};
const person = {
name: 'μΌλΆμ΄',
age: 10,
introduce: function () {
console.log(`μλ
νμΈμ μ λ ${this.name}μ΄κ³ , λμ΄λ ${this.age}μ΄μ΄μμ`);
},
};
μ§κΈ ECMA Script μμλ
classλ¬Έλ²μ΄ μΆκ°λμ. μ΄κ±΄ μλ λ²μ !
new λΆμ΄λ©΄ λ¨. κ·Έλμ κ°κ° κ°λ³ κ°μ²΄λ₯Ό thisκ° κ°λ¦¬ν€κ² λ κ²μΈμ€ν΄μ€
= κ°κ°μ μμ±μ ν¨μλ‘ λ§λ€μ΄λΈ κ°μ²΄
/// μμ±μ (constructor)
function Person(nickname, age) {
this.nickname = nickname;
this.age = age;
this.introduce = function () {
console.log(`μλ
νμΈμ μ λ ${this.nickname}μ΄κ³ , λμ΄λ ${this.age}μ΄μμ`);
};
}
// μΈμ€ν΄μ€ (instance)
const person1 = new Person('μΌλΆμ΄', 10);
const person2 = new Person('μ΄λΆμ΄', 8);
person1.introduce();
person2.introduce();
introduceλthis.nickname,this.ageμ κ°μ΄ κ°μ λ 벨μ μκΈ° λλ¬Έμ λ©λͺ¨λ¦¬ μ μ . νμ§λ§introduceλ λ€ λκ°μ prop. κ·Έλμ λΉν¨μ¨μ . λ©λͺ¨λ¦¬ λλΉμ=> νλλ§ λ§λ€μ΄λκ³ κ°λ³ κ°μ²΄λ€μ΄ 곡μ νκ²λ

function Card(num, color) {
this.num = num;
this.color = color;
}
Card.prototype.width = 100;
Card.prototype.height = 150;
const card1 = new Card(1, 'green');
const card2 = new Card(2, 'red');
console.log(card1.color);
console.log(card2.color);
console.log(card1.width);
console.log(card2.width);

function Card(num, color) {
this.num = num;
this.color = color;
this.init();
}
Card.prototype = {
constructor: Card,
init: function () {
const mainElm = document.createElement('div');
mainElm.style.color = this.color;
mainElm.innerHTML = this.num;
mainElm.classList.add('card');
document.body.appendChild(mainElm);
},
};
const card1 = new Card(1, 'green');
const card2 = new Card(2, 'red');
class Card {
constructor(num, color) {
this.num = num;
this.color = color;
this.init();
}
init() {
const mainElm = document.createElement('div');
mainElm.style.color = this.color;
mainElm.innerHTML = this.num;
mainElm.classList.add('card');
document.body.appendChild(mainElm);
}
}
const card1 = new Card(1, 'green');
const card2 = new Card(2, 'red');
