JavaScriptFundamental (ES6), part 1
Classes, constructors and prototype methods in JavaScript
41 A class, its constructor and where its methods live
easyWrite a class with a constructor and one method, build two instances, and prove that the data sits on each instance while the method sits somewhere both of them share.
Stuck? Three hints, smallest first
- 1. Whatever the constructor assigns to
thisbecomes an own property of that one instance. - 2. A method written in the class body is installed on
ClassName.prototype, not on the instance. - 3.
Object.keysandJSON.stringifyboth list own enumerable properties only.
class Stall {
constructor(owner, area) {
this.owner = owner;
this.area = area;
}
label() {
return `${this.owner} of ${this.area}`;
}
}
const oyin = new Stall('oyin', 'Balogun');
const bola = new Stall('bola', 'Yaba');
const ownData = Object.keys(oyin);
const methodIsOwn = Object.prototype.hasOwnProperty.call(oyin, 'label');
const methodIsShared = Object.prototype.hasOwnProperty.call(Stall.prototype, 'label');
const sameFunction = oyin.label === bola.label;
const enumerable = Object.getOwnPropertyDescriptor(Stall.prototype, 'label').enumerable;
const seenByForIn = [];
for (const key in oyin) seenByForIn.push(key);
console.log('instance :', oyin);
console.log('label() :', oyin.label());
console.log('own keys :', ownData, '| label is own?', methodIsOwn);
console.log('on prototype:', methodIsShared, '| one function for both instances?', sameFunction);
console.log('serialised :', JSON.stringify(oyin));
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('the constructor put two own properties on the instance', ownData, ['owner', 'area']);
test('the method calls with this bound to the receiver', oyin.label(), 'oyin of Balogun');
test('the method is not an own property of the instance', methodIsOwn, false);
test('it is an own property of the prototype', methodIsShared, true);
test('so both instances hold the very same function object', sameFunction, true);
test('class methods are not enumerable', enumerable, false);
test('which is why for...in over the instance sees only data', seenByForIn, ['owner', 'area']);
test('and only that data serialises', JSON.stringify(oyin), '{"owner":"oyin","area":"Balogun"}');
test('the instance still knows which class built it', oyin.constructor.name, 'Stall');
The saving is the fifth assertion. A thousand stalls share one label function, because the method is on the prototype and the instance only holds data. Write the same thing as an object literal with a method inside a factory and you get a thousand function objects.
The trap is the sixth. Class methods are deliberately non-enumerable, unlike a method you assign into an object literal, so for...in, Object.keys and JSON.stringify all skip them. That is usually a relief — no behaviour leaking into your JSON — but it also means a quick Object.keys(instance) tells you nothing about what the instance can do.