Why Use proto?
protoplays nice - you can use proto to inherit from any object, even if it was created with a different inheritance library!
- constructors are inheritable
- non-objects can be returned from a constructor (even
- easy access to an object's superclass
- you can give your classes dynamic names
- you don't hafta use the
- native objects work with
protoproperly* creates classes that inherit from native objects - even all the
constructorproperties are propertly set
Object.createso it should work with older browsers ( testers welcome! )
protois small: (833 bytes minified and gzipped in UMD format)
protois lightweight. It doesn't attempt to emulate class-based languages or create any fancy features you probably don't actually need (interfaces, abstract classes, etc)
- It has solid comparison when compared to the fastest inheritance libraries. The following two performance tests use specific methodology to improve their similarity to running code in the real applications:
var Person =var Girl =var g = // instantiationgconsoleconsolevar newPerson = gconsole
npm install proto
// node.jsvar proto =// amdrequire// global variable<script src="proto.umd.js"></script>proto; // proto.umd.js can define proto globally if you really// want to shun module-based design
var Parent =Parentname; // the name property can be accessed directly from the returned proto class object// you can inherit from any object!// the resulting object factory will generate instances inheriting from:// [if you inherit from]// [a function]: that function's prototype// [anything else]: that object itselfvar Child =var object = // instantiationobject // method call (as usual)var object2 = Child // static method callChildparent === Parent; // the 'parent' property on the will point to the proto class's parent
Creating a custom Error object:
var CustomError =
- Inheriting from
Errorand other exception types doesn't automatically set a correct
nameproperty, so you need to set it as a static properly "manually".
- Objects inheriting from
Stringcan't use the
- Inheriting from
- Inheriting from
RegExpdoesn't work either (the results can't use the
- You can't properly access any non-writable properties of a function from the returned proto-object factory though the properties will work correctly on instances. This includes:
- Some properties are read-only and so can't be reset on the prototype object. An example is
IE11 [ ]
IE10 [ ]
IE9 [ ]
IE8 [ ]
Opera [ ]
- Combine ProtoObjectFactory with the NamedFunction so you remove a function call there
- memoize whether the class has 'init' or not (to remove one branch from ProtoObjectFactory)
Consider creating a Proto2 that focuses on further performance improvements:
- Requires the use of 'new'
- Maybe sacrifices dynamic prototypes (that change after constructor creation) to get rid of the prototype chain walking (and just merges in functions)
How to Contribute!
- Creating issues (aka tickets/bugs/etc). Please feel free to use issues to report bugs, request features, and discuss changes
- Updating the documentation: ie this readme file. Be bold! Help create amazing documentation!
- Submitting pull requests.
How to submit pull requests:
- Please create an issue and get my input before spending too much time creating a feature. Work with me to ensure your feature or addition is optimal and fits with the purpose of the project.
- Fork the repository
- clone your forked repo onto your machine and run
npm installat its root
- If you're gonna work on multiple separate things, its best to create a separate branch for each of them
- If it's a code change, please add to the unit tests (at test/protoTest.js) to verify that your change
- When you're done, run the unit tests and ensure they all pass
- Commit and push your changes
- Submit a pull request: https://help.github.com/articles/creating-a-pull-request
- 1.0.18 - Setting the 'stack' property of custom errors as 'configurable' so you can change it if you want
- 1.0.17 - Correcting distribution
- 1.0.16 - optimizing instance creation - made it about 3 times as fast! Now its one of the fastest inheritance libraries!
- 1.0.15 - changing to using webpack to make UMD packages
- 1.0.14 - fixing the name property so if there is no name, 'undefined' doesn't become the functions name
- 1.0.13 - adding a 'parent' property on the returned proto class
- 1.0.12 - making the constructor's name property settable (via
this.namein the class construction function - the function passed to proto)
- 1.0.11 - adding the ability to access getters and setters correctly statically
- 1.0.10 - making the stack property a getter (like it is in native error objects)
- 1.0.8 - if a static property can't be written (because it's read only or for some other reason throws an exception when being set), it will now silently not set, instead of throwing an exception
- 1.0.7 - getting rid of useless line in stack trace
- 1.0.6 - fixing custom error name in stacktraces
- 1.0.5 - fixing github dependencies
Released under the MIT license: http://opensource.org/licenses/MIT