Defining function properties as methods on a plain object
Defining methods on object literals has always been possible with normal key-value pairs. More recent versions of ECMAScript have added a shorthand that mimics the syntax for defining methods on classes.
In this recipe, we'll see that we can create and override methods on object literals using either technique.
Getting ready
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named
06-04-define-function-properties-as-method
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create a
main.js
with a function namedmain
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...