How to reassigned object inside property(function)

Problem changing

let school={
  shName:'Rp public',
  totStudent:200,
  day:'Monday',
  today(){
    return this.day
  }
}
console.log("Today's Day is "+school.today())
school.today()=function today(){
  return 'Sunday';
}
console.log("Today's Day is "+school.today())

Hi, and welcome to the forums!

One thing to recall with regards to object methods is the difference between fetching a method and calling a method. When you do

console.log(school.today())
// logs "Monday"

You are calling the method, as you have added the brackets at the end. Therefore this runs the method and returns the result. However if you want to fetch the actual content of a method, you have to call it like you would any other property.

school.today
// logs "[Function: today]"

In this one, you are fetching the actual method itself without calling and running it. This is what you need to overwrite if you want to replace it with a new method content. Therefore it should be

school.today = function today() {//your code here}
1 Like