Chapter 4 · Real programs

Prototypes: objects that share

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

Objects that share

By the end of this lesson you can:

  • give an object methods that use this,
  • link one object to another, so it can use the other's properties,
  • see where push and map really live.

Lesson 15 gave each account its own functions. Here many objects share one.

const rex = {
  name: "Rex",
  speak() {
    return `${this.name} says woof`;
  },
};
const tom = {
  name: "Tom",
  speak: rex.speak,
};
console.log(rex.speak());
console.log(tom.speak());
const book = {
  title: "Dune",
  pages: 412,
  // describe() returns "Dune, 412 pages"
};
const guide = {
  title: "Maps",
  pages: 96,
  // the same describe() here
};
console.log(book.describe());
console.log(guide.describe());
const animal = { legs: 4 };
const rex = Object.create(animal);
rex.name = "Rex";
const pup = Object.create(rex);
console.log(pup.legs, pup.name);
const animal = {
  legs: 4,
  speak() {
    return `${this.name} makes a sound`;
  },
};
const rex = Object.create(animal);
rex.name = "Rex";
console.log(rex);
console.log(Object.hasOwn(rex, "legs"));
console.log(Object.hasOwn(animal, "legs"));
console.log(Object.getPrototypeOf(rex) === animal);
rex.fly();
const animal = { legs: 4 };
const bird = Object.create(animal);
bird.legs = 2;
console.log(bird.legs, animal.legs);
console.log(Object.hasOwn(bird, "legs"));
const animal = {
  sound: "...",
  // 1. speak() returns "<name> says <sound>"
};
const cat = Object.create(animal);
cat.name = "Tom";
// 2. the cat's own sound is "meow"
const fish = Object.create(animal);
fish.name = "Nemo";
console.log(cat.speak());
console.log(fish.speak());
const proto = {
  deposit(amount) {
    this.balance += amount;
  },
};
const a = Object.create(proto);
a.balance = 0;
const b = Object.create(proto);
b.balance = 10;
a.deposit(5);
b.deposit(5);
console.log(a.balance, b.balance, a.deposit === b.deposit);

function makeAccount() {
  let balance = 0;
  const deposit = (amount) => {
    balance += amount;
  };
  const getBalance = () => balance;
  return { deposit, getBalance };
}
const c = makeAccount();
const d = makeAccount();
c.deposit(5);
d.deposit(1);
const same = c.deposit === d.deposit;
console.log(c.getBalance(), d.getBalance(), same);
console.log([1].push === [2].push, String({}));
// 1. shape with describe()
// 2. square and rect, linked to shape
// 3. log both descriptions and the hasOwn answer
const animal = {
  sound: "...",
  speak() {
    return `${this.name} says ${this.sound}`;
  },
};
const cat = Object.create(animal);
cat.name = "Tom";
const fish = Object.create(animal);
fish.name = "Nemo";
// after the pets: give animal a method trick()
console.log(cat.trick());
console.log(fish.trick());
console.log(Object.hasOwn(cat, "trick"));
Terminal
Step 1 of 9The object before the dot

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)