Skip to content

Latest commit

 

History

History
77 lines (61 loc) · 1.29 KB

File metadata and controls

77 lines (61 loc) · 1.29 KB

Constructor

Function constructor

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())  // 60000

const rule = new rooms()

  1. new: Create a empty object
  2. this: this refers to the new object

Class constructor

  • 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

Object

  • 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