proto-inherit
v1.0.0
Published
A small utility for setting up prototype inheritance between JavaScript constructor functions, built with Vite.
Maintainers
Readme
proto-inherit
A small utility for setting up prototype inheritance between JavaScript constructor functions — no more manual Object.create() and constructor fixing.
Built with Vite.
📦 Installation
npm install proto-inherit🚀 Usage
import { inheritPrototype } from "proto-inherit";
// Parent constructor
function Parent(name) {
this.name = name;
}
Parent.prototype.sayHi = function () {
console.log(`Hi, I'm ${this.name}`);
};
// Child constructor
function Child(name, age) {
Parent.call(this, name); // Call parent constructor
this.age = age;
}
// Set up prototype inheritance
inheritPrototype(Child, Parent);
Child.prototype.sayAge = function () {
console.log(`I'm ${this.age} years old`);
};
const kid = new Child("Alice", 10);
kid.sayHi(); // Hi, I'm Alice
kid.sayAge(); // I'm 10 years old🛠 API
inheritPrototype(Child, Parent)
Sets up prototype inheritance between Child and Parent.
- Child — The child constructor function
- Parent — The parent constructor function
- Returns the
Childconstructor for chaining
What it does internally:
- Sets
Child.prototype = Object.create(Parent.prototype) - Restores
Child.prototype.constructor = Child - Adds
Child.superClass = Parentfor convenience
📜 License
MIT License © 2025 Prince Verma
