liveBook · Manning

Method

As explained in Essential TypeScript 5, Third Edition

A method is an object property whose value is a function and which can be invoked as part of the object.

A method is an object property whose value is a function. Call it through the object when the function needs to work with that object’s data. Methods can use ordinary function features, including default and rest parameters. section 3.5.4

Define and call a method

In primer/index.js, `writeDetails: function() { console.log(`${this.name}: ${this.price}`); }` is a method property. The concise form omits the colon and `function`: `writeDetails() { console.log(`${this.name}: ${this.price}`); }`. Invoke either form with `hat.writeDetails()`. section 3.5.4

How a function is called matters. In `myObject.writeMessage("It is sunny today")`, `this` is `myObject`; the call is equivalent to `myObject.writeMessage.call(myObject, "It is sunny today")`. Assigning the function to a variable and calling that variable changes `this` in the illustrated environment. Watch for this when passing methods as callbacks or event handlers. section 3.6.2

Listing 10.8 Including a method in a shape type in the index.ts file in the src folder
enum Feature { Waterproof, Insulated }

let hat = { name: "Hat", price: 100 };
let gloves = { name: "Gloves", price: 75 };
let umbrella = { name: "Umbrella", price: 30, 
        hasFeature: (feature) => feature === Feature.Waterproof };
 
let products: { name: string, price?: number, 
        hasFeature?(Feature): boolean }[] 
    = [hat, gloves, umbrella];

products.forEach(prod => console.log(`${prod.name}: ${prod.price} ` 
    + `Waterproof: ${prod.hasFeature(Feature.Waterproof)}`));

The TypeScript `src/index.ts` listing declares `hasFeature?(Feature): boolean` in an object shape. Only the umbrella implements it, yet the loop calls it for every product; the missing methods cause a runtime TypeError. section 10.2.2

A shape type can specify a method’s parameter and result types. Put `?` after its name to make it optional. An absent optional method is `undefined`, so check that it exists before calling it: `prod.hasFeature ? prod.hasFeature(Feature.Waterproof) : "false"`. With `strictNullChecks` enabled, TypeScript flags an unchecked call as possibly undefined. section 10.2.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Works through definition, invocation-dependent `this`, and optional shape signatures, including an unchecked-call failure and its guard.
Examples
Uses a price-reporting object method, a detached greeting function, and products with an optional feature-checking method.
What it assumes
Builds on JavaScript functions and object properties; introduces method syntax and later applies it to TypeScript shape types.
What you can do after
Build it from scratch by defining object methods and shape signatures, then guard optional methods before calling them.
How it connects
Connects function values and object properties to `this`, callbacks, shape types, and strict null checking.

Also explained in

Compare how 20 books explain method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.