GithubHelp home page GithubHelp logo

olical / heir Goto Github PK

View Code? Open in Web Editor NEW
60.0 11.0 9.0 84 KB

Helper functions for prototypical inheritance in JavaScript

Home Page: https://www.npmjs.com/package/heir

License: The Unlicense

JavaScript 100.00%
inheritance classes javascript

heir's Introduction

Heir npm

Helper functions for prototypical inheritance in JavaScript.

Use the source code and JSDoc comments as documentation, here's a quick example to get you started.

const heir = require('heir')

// Create the base class.
function Base() {}

// Add a method.
Base.prototype.foo = function () {
  return 'Base#foo'
}

// Create a sub class which inherits from base.
function Sub() {}
heir.inherit(Sub, Base)

// Mix in some functionality enhancing objects.
heir.mixin(Sub, events)
heir.mixin(Sub, pooling)

// Change the original method.
Sub.prototype.foo = function () {
  return [
    'Sub#foo',
    Sub._super.foo.call(this)
  ].join(', ')
}

// Create an instance of Sub and call it's method.
const s = new Sub()
s.foo() // Returns "Sub#foo, Base#foo"

Unlicense

This project is given to you under the unlicense, as documented in the UNLICENSE file in this directory. Enjoy.

heir's People

Contributors

nathggns avatar olical avatar skrivle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

heir's Issues

Should I rewrite a lot of this, but better?

I built this almost a year ago, I've learnt a lot in that time. One of the main things related to this project is my knowledge about prototypical inheritance and how I think it should be done.

I've summed up how I think it should now be approached within this post: http://oli.me.uk/2013/06/01/prototypical-inheritance-done-right/

I would quite like to restructure a lot of this code so it still kept the old mixin style but also had a new method that actually extends the class. At the moment, if you extend something, it won't actually be an instance of the parent class.

var Parent = function(){}
var Child = function(){}.extend(Parent);

var a = new Parent();
var b = new Child();

a instanceof Child; // false, which is correct
a instanceof Parent; // true, also correct

b instanceof Child; // true, this is fine
b instanceof Parent; // false, but it should be true

If I added in my new extension method for extending and then kept the old code as a kind of mixin for multiple inheritance, I think this project would become a lot more useful.

At the moment it is not implemented how it should be. It's only like this because I did not know what I know now all those months ago when I wrote it.

I would like to restructure a lot of this but keep the same essence. It will do the same thing, but in a slightly different way. I'd ideally like to do this before firing it off into the world of package managers too.

I'd also like to remove the modification of Function.prototype, but I'm not sure about that one. It feels wrong.

Thoughts?

Bug/Error with the _super property in latest version

I found a bug when using the _super property inside a subclass. Please check the fiddle provided below
I used the same code as in your demo.

"Uncaught TypeError: Cannot read property 'foo' of undefined
at Sub.foo ((index)..."

I believe this error is due to this line :

destination._super = source.prototype

If changed to

destination.prototype._super = source.prototype

then it will work.

Version

v3.0.0

Test Case

https://jsfiddle.net/josefano09/w81xdg45/1/

Steps to reproduce

Use heir.inherit(Sub, Base); and then inside the Sub class try to call this._super. The above error will show in the console.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.