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: ( 1.74 KB minified and 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)
- Its one of the fastest inheritance libraries: method calls, instance creation
var Person = proto // prototype builder// constructorthislegs = legsthisarms = arms// instance methodthislegs -= 1return thisarms + thislegsObjectdefinePropertythis 'limbs' // getters (and setters, etc) can be set right on the prototype!return thisarms + thislegsvar Girl = protoPerson // inheritancereturn Person22var g = Girl22 // instantiationggetCaughtInBearTrapconsole.log"Girl has "+glimbs+" limbs"console.log": ("var newPerson = ghaveBabyconsole.log"New person has" +newPersonlimbs+" limbs : )"
npm install proto
// node.jsvar proto = require'proto'// amdrequireconfigpaths: proto: '../generatedBuilds/proto.umd.js'require'proto' /* your code */// 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 = protothis; // points to the prototype, so set methods and static properties on this// the name property has an impact on how proto classes are displayed in dev toolsthisname = 'MyProto'; // set a name for your proto class// constructorthis; // inside methods, 'this' references the instanceifv > 0thisx = v // you can normally access the object with this inside methodselse ifv !== undefinedreturn true // you can return non-object valueselsereturn protoundefined // return undefined by using a special constructor return valuethisanythingElse = 5 // static properties can be accessed by the class and the instance// getters and setters (enumerable makes it available statically too! Ie)ObjectdefinePropertythis 'moose'enumerable: truereturn 5console.log"just kidding, i'm not setting anything!"// private functions don't have access to the correct 'this', so pass it invarthatx = arg1 + etcprivateFnthis thisx 1Parentname; // 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 = protoParentsuperclassinitcallthis arguments // super-class method call// superclass.prototype.init.call(this, arguments) // remember that you probably need to access superclass.prototype for parents that aren't proto objectsthisr = 10// create static methods just like instance methods - you can access them from the constructorreturn thisconstructorx+12 // uses its own constructor to create a Child objectvar object = Child1 // instantiationobjectdoSomething // method call (as usual)var object2 = ChildstaticMethod1 // static method callChildparent === Parent; // the 'parent' property on the will point to the proto class's parent
Creating a custom Error object:
var CustomError = protoErrorthisname = 'CustomError'superclasscallthis msgforvar n in propertiesthisn = propertiesn
- 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)
- 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.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