Comparing version 1.0.1 to 1.0.2
@@ -47,9 +47,11 @@ /** | ||
* Create a new object using Object.create. The arguments will be | ||
* passed to the new instances init method. | ||
* passed to the new instances init method or to a method name set in | ||
* __init. | ||
*/ | ||
create : function () | ||
{ | ||
var instance = Object.create(this); | ||
if (typeof instance.init == "function") { | ||
instance.init.apply(instance, arguments); | ||
var instance = Object.create(this), | ||
init = typeof instance.__init === 'string' ? instance.__init : 'init'; | ||
if (typeof instance[init] == "function") { | ||
instance[init].apply(instance, arguments); | ||
} | ||
@@ -78,3 +80,3 @@ return instance; | ||
// Check if we're overwriting an existing function | ||
self[name] = (typeof prop[name] == "function" && typeof _super[name] == "function" && fnTest.test(prop[name])) // | ||
self[name] = (typeof prop[name] == "function" && typeof _super[name] == "function" && fnTest.test(prop[name])) | ||
|| (typeof _old == "function" && typeof prop[name] == "function") ? // | ||
@@ -81,0 +83,0 @@ (function (old, name, fn) |
{ | ||
"name": "uberproto", | ||
"description": "JavaScript object inheritance sugar: Easy extension, mixins, super methods, proxies", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"homepage": "http://daffl.github.com/uberproto", | ||
@@ -6,0 +6,0 @@ "repository": { |
{ | ||
"title": "UberProto", | ||
"content_file": "readme.markdown", | ||
"content_file": "readme.md", | ||
"include_index": false, | ||
@@ -5,0 +5,0 @@ "header": "UberProto", |
@@ -1,2 +0,3 @@ | ||
(function(){typeof define=="undefined"&&(define=function(a){a=a();typeof exports=="undefined"?Proto=a:module.exports=a});define(function(){return{create:function(){var a=Object.create(this);typeof a.init=="function"&&a.init.apply(a,arguments);return a},mixin:function(a,d){var c=d||this,g=/xyz/.test(function(){})?/\b_super\b/:/.*/,f=Object.getPrototypeOf(c)||c.prototype,e,b;for(b in a)e=c[b],c[b]=typeof a[b]=="function"&&typeof f[b]=="function"&&g.test(a[b])||typeof e=="function"&&typeof a[b]=="function"? | ||
function(a,b,c){return function(){var d=this._super;this._super=typeof a=="function"?a:f[b];var e=c.apply(this,arguments);this._super=d;return e}}(e,b,a[b]):a[b];return c},extend:function(a,d){return this.mixin(a,Object.create(d||this))},proxy:function(a,d){var c=d||this;return function(){return c[a].apply(c,arguments)}}}})})(); | ||
(function(){typeof define=="undefined"&&(define=function(a){a=a();typeof exports=="undefined"?Proto=a:module.exports=a});if(!Object.create)Object.create=function(a){function b(){}if(arguments.length>1)throw Error("Object.create implementation only accepts the first parameter.");b.prototype=a;return new b};define(function(){return{create:function(){var a=Object.create(this),b=typeof a.__init==="string"?a.__init:"init";typeof a[b]=="function"&&a[b].apply(a,arguments);return a},mixin:function(a,b){var d= | ||
b||this,g=/xyz/.test(function(){})?/\b_super\b/:/.*/,f=Object.getPrototypeOf(d)||d.prototype,e,c;for(c in a)e=d[c],d[c]=typeof a[c]=="function"&&typeof f[c]=="function"&&g.test(a[c])||typeof e=="function"&&typeof a[c]=="function"?function(a,b,c){return function(){var d=this._super;this._super=typeof a=="function"?a:f[b];var e=c.apply(this,arguments);this._super=d;return e}}(e,c,a[c]):a[c];return d},extend:function(a,b){return this.mixin(a,Object.create(b||this))},proxy:function(a,b){var d=b||this; | ||
return function(){return d[a].apply(d,arguments)}}}})})(); |
@@ -12,4 +12,4 @@ Uberproto is a simple base object that adds some sugar to ECMAScript 5 style object inheritance | ||
With a small footprint (0.8Kb minified and 0.4Kb compressed) and an easy to handle | ||
API of just four methods it also doesn't add a lot of baggage to your JavaScript application. | ||
With a small footprint (about 1Kb minified) and an easy to handle API of just | ||
four methods it also doesn't add a lot of baggage to your JavaScript application. | ||
@@ -33,2 +33,5 @@ ## Usage | ||
[Download proto.min.js](https://raw.github.com/daffl/uberproto/master/proto.min.js) | ||
(1Kb minified) and include it as a script: | ||
<script type="text/javascript" src="proto.min.js"></script> | ||
@@ -81,5 +84,8 @@ | ||
Play around with the examples in [this JSFiddle](http://jsfiddle.net/Daff/2GB8n/1/). | ||
### Initialize | ||
You can create a new instance by calling *create*: | ||
You can create a new instance by calling *create*. This will create a new object and call the *init* method, | ||
if defined: | ||
@@ -90,2 +96,13 @@ var dave = Person.create('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 = Proto.extend({ | ||
__init : 'construct', | ||
construct : function(name) { | ||
this.name = name; | ||
} | ||
}; | ||
For calling the constructor on a plain object, call *create* on an UberProto object: | ||
@@ -101,3 +118,3 @@ | ||
In each method `this.\_super` refers to the method being overwritten, if there is one. | ||
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: | ||
@@ -140,3 +157,3 @@ | ||
Mixins add functionality to an existing object. Mixins can also access their super methods using `this.\_super`. | ||
Mixins add functionality to an existing object. Mixins can also access their super methods using `this._super`. | ||
This will either refer the overwritten method on the object itself or the one on the prototype: | ||
@@ -197,1 +214,15 @@ | ||
var cb = Proto.proxy('fullName', PersonObject); | ||
## Changelog | ||
__1.0.2__ | ||
* Added `__init` property to allow constructor functions to be named other than *init*. Fixes issue [#1](https://github.com/daffl/uberproto/pull/1) | ||
__1.0.1__ | ||
* API now usable with plain objects like `Proto.mixin({}, PlainObject)` | ||
__1.0.0__ | ||
* Initial stable release |
@@ -19,2 +19,3 @@ if(typeof Proto == "undefined") { | ||
}, | ||
create : function(test) { | ||
@@ -44,2 +45,22 @@ test.expect(5); | ||
initAlias : function(test) { | ||
test.expect(2); | ||
var Obj = Proto.extend({ | ||
__init : 'myConstructor', | ||
myConstructor : function(arg) { | ||
test.equal(arg, 'myConstructor', 'Got proper arguments in myConstructor'); | ||
} | ||
}), | ||
OtherObj = { | ||
__init : 'testConstructor', | ||
testConstructor : function(arg) { | ||
test.equal(arg, 'testConstructor', 'Got proper arguments in myConstructor'); | ||
} | ||
} | ||
Obj.create('myConstructor'); | ||
Proto.create.call(OtherObj, 'testConstructor'); | ||
test.done(); | ||
}, | ||
_super : function(test) { | ||
@@ -46,0 +67,0 @@ test.expect(3); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
225478
2326
222