Code Issues Solver
Prototypal Inheritance Implementation in JavaScript
This project implements a prototypal inheritance structure in JavaScript with three classes: Activity, Payment, and Refund. Each class has defined methods for managing amounts and associated properties, demonstrating robust inheritance
Prompt
Answer
Prototypal Inheritance in JavaScript
Problem Analysis
The objective is to implement a prototypal inheritance structure in JavaScript involving three functions: Activity
, Payment
, and Refund
. Each of these functions has specific parameters and behaviors that need to be defined.
Classes Overview
Activity: Base class that initializes with an amount.
- Prototype Methods:
setAmount(value)
: Sets the amount if valid.getAmount()
: Returns the current amount.
- Prototype Methods:
Payment: Inherits from
Activity
, initializes withamount
andreceiver
.- Prototype Methods:
setReceiver(value)
: Sets the receiver's name.getReceiver()
: Returns the receiver's name.
- Prototype Methods:
Refund: Inherits from
Activity
, initializes withamount
andsender
.- Prototype Methods:
setSender(value)
: Sets the sender's name.getSender()
: Returns the sender's name.
- Prototype Methods:
Implementation Steps
- Define the
Activity
function and its methods. - Define
Payment
andRefund
functions, ensuring they inherit fromActivity
. - Add specific prototypes for
Payment
andRefund
. - Test the structure with sample inputs.
Complete Code Implementation
// Define the Activity constructor
function Activity(amount) {
this.amount = amount; // Initial amount assignment
}
// Add setAmount method to Activity's prototype
Activity.prototype.setAmount = function(value) {
if (value <= 0) {
return false; // Reject invalid amount
}
this.amount = value; // Set the valid amount
return true; // Indicate success
};
// Add getAmount method to Activity's prototype
Activity.prototype.getAmount = function() {
return this.amount; // Return current amount
};
// Define the Payment constructor
function Payment(amount, receiver) {
Activity.call(this, amount); // Call parent constructor
this.receiver = receiver; // Initialize receiver
}
// Inherit from Activity
Payment.prototype = Object.create(Activity.prototype);
Payment.prototype.constructor = Payment;
// Add setReceiver method to Payment's prototype
Payment.prototype.setReceiver = function(value) {
this.receiver = value; // Assign new receiver
};
// Add getReceiver method to Payment's prototype
Payment.prototype.getReceiver = function() {
return this.receiver; // Return current receiver
};
// Define the Refund constructor
function Refund(amount, sender) {
Activity.call(this, amount); // Call parent constructor
this.sender = sender; // Initialize sender
}
// Inherit from Activity
Refund.prototype = Object.create(Activity.prototype);
Refund.prototype.constructor = Refund;
// Add setSender method to Refund's prototype
Refund.prototype.setSender = function(value) {
this.sender = value; // Assign new sender
};
// Add getSender method to Refund's prototype
Refund.prototype.getSender = function() {
return this.sender; // Return current sender
};
// Example Usage
let payment = new Payment(5000, "John");
console.log(`Payment object created with amount ${payment.getAmount()} and receiver ${payment.getReceiver()}`);
let isAmountUpdated = payment.setAmount(4000);
console.log(isAmountUpdated ? `Amount updated to ${payment.getAmount()}` : 'Amount not updated');
payment.setReceiver("John B");
console.log(`Receiver updated to ${payment.getReceiver()}`);
// Check properties
console.log(`Payment.prototype has property setAmount: ${Payment.prototype.setAmount !== undefined}`);
console.log(`Payment.prototype has property getAmount: ${Payment.prototype.getAmount !== undefined}`);
console.log(`Payment.prototype has property setReceiver: ${Payment.prototype.setReceiver !== undefined}`);
console.log(`Payment.prototype has property getReceiver: ${Payment.prototype.getReceiver !== undefined}`);
let refund = new Refund(5000, "John");
console.log(`Refund object created with amount ${refund.getAmount()} and sender ${refund.getSender()}`);
isAmountUpdated = refund.setAmount(4000);
console.log(isAmountUpdated ? `Amount updated to ${refund.getAmount()}` : 'Amount not updated');
refund.setSender("John B");
console.log(`Sender updated to ${refund.getSender()}`);
// Check properties for Refund
console.log(`Refund.prototype has property setAmount: ${Refund.prototype.setAmount !== undefined}`);
console.log(`Refund.prototype has property getAmount: ${Refund.prototype.getAmount !== undefined}`);
console.log(`Refund.prototype has property setSender: ${Refund.prototype.setSender !== undefined}`);
console.log(`Refund.prototype has property getSender: ${Refund.prototype.getSender !== undefined}`);
Code Explanation
Activity Constructor:
- Initializes with an amount.
- Validates and sets a new amount through
setAmount
. - Retrieves the amount with
getAmount
.
Payment Constructor:
- Inherits from
Activity
. - Initializes with an additional
receiver
property. - Allows setting and getting of the receiver.
- Inherits from
Refund Constructor:
- Also inherits from
Activity
. - Initializes with a
sender
property. - Allows setting and getting of the sender.
- Also inherits from
Conclusion
This implementation successfully demonstrates prototypal inheritance in JavaScript and adheres to the principles outlined in the problem statement. The methods are succinct, and the structure is flexible for future extensions or modifications.
Description
This project implements a prototypal inheritance structure in JavaScript with three classes: Activity, Payment, and Refund. Each class has defined methods for managing amounts and associated properties, demonstrating robust inheritance and prototype method usage.