function rooms(){
this.height = 200
this.width = 300
this.measure = function(){
return this.height * this.width
}
}const rule = new rooms()
console.log(rule) //rooms { height: 200, width: 300, measure: [Function] }
console.log(rule.measure) // [Function]
console.log(rule.measure()) // 60000const rule = new rooms()
new: Create a empty objectthis: this refers to the new object
get-> set a property
class rooms {
constructor() {
this.height = 200
this.width = 300
}
get getMeasure() {
return this.height * this.width;
}
measureMethod() {
return this.height * this.width;
}
}const rule = new rooms();
console.log(rule) // rooms { height: 200, width: 300 }
console.log(rule.getMeasure) // 60000
console.log(rule.getMeasure()) // is not a function
console.log(rule.measureMethod) // function
console.log(rule.measureMethod()) // // 60000- this -> refer to the room()
var room = {
height: 200,
width: 300,
measure:function() {
return this.height * this.width
}
}
console.log(room.measure()) // 60000- this -> refer to the global
var stolenFunction = room.measure
console.log(stolenFunction()) //NAN