How to do it...

  1. Open your command-line application and navigate to your workspace.
  2. Create a new folder named 06-04-define-function-properties-as-method.
  3. Copy or create an index.html that loads and runs a main function from main.js.
  4. Create a main.js with a function named main that defines two methods with the property and method syntax, overrides them, and calls them before and after override:
// main.js  
export function main() { 
     const obj = { 
    method0: function() { 
      console.log('Hello, from method one.') 
    }, 
       method1() { 
      console.log('Hello, from method one.') 
    } 
  }; 
  obj.method0(); 
  obj.method1(); 
 
  obj.method0 = () => console.log('Override of method 0.'); 
  obj.method1 = () => console.log('Override of method 1.'); 
  obj.method0(); 
  obj.method1(); 
}   

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.