Public
Edited
Mar 13, 2023
Insert cell
Insert cell
function Transaction(sender, recipient) {
this.sender = sender;
this.recipient = recipient;
}
Insert cell
Transaction.prototype.displayTransaction = function displayTransaction() {
return `Transaction from ${this.sender} to ${this.recipient}`;
}
Insert cell
function HashTransaction(sender, recipient) {
if (!new.target) {
return new HashTransaction(sender, recipient);
}
Transaction.call(this, sender, recipient);
}
Insert cell
//t = new HashTransaction('luis@tjoj.com', 'luke@tjoj.com');

Insert cell
//t.__proto__
Insert cell
HashTransaction.prototype.calculateHash = function calculateHash() {
const data = [this.sender, this.recipient].join('');
let hash = 0, i = 0;
while (i < data.length) {
hash = ((hash << 5) - hash + data.charCodeAt(i++)) << 0;
}
return hash**2;
}
Insert cell
//t2 = new HashTransaction('luis@tjoj.com', 'luke@tjoj.com');

Insert cell
//t2.__proto__
Insert cell
HashTransaction.prototype = Object.create(Transaction.prototype);
Insert cell
HashTransaction.prototype.constructor = HashTransaction;
Insert cell
tx = new HashTransaction('luis@tjoj.com', 'luke12@tjoj.com');

Insert cell
tx.__proto__

Insert cell
tx2 = new HashTransaction('luis@tjoj.com', 'luke12@tjoj.com');
Insert cell
Transaction.prototype.isPrototypeOf(tx);
Insert cell
tx.calculateHash === tx2.calculateHash; // true

Insert cell
tx.displayTransaction === tx2.displayTransaction; // true
Insert cell
tx.__proto__;
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more