I would like to be able to concatenate strings by creating an object and passing down the constructor, so every time it is called it keeps the references of the previous one. I would like to achieve something like this:
foo = Chain("h")
bar = foo("e")("l")("l")("o")
foo.toString() == "h"
bar.toString() == "hello"
bar.ancestor.toString() == "hell"
What I have so far is a method chaining, when is rather similar but it is not quite what I want to accomplish here. I've been following the following documentation:
Inheritance and prototype chain
function ChainParent() {
}
function Chain(letter) {
this.letter = letter;
this.ancestor = this.letter.substring(0, this.letter.length - 1);
}
Chain.prototype = Object.create(ChainParent.prototype)
Chain.prototype.constructor = Chain;
Chain.prototype.create = function create(letter) {
const letra = this.letter.concat(letter);
return new this.constructor(letra)
}
Chain.prototype.toString = function() {
console.log(this.letter);
}
const foo = new Chain("h");
const bar = foo.create("e").create("l").create("l").create("o");
foo.toString();
bar.toString();
bar.ancestor.toString(); //This won't work