HashTransaction

The concept of HashTransaction is explored through different JavaScript patterns, focusing on how to create and manage objects that represent transactions with hashing capabilities. This page will delve into the implementation of HashTransaction using both constructor functions and the OLOO (Objects Linked to Other Objects) pattern.

Constructor Functions Pattern

In JavaScript, constructor functions are a traditional way to create objects. The HashTransaction is built upon the Transaction constructor, inheriting its properties and methods. Here's how it is implemented:

[Listing 2.6: Configuring the Prototype Chain Using Constructor Functions](https://livebook.manning.com/book/the-joy-of-javascript/chapter-2/63)

function Transaction(sender, recipient) {  
    this.sender = sender;
    this.recipient = recipient;
}
Transaction.prototype.displayTransaction = function displayTransaction() {
    return `Transaction from ${this.sender} to ${this.recipient}`;
} 
 
function HashTransaction(sender, recipient) {
    if (!new.target) { 
        return new HashTransaction(sender, recipient);
    }
    Transaction.call(this, sender, recipient);      
}
 
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;
}
 
HashTransaction.prototype = Object.create(Transaction.prototype);    // Links prototypes
HashTransaction.prototype.constructor = HashTransaction;             // Sets constructor

const tx = new HashTransaction('luis@tjoj.com', 'luke@tjoj.com');
const tx2 = new HashTransaction('luis@tjoj.com', 'luke@tjoj.com');
Transaction.prototype.isPrototypeOf(tx); // true
tx.calculateHash === tx2.calculateHash;  // true
tx.displayTransaction === tx2.displayTransaction; // true
tx.__proto__.__proto__; 
// Transaction { displayTransaction: [Function: displayTransaction] }

Key Points

  • Prototype Chain: The prototype chain is configured to allow HashTransaction to inherit from Transaction. This setup ensures that methods like displayTransaction are accessible to HashTransaction instances.
  • Constructor Function: The HashTransaction constructor checks if it is called with new and corrects it if not, ensuring proper instantiation.
  • Method Sharing: Methods like calculateHash are shared across instances, avoiding duplication.

OLOO Pattern

The OLOO pattern provides an alternative to constructor functions by using Object.create to link objects. This pattern is more aligned with JavaScript's prototypal inheritance.

[Listing 3.10: OLOO Implemented with Object.assign](https://livebook.manning.com/book/the-joy-of-javascript/chapter-3/89)

const Transaction = { 
  init(sender, recipient, funds = 0.0) {    
    this.sender = sender;
    this.recipient = recipient;
    this.funds = Number(funds);
    return this;
  },
  displayTransaction() {
    return `Transaction from ${this.sender} to ${this.recipient} for   
       ${this.funds}`;
  }
}
 
const HashTransaction = Object.assign(
  Object.create(Transaction),
  {
      calculateHash() {
        const data = [this.sender, this.recipient, this.funds].join('');
        let hash = 0, i = 0;
        while (i < data.length) {
          hash = ((hash << 5) - hash + data.charCodeAt(i++)) << 0;
        }
        return hash**2; 
    }
  }
);

Key Points

  • Object Creation: HashTransaction is created using Object.create(Transaction), linking it to Transaction.
  • Property Assignment: Object.assign is used to add properties and methods to HashTransaction.
  • Encapsulation: The pattern encapsulates the transaction logic, making the code less fragile and more maintainable.

Additional Considerations

  • Encapsulation: The encapsulation of HashTransaction ensures that unnecessary details are hidden, promoting maintainability.
  • Prototype Configuration: Proper configuration of prototypes is crucial to ensure that methods are shared and not duplicated across instances.
  • Mixins: The use of mixins, as seen in the OLOO pattern, allows for flexible and reusable code components.

By understanding these patterns, developers can effectively implement HashTransaction objects that are both efficient and maintainable, leveraging JavaScript's prototypal inheritance and object creation capabilities.

FAQ (Frequently asked questions)