| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
A comprehensive implementation demonstrating classical prototypal inheritance patterns in JavaScript 🚀
Master the art of JavaScript prototypal inheritance through a real-world financial transaction system. This project showcases how to build robust inheritance hierarchies using constructor functions, prototype chains, and the classic inheritance pattern.
This project implements a three-tier inheritance hierarchy for managing financial activities:
Activity (Parent)
/ \
/ \
Payment Refund
(Child) (Child)
What makes this special?
Node.js >= 12.0.0Clone the repository:
git clone https://github.com/yourusername/javascript-activity-list.git
cd javascript-activity-listRun the application:
node activityList.js < test-cases/payment-example.txtRun tests:
npm testThis project uses classical prototypal inheritance pattern:
// 1. Parent Constructor
function Activity(amount) {
this.amount = amount;
}
// 2. Parent Methods on Prototype
Activity.prototype.setAmount = function(value) { ... }
Activity.prototype.getAmount = function() { ... }
// 3. Child Constructor
function Payment(amount, receiver) {
Activity.call(this, amount); // Call parent constructor
this.receiver = receiver;
}
// 4. Set up Inheritance Chain
Payment.prototype = Object.create(Activity.prototype);
Payment.prototype.constructor = Payment;
// 5. Add Child-Specific Methods
Payment.prototype.setReceiver = function(receiver) { ... }paymentInstance
↓
Payment.prototype (has setReceiver, getReceiver)
↓
Activity.prototype (has setAmount, getAmount)
↓
Object.prototype
↓
null
new Activity(amount)setAmount(value)
const activity = new Activity(100);
activity.setAmount(200); // Returns true
activity.setAmount(-50); // Returns falsegetAmount()
const activity = new Activity(100);
console.log(activity.getAmount()); // 100new Payment(amount, receiver)setReceiver(receiver)
const payment = new Payment(500, "John Doe");
payment.setReceiver("Jane Smith");getReceiver()
const payment = new Payment(500, "John Doe");
console.log(payment.getReceiver()); // "John Doe"new Refund(amount, sender)setSender(sender)
const refund = new Refund(300, "Amazon");
refund.setSender("PayPal");getSender()
const refund = new Refund(300, "Amazon");
console.log(refund.getSender()); // "Amazon"// Create a payment of $5000 to John
const payment = new Payment(5000, "John");
console.log(payment.getAmount()); // 5000
console.log(payment.getReceiver()); // "John"
// Update the amount
payment.setAmount(4000); // Returns true
console.log(payment.getAmount()); // 4000
// Update the receiver
payment.setReceiver("John B");
console.log(payment.getReceiver()); // "John B"
// Try to set invalid amount
payment.setAmount(-100); // Returns false
console.log(payment.getAmount()); // Still 4000// Create a refund of $3000 from Amazon
const refund = new Refund(3000, "Amazon");
console.log(refund.getAmount()); // 3000
console.log(refund.getSender()); // "Amazon"
// Update the amount
refund.setAmount(2500); // Returns true
// Update the sender
refund.setSender("Amazon Prime");
console.log(refund.getSender()); // "Amazon Prime"const payment = new Payment(1000, "Vendor");
// Check which methods are own vs inherited
console.log(Payment.prototype.hasOwnProperty('setAmount')); // false (inherited)
console.log(Payment.prototype.hasOwnProperty('getAmount')); // false (inherited)
console.log(Payment.prototype.hasOwnProperty('setReceiver')); // true (own)
console.log(Payment.prototype.hasOwnProperty('getReceiver')); // true (own)
// Verify inheritance chain
console.log(payment instanceof Payment); // true
console.log(payment instanceof Activity); // true
console.log(payment instanceof Object); // trueThis project demonstrates core JavaScript concepts:
These patterns are used in:
Perfect for:
npm testPayment Test:
node activityList.js < test-cases/payment-example.txtRefund Test:
node activityList.js < test-cases/refund-example.txtEdge Cases:
node activityList.js < test-cases/invalid-amount.txt
node activityList.js < test-cases/zero-amount.txtPayment Example:
Payment object created with amount 5000 and receiver John Amount updated to 4000 Receiver updated to John B Payment object details - amount is 4000 and receiver is John B Payment.prototype has property setAmount: false Payment.prototype has property getAmount: false Payment.prototype has property setReceiver: true Payment.prototype has property getReceiver: true
javascript-activity-list/
├── activityList.js # Main implementation
├── test.js # Unit tests
├── package.json # npm configuration
├── README.md # This file
├── LICENSE # MIT License
├── CONTRIBUTING.md # Contribution guidelines
├── .gitignore # Git ignore rules
└── test-cases/ # Test input files
├── payment-example.txt
├── refund-example.txt
├── invalid-amount.txt
└── zero-amount.txt
We welcome contributions! Here's how you can help:
See CONTRIBUTING.md for detailed guidelines.
This project is licensed under the MIT License - see the LICENSE file for details.
| Back | FazBrowse Home | New Git URL |