반응형
function Health(name){
this.name = name;
}
Health.prototype.showHealth = function(){
console.log(this.name + '님 안녕하세요.');
}
const h = new Health('gold');
h.showHealth();
class Health{
constructor(name, lastTime){
this.name = name;
this.lastTime = lastTime;
}
showHealth(){
console.log('안녕하세요.' + this.name);
}
}
const h = new Health('Gold');
h.showHealth();
겉보기에는 class 이지만 toString.call(Health); 해보면 [object Function] 이다.
class와 같이 모듈화 해놓으면 가독성이 좋다.
const healthObj = {
showHealth : function(){
console.log('오늘 운동시간: ' + this.healthTime);
}
}
const myHealth = Object.create(healthObj);
myHealth.healthTime = '11:00';
myHealth.name = 'gold';
console.log(healthObj === myHealth);
console.log(myHealth);
const myHealth2 = Object.assign(Object.create(healthObj), {
name : 'crong',
lastTime : '11:10'
});
console.log(myHealth2);
아래 assign을 통해서 새로운 객체를 만들어 줄 수 있다.
const previousObj = {
name : 'gold',
lastTime : '12:00'
}
const myHealth2 = Object.assign({}, previousObj, {
name : 'Gold',
lastTime : '13:00'
});
console.log(myHealth2);
setPrototypeOf
반응형
'인강 > 모던 자바스크립트(javascript) 개발을 위한 ES6 강좌' 카테고리의 다른 글
Arrow Function (0) | 2023.01.26 |
---|---|
Template (0) | 2023.01.26 |
Set (0) | 2023.01.24 |
Obj (0) | 2023.01.24 |
Array (0) | 2023.01.24 |