bhylu.com

Advertisements

Object this

A method can read its receiver through this. The same function can operate on different receivers when called explicitly.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

A practical example

JavaScript

function label() { return this.title; }
const first = {title: "Repair club", label};
const second = {title: "Woodwork", label};
console.log(first.label(), second.label());

What happens

Each method call reads its own object title.