// function Circle(radius) {
// this.radius = radius
// this.getArea = function(){
// return Math.PI * this.radius ** 2;
// }
// }
function Circle(radius) {
this.radius = radius
}
Circle.prototype.getArea = function(){
return Math.PI * this.radius ** 2;
}
Circle.prototype.PI = Math.PI // circle1.pi로만 접근 가능
Circle.PI = Math.PI // circle.pi로만 접근 가능(생성자이름)
const circle1 = new Circle(1);
const circle2 = new Circle(2);
console.log(circle1.getArea == circle2.getArea)
console.log(circle1.getArea === circle2.getArea)
console.log(circle1.getArea())
console.log(circle2.getArea())
console.log(circle1.PI)
console.log(circle1.__proto__.PI) // 이거랑
console.log(Circle.PI) // 이거랑 동일
console.log("--------------------")
// Date
// Date 타입의 인스턴스에 toMyString이라는 메서드를 제작 >
// yyyy-MM-DD HH:MM:SS 형태의 시간정보를 반환
Number.prototype.lz = function(){
return this < 10 ? "0" + this : ""+this;
}
Date.prototype.toMyString = function(){
return `${this.getFullYear()}/${(this.getMonth() + 1).lz()}/${this.getDate().lz()}`
+ ` ${this.getHours().lz()}:${this.getMinutes().lz()}:${this.getSeconds().lz()}`
}
const now = new Date();
console.log(now.toMyString())
const Person = (function(){
function Person(name){
this.name = name
}
Person.prototype.sayHello = function(){
console.log(`Hi! My name is ${this.name}`)
}
return Person
}())
const me = new Person('Lee')
me.sayHello()
me.sayHello = function(){
console.log(`Hi! My name is ${this.name}`)
}
me.sayHello()
// 프로퍼티 제거
delete me.sayHello;
me.sayHello;
me.sayHello()
delete Person.prototype.sayHello;
// me.sayHello();
댓글