Skip to content

An easy way to properly use prototype inheritance and chaining.

License

Notifications You must be signed in to change notification settings

getsnoopy/inherit.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

inherit.js

inherit.js is an easy way to properly use prototype chaining in JavaScript. Typically, only 1-level prototype chains are used to keep things simple, and because multi-level prototype chains are hard to get right. This library makes it really easy to use any number of levels in the prototype chain, which helps keep code DRY.

Usage

inherit( childConstructor, parentConstructor )

Example

Code
function Grandfather() {}
Grandfather.prototype.getName = function () { return 'John Doe I'; };
function Father() {}
Father.prototype.getName = function () { return 'John Doe II'; };

inherit( Father, Grandfather );

function Child() {}
Child.prototype.getName = function () { return 'John Doe III'; };

inherit( Child, Father );

var john_iii = new Child();
Demo

ScreenShot

Compatibility

ECMAScript 3rd Edition and above. The script uses a polyfill for providing Object.create in older browsers.

Note: Since property descriptors cannot be set in versions before ECMAScript 5, the constructor property will be enumerable after a child has been extended.

About

An easy way to properly use prototype inheritance and chaining.

Resources

License

Stars

Watchers

Forks

Packages

No packages published