liveBook · Manning

Method

As explained in JavaScript in Depth

A property whose value is a function intended to be invoked as behavior associated with an object.

A method is a property whose value is a function intended to provide behavior associated with an object. Property lookup finds it directly or through the prototype chain. section 5.3.2

Finding a method and deciding what it operates on are separate steps. JavaScript determines this at runtime from how the function is created and called, allowing one method to serve different objects. section 5.4

Shared prototype behavior

const drawerPrototype = { open() {} }; const drawer = Object.create(drawerPrototype); drawer.open(); The open method is stored on the prototype, found through lookup, and invoked as behavior associated with drawer. section 5.3.2 section 5.4

const drawerPrototype = { open() {} };
const drawer = Object.create(drawerPrototype);
console.log(Object.getPrototypeOf(drawer) === drawerPrototype);  #A
console.log(Object.getPrototypeOf(drawerPrototype) ===
            Object.prototype);  #A
console.log(Object.getPrototypeOf(Object.prototype) === null);  #A

Trace the prototype links that make a method available on an object without being stored directly on it. section 5.3.2

How JavaScript in Depth covers it

How deep it goes
Explains method representation, prototype discovery, and runtime this binding, but stops short of implementation internals.
Examples
Uses drawerPrototype, Object.create, open, and prototype-chain inspection to demonstrate shared methods.
What you can do after
Evaluate method availability and explain which object a shared method operates on at call time.
How it connects
Ties methods to objects, functions, prototype chains, property lookup, and this.

Also explained in

Compare how 25 books explain method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.