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



















