Comparing version 3.0.0 to 4.0.0
140
heir.js
/** | ||
* Heir v3.0.0 - http://git.io/F87mKg | ||
* Oliver Caldwell - http://oli.me.uk/ | ||
* Heir v4.0.0 - http://git.io/F87mKg | ||
* Oliver Caldwell - https://oli.me.uk/ | ||
* Unlicense - http://unlicense.org/ | ||
*/ | ||
(function (name, root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
define(factory); | ||
const heir = { | ||
/** | ||
* Causes your desired class to inherit from a source class. This uses | ||
* prototypical inheritance so you can override methods without ruining | ||
* the parent class. | ||
* | ||
* This will alter the actual destination class though, it does not | ||
* create a new class. | ||
* | ||
* @param {Function} destination The target class for the inheritance. | ||
* @param {Function} source Class to inherit from. | ||
* @param {Boolean} addSuper Should we add the _super property to the destination object? Defaults to true. | ||
*/ | ||
inherit: function inherit (destination, source, addSuper) { | ||
const proto = destination.prototype = Object.create(source.prototype) | ||
proto.constructor = destination | ||
if (addSuper || typeof addSuper === 'undefined') { | ||
destination._super = source.prototype | ||
} | ||
else if (typeof exports === 'object') { | ||
module.exports = factory(); | ||
} | ||
else { | ||
root[name] = factory(); | ||
} | ||
}('heir', this, function () { | ||
/*global define,module*/ | ||
'use strict'; | ||
}, | ||
var heir = { | ||
/** | ||
* Causes your desired class to inherit from a source class. This uses | ||
* prototypical inheritance so you can override methods without ruining | ||
* the parent class. | ||
* | ||
* This will alter the actual destination class though, it does not | ||
* create a new class. | ||
* | ||
* @param {Function} destination The target class for the inheritance. | ||
* @param {Function} source Class to inherit from. | ||
* @param {Boolean} addSuper Should we add the _super property to the prototype? Defaults to true. | ||
*/ | ||
inherit: function inherit(destination, source, addSuper) { | ||
var proto = destination.prototype = heir.createObject(source.prototype); | ||
proto.constructor = destination; | ||
/** | ||
* Mixes the specified object into your class. This can be used to add | ||
* certain capabilities and helper methods to a class that is already | ||
* inheriting from some other class. You can mix in as many object as | ||
* you want, but only inherit from one. | ||
* | ||
* These values are mixed into the actual prototype object of your | ||
* class, they are not added to the prototype chain like inherit. | ||
* | ||
* @param {Function} destination Class to mix the object into. | ||
* @param {Object} source Object to mix into the class. | ||
*/ | ||
mixin: function mixin (destination, source) { | ||
let key | ||
if (addSuper || typeof addSuper === 'undefined') { | ||
destination._super = source.prototype; | ||
} | ||
}, | ||
for (key in source) { | ||
if (source.hasOwnProperty(key)) { | ||
destination.prototype[key] = source[key] | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Creates a new object with the source object nestled within its | ||
* prototype chain. | ||
* | ||
* @param {Object} source Method to insert into the new object's prototype. | ||
* @return {Object} An empty object with the source object in it's prototype chain. | ||
*/ | ||
createObject: Object.create || function createObject(source) { | ||
var Host = function () {}; | ||
Host.prototype = source; | ||
return new Host(); | ||
}, | ||
/** | ||
* Mixes the specified object into your class. This can be used to add | ||
* certain capabilities and helper methods to a class that is already | ||
* inheriting from some other class. You can mix in as many object as | ||
* you want, but only inherit from one. | ||
* | ||
* These values are mixed into the actual prototype object of your | ||
* class, they are not added to the prototype chain like inherit. | ||
* | ||
* @param {Function} destination Class to mix the object into. | ||
* @param {Object} source Object to mix into the class. | ||
*/ | ||
mixin: function mixin(destination, source) { | ||
return heir.merge(destination.prototype, source); | ||
}, | ||
/** | ||
* Merges one object into another, change the object in place. | ||
* | ||
* @param {Object} destination The destination for the merge. | ||
* @param {Object} source The source of the properties to merge. | ||
*/ | ||
merge: function merge(destination, source) { | ||
var key; | ||
for (key in source) { | ||
if (heir.hasOwn(source, key)) { | ||
destination[key] = source[key]; | ||
} | ||
} | ||
}, | ||
/** | ||
* Shortcut for `Object.prototype.hasOwnProperty`. | ||
* | ||
* Uses `Object.prototype.hasOwnPropety` rather than | ||
* `object.hasOwnProperty` as it could be overwritten. | ||
* | ||
* @param {Object} object The object to check | ||
* @param {String} key The key to check for. | ||
* @return {Boolean} Does object have key as an own propety? | ||
*/ | ||
hasOwn: function hasOwn(object, key) { | ||
return Object.prototype.hasOwnProperty.call(object, key); | ||
} | ||
}; | ||
return heir; | ||
})); | ||
module.exports = heir |
{ | ||
"name": "heir", | ||
"version": "3.0.0", | ||
"description": "Makes prototypical inheritance easy and robust", | ||
"license": "Unlicense", | ||
"main": "heir.js", | ||
"readmeFilename": "README.md", | ||
"bugs": { | ||
"url": "https://github.com/Wolfy87/Heir/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/Wolfy87/Heir.git" | ||
}, | ||
"directories": { | ||
"test": "tests" | ||
}, | ||
"keywords": [ | ||
"prototype", | ||
"inheritance", | ||
"class", | ||
"extend", | ||
"inherit", | ||
"oop" | ||
], | ||
"author": { | ||
"name": "Oliver Caldwell", | ||
"email": "olliec87@gmail.com", | ||
"url": "http://oli.me.uk/" | ||
} | ||
"name": "heir", | ||
"version": "4.0.0", | ||
"description": "Helper functions for prototypical inheritance", | ||
"license": "Unlicense", | ||
"main": "heir.js", | ||
"scripts": { | ||
"prepublish": "npm run lint && npm run test", | ||
"lint": "standard", | ||
"test": "tape tests.js | faucet" | ||
}, | ||
"readmeFilename": "README.md", | ||
"bugs": { | ||
"url": "https://github.com/Olical/Heir/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/Olical/Heir.git" | ||
}, | ||
"directories": { | ||
"test": "tests" | ||
}, | ||
"keywords": [ | ||
"prototype", | ||
"inheritance", | ||
"class", | ||
"extend", | ||
"inherit", | ||
"oop" | ||
], | ||
"author": { | ||
"name": "Oliver Caldwell", | ||
"email": "olliec87@gmail.com", | ||
"url": "https://oli.me.uk/" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"faucet": "^0.0.1", | ||
"standard": "^11.0.1", | ||
"tape": "^4.9.0" | ||
} | ||
} |
@@ -1,8 +0,10 @@ | ||
# Heir | ||
# Heir [![npm](https://img.shields.io/npm/v/heir.svg)](https://www.npmjs.com/package/heir) | ||
This is a small script that adds the ability to inherit other functions prototype objects into your own. It makes prototypical inheritance easy and robust. | ||
Helper functions for prototypical inheritance in JavaScript. | ||
Due to there not being any documentation yet, you may want to read my JSDoc comments in the source. They will tell you everything you need to know. Here is a quick example to get you going anyway. | ||
Use the source code and JSDoc comments as documentation, here's a quick example to get you started. | ||
```javascript | ||
const heir = require('heir') | ||
// Create the base class. | ||
@@ -13,64 +15,26 @@ function Base() {} | ||
Base.prototype.foo = function () { | ||
return 'Base#foo'; | ||
}; | ||
return 'Base#foo' | ||
} | ||
// Create a sub class which inherits from base. | ||
function Sub() {} | ||
heir.inherit(Sub, Base); | ||
heir.inherit(Sub, Base) | ||
// Mix in some functionality enhancing objects. | ||
heir.mixin(Sub, events); | ||
heir.mixin(Sub, pooling); | ||
heir.mixin(Sub, events) | ||
heir.mixin(Sub, pooling) | ||
// Change the original method. | ||
Sub.prototype.foo = function () { | ||
return [ | ||
'Sub#foo', | ||
this._super.foo.call(this) | ||
].join(', '); | ||
}; | ||
return [ | ||
'Sub#foo', | ||
Sub._super.foo.call(this) | ||
].join(', ') | ||
} | ||
// Create an instance of Sub and call it's method. | ||
var s = new Sub(); | ||
s.foo(); // Returns "Sub#foo, Base#foo" | ||
const s = new Sub() | ||
s.foo() // Returns "Sub#foo, Base#foo" | ||
``` | ||
You can load this script into your browser using a normal script tag or AMD. You can also use node.js' `require` if you are running server side. | ||
## Changes from v1 | ||
The `inherit` method used to work by cloning and merging multiple prototypes into one. This meant things like `instanceof` didn't work and you could get into some weird scenarios [caused by multiple inheritance][mi]. | ||
The new `inherit` uses the built in prototypical inheritance to provide a much cleaner outcome, as shown in [this post about prototypical inheritance][pi]. The major change is that you can't inherit from multiple classes any more. | ||
If you still need to have multiple things shared between classes to avoid duplication, you can now use the `mixin` method to merge objects into your inheritance hierarchies where required. | ||
## Changes from v2 | ||
The concept of `_super` now works better, but in a completely different way. Thanks to @vejersele in issue #9. | ||
## Downloading | ||
You can obtain a copy by cloning this repository with git, installing through [npm][] or [bower][]. Heir is called `heir` within both package managers. | ||
If you wish to run the tests you will also need to install Jasmine. You can do this through bower like so. | ||
```bash | ||
bower install --dev | ||
``` | ||
## Testing | ||
Tests are performed using Jasmine in the following browsers. | ||
* Firefox | ||
* Chrome | ||
* Opera | ||
* Safari | ||
* IE6+ | ||
When testing in the more modern browsers, not Internet Explorer basically, I run it through the very early versions, some midrange versions and the very latest ones too. I don't just do the latest version. | ||
Heir will always be tested and working perfectly in all of them before a release. I will not release anything I think is riddled with bugs. However, if you do spot one, please [submit it as an issue][issues] and I will get right on it. | ||
## Unlicense | ||
@@ -80,7 +44,2 @@ | ||
[npm]: https://npmjs.org/ | ||
[bower]: http://bower.io/ | ||
[issues]: https://github.com/Wolfy87/Heir/issues | ||
[mi]: http://stackoverflow.com/questions/225929/what-is-the-exact-problem-with-multiple-inheritance | ||
[pi]: http://oli.me.uk/2013/06/01/prototypical-inheritance-done-right/ | ||
[unlicense]: http://unlicense.org/ | ||
[unlicense]: http://unlicense.org/ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
58324
3
7
122
44
2