Here is what it can do in a nutshell:
UberProto can be used as a CommonJS AMD module (e.g. with RequireJS), NodeJS or directly in the browser. If no module loader is available, the global variable Proto will be defined after you include the script. In the browser you have two options: The default build that includes EcmaScript 5 shims or, if you only support modern browsers or provide the shims already, without.
Make sure proto.js is in the right folder and then just define a module like this:
define'proto'// Source goes here;
Now Proto is available as a global vairable.
After installing the package using NPM
npm install uberproto
just require it like any other module:
var Proto = require'uberproto';
You can extend any UberProto object by using extend to create a new object that inherits from the current one. Internally Object.create is being used (the library provides a polyfill for browsers that don't support Object.create) and the prototype is set to the object that you are extending. If defined, the init method will be used as the constructor. That way you can define a simple Person object (which will be reused throughout the next paragraphs):
var Person = Protoextendthisname = name;return thisname;;
You can also define a plain object and pass it to UberProto object methods:
var PersonObject =thisname = name;return thisname;;
Play around with the examples in this JSFiddle.
You can create a new instance by calling create. This will create a new object and call the init method, if defined:
var dave = Personcreate'Dave';console.logdavename; // -> 'Dave'console.logdavefullName; // -> 'Dave'
If you are using init already for something else you can also set the __init property to the method name of your intialization method:
var MyPerson = Protoextend__init : 'construct'thisname = name;;
For calling the constructor on a plain object, call create on an UberProto object:
var john = ProtocreatecallPersonObject 'John';console.logjohnfullName; // -> 'John'
Overwriting create is great if you want to customize the way new objects are being instantiated.
In each method
this._super refers to the method being overwritten, if there is one.
For our Person object, for example, it would be a lot better if it also had a last name:
var BetterPerson = Personextend// If you want to pass all original arguments to the// _super method just use apply:// this._super.apply(this, arguments);this_supername;thislastname = lastname;return this_super + ' ' + thislastname;;var dave = BetterPersoncreate'Dave' 'Doe';console.logdavename; // -> 'Dave'console.logdavelastname; // -> 'Doe'console.logdavefullName; // -> 'Dave Doe'
You can also extend a plain object if you don't want to inherit from an UberProto object:
var BetterPersonObject = Protoextendthis_supername;thislastname = lastname;return this_super + ' ' + thislastname;PersonObject; // Pass the plain object as the second parameter
Mixins add functionality to an existing object. Mixins can also access their super methods using
This will either refer the overwritten method on the object itself or the one on the prototype:
Personmixinthis_superapplythis arguments;thiscan_sing = true;return 'Laaaa';;var dude = Personcreate'Dude';console.logdudesing; // -> 'Laaaa'console.logdudecan_sing; // -> true
Actual instances can be mixed in just the same:
var operaSinger = Personcreate'Pavarotti';operaSingermixinreturn this_super + ' Laalaaa!';;console.logoperaSingersing; // -> 'Laaaa Laalaaa!'
And you can also mix into plain objects e.g. overwriting the constructor of PersonObject:
Protomixinreturn 'My name is: ' + this_super;PersonObject;// Create a plain object without calling the constructorvar instance = ObjectcreatePersonObject;instancename = 'Dude';console.loginstancefullName; // 'My name is: Dude'
You can create proxy callbacks, that make sure that this will always point to the right object:
var callback = operaSingerproxy'fullName';console.logcallback; // -> 'Pavarotti'
And you can partially apply function arguments:
operaSingermixinreturn this_super + ' ' + text;;var singHello = operaSingerproxy'sing' 'Helloooooo!';singHello // Laaaa Laalaaa! Helloooooo!
proxy only works on objects extended from UberProto.
bower install uberproto
__initproperty to allow constructor functions to be named other than init. Fixes issue #1
Copyright (C) 2013 David Luecke firstname.lastname@example.org
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.