Prototype-based object orientation links objects so one can use properties and methods found on another. In JavaScript, a property lookup can follow an object’s internal prototype link up a chain. This is delegation: the linked object supplies behavior without its properties first being copied onto the receiving object. section 2.1 section 3.1.1
Listing 2.2 Transaction objects linked by a basic prototype setup
const transaction = { #1
sender: 'luis@tjoj.com',
recipient: 'luke@tjoj.com'
};
const moneyTransaction = Object.create(transaction); #2
moneyTransaction.funds = 0.0;
moneyTransaction.addFunds = function addFunds(funds = 0) { #3
this.funds += Number(funds);
}
moneyTransaction.addFunds(10.0);
moneyTransaction.funds; // 10.0
The transaction example uses Object.create(transaction) to link a money transaction to a shared object, then adds funds and addFunds directly to the new object. section 2.1
The link is implicit in ordinary property access. A money transaction can read a sender supplied by transaction, while its own addFunds method updates its funds through this. Object.getPrototypeOf(moneyTransaction) can verify the link. Delegated methods use the receiving object’s context, so a method found higher in the chain can work with the object on which it was called. section 2.1 section 3.1.1
Choose an object-building style with its underlying behavior in mind:
- Object.create establishes a direct link to a chosen prototype. section 2.1
- Constructor functions and classes also use implicit prototype links; class syntax smooths over their configuration rather than replacing the mechanism. section 2.4 section 3.1.1
- Object.assign can instead assemble an object from copied properties. Copying avoids depending on a long delegation chain, though it is a different form of reuse. section 3.3
A shared prototype makes behavior reusable, but a change to a base object can affect objects farther down its delegation chain. Keep that coupling in view when modeling a domain: prototypes are useful for more than parent–child relationships, and copying or composing properties may better suit a model that needs to change. section 2.0 section 3.3

