Skip to content

Instantly share code, notes, and snippets.

class Arithmetic {
// inititalize the private property
#value;
// add getter for value
get val() {
return this.#value; // <== access the private property
}
// rest of the code truncated for clarity
class Arithmetic {
// add getter for value
get val() {
return this.value;
}
// rest of the code truncated for clarity
}
class Arithmetic {
constructor() {
this.value = 0;
}
sum(...args) {
this.value = args.reduce((sum, current) => sum + current, 0);
return this;
}
add(value) {
this.value = this.value + value;
class Arithmetic {
constructor() {
this.value = 0;
}
get val() {
return this.value;
}
sum(...args) {
this.value = args.reduce((sum, current) => sum + current, 0);
return this;
@segunolalive
segunolalive / method-chaining1.js
Last active February 13, 2021 18:20
Simple Method Chaining
class ChainAble {
firstMethod() {
console.log('This is the First Method');
return this;
}
secondMethod() {
console.log('This is the Second Method');
return this;
}
{
address: 'plot 1',
owner: 'segun',
type: 'duplex',
color: 'green',
rooms: '5',
kitchens: '1',
windows: 20
}
{
address: 'plot 1',
owner: 'segun',
type: 'duplex',
color: 'green',
rooms: '5',
kitchens: '1',
windows: 21
}
{
windows: 21
}
class ShapeShifter {
constructor(arg) {
this.message = '';
const argType = typeof arg;
const argTypeToTitleCase = `${argType[0].toUpperCase()}${argType.slice(1)}`;
const methodName = `handle${argTypeToTitleCase}`;
this[methodName]();
}
handleString() {