inheritfn
Prototypical inheritance for functions.
Enables you to wrap a function with another function without losing access to values defined on the original function.
inheritfn
implements copy-on-write behaviour similarly to regular
Object prototypical inheritance via Object.create
.
Example
var inheritfn = { return originalvalue} { return wrappervalue} originalvalue = 10 // wrapper inherits values from originalconsole // => 10console // => 10 // changing original.value changes wrapper.valueoriginalvalue = 20console // => 20console // => 20
copy on write!
inheritfn
implements copy-on-write behaviour similarly to regular
Object prototypical inheritance via Object.create
:
// write to wrapper.valuewrappervalue = 100 // wrapper.value becomes no longer linked to originalconsole // => 100 // original is unchangedconsole // => 20
- If a value on the original function changes, this is reflected on the new function.
- If you write to a key on the new function, access to the original function's value with the same key is shadowed by this new value.
- Unlike regular JavaScript prototypical inheritance, deleting a key
on the new function will not restore access to the original function's
key. You'll need to delete the key from
newFn.__proxy__
.
__proto__
To round out the prototypical inheritance 'simulation' a bit more.
wrapper__proto__ == original // => true
See Also
License
MIT