Comparing version 0.0.9 to 1.0.0
116
lib/proto.js
/** | ||
* A base object for ECMAScript 5 style prototypal inheritance. | ||
* | ||
* | ||
* @see https://github.com/rauschma/proto-js/ | ||
@@ -8,7 +8,9 @@ * @see http://ejohn.org/blog/simple-javascript-inheritance/ | ||
*/ | ||
(function() { | ||
if(typeof define == "undefined") { | ||
define = function(fn) { | ||
(function () | ||
{ | ||
if (typeof define == "undefined") { | ||
define = function (fn) | ||
{ | ||
var res = fn(); | ||
if(typeof exports == "undefined") { | ||
if (typeof exports == "undefined") { | ||
Proto = res; | ||
@@ -19,9 +21,16 @@ } else { | ||
} | ||
}; | ||
define(function() { | ||
} | ||
; | ||
define(function () | ||
{ | ||
return { | ||
create : function() { | ||
/** | ||
* Create a new object using Object.create. The arguments will be | ||
* passed to the new instances init method. | ||
*/ | ||
create : function () | ||
{ | ||
var instance = Object.create(this); | ||
if(typeof instance.init == "function") { | ||
if (typeof instance.init == "function") { | ||
instance.init.apply(instance, arguments); | ||
@@ -31,42 +40,65 @@ } | ||
}, | ||
mixin : function(prop) { | ||
var fnTest = /xyz/.test(function() { xyz; | ||
}) ? /\b_super\b/ : /.*/, _super = Object.getPrototypeOf(this) || this.prototype, _old; | ||
/** | ||
* Mixin a given set of properties | ||
* @param prop | ||
* @param obj | ||
*/ | ||
mixin : function (prop, obj) | ||
{ | ||
var self = obj || this, | ||
fnTest = /xyz/.test(function () | ||
{ | ||
xyz; | ||
}) ? /\b_super\b/ : /.*/, | ||
_super = Object.getPrototypeOf(self) || self.prototype, | ||
_old; | ||
// Copy the properties over | ||
for(var name in prop) { | ||
for (var name in prop) { | ||
// store the old function which would be overwritten | ||
_old = this[name]; | ||
_old = self[name]; | ||
// Check if we're overwriting an existing function | ||
this[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") ? // | ||
(function(old, name, fn) { | ||
return function() { | ||
var tmp = this._super; | ||
// Add a new ._super() method that is the same method | ||
// but either pointing to the prototype method | ||
// or to the overwritten method | ||
this._super = (typeof old == 'function') ? old : _super[name]; | ||
// The method only need to be bound temporarily, so we | ||
// remove it when we're done executing | ||
var ret = fn.apply(this, arguments); | ||
this._super = tmp; | ||
return ret; | ||
}; | ||
})(_old, name, prop[name]) : prop[name]; | ||
(function (old, name, fn) | ||
{ | ||
return function () | ||
{ | ||
var tmp = this._super; | ||
// Add a new ._super() method that is the same method | ||
// but either pointing to the prototype method | ||
// or to the overwritten method | ||
this._super = (typeof old == 'function') ? old : _super[name]; | ||
// The method only need to be bound temporarily, so we | ||
// remove it when we're done executing | ||
var ret = fn.apply(this, arguments); | ||
this._super = tmp; | ||
return ret; | ||
}; | ||
})(_old, name, prop[name]) : prop[name]; | ||
} | ||
return this; | ||
return self; | ||
}, | ||
proxy : function(name) { | ||
var self = this; | ||
return function() { | ||
/** | ||
* Extend the current or a given object with the given property | ||
* and return the extended object. | ||
* @param prop | ||
* @param obj | ||
* @returns The extended object | ||
*/ | ||
extend : function (prop, obj) | ||
{ | ||
return this.mixin(prop, Object.create(obj || this)); | ||
}, | ||
proxy : function (name, context) | ||
{ | ||
var self = context || this; | ||
return function () | ||
{ | ||
return self[name].apply(self, arguments); | ||
} | ||
}, | ||
extend : function(prop) { | ||
return Object.create(this).mixin(prop); | ||
} | ||
@@ -73,0 +105,0 @@ }; |
{ | ||
"name": "uberproto", | ||
"description": "JavaScript object inheritance sugar: Easy extension, mixins, super methods, proxies", | ||
"version": "0.0.9", | ||
"version": "1.0.0", | ||
"homepage": "http://daffl.github.com/uberproto", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -1,2 +0,2 @@ | ||
(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){var c=/xyz/.test(function(){})?/\b_super\b/:/.*/,e=Object.getPrototypeOf(this)||this.prototype,d,b;for(b in a)d=this[b],this[b]=typeof a[b]=="function"&&typeof e[b]=="function"&&c.test(a[b])||typeof d=="function"&&typeof a[b]=="function"? | ||
function(a,b,c){return function(){var d=this._super;this._super=typeof a=="function"?a:e[b];var f=c.apply(this,arguments);this._super=d;return f}}(d,b,a[b]):a[b];return this},proxy:function(a){var c=this;return function(){return c[a].apply(c,arguments)}},extend:function(a){return Object.create(this).mixin(a)}}})})(); | ||
(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)}}}})})(); |
@@ -61,5 +61,32 @@ if(typeof Proto == "undefined") { | ||
test.equal(inst.name, 'Tester', 'Name set in prototype'); | ||
test.done(); | ||
}, | ||
extendObject : function(test) | ||
{ | ||
test.expect(3); | ||
var Obj = { | ||
test : function(name) | ||
{ | ||
test.ok(true, 'Super test method called'); | ||
this.name = name; | ||
} | ||
}; | ||
var Extended = Proto.extend({ | ||
test : function() { | ||
this._super.apply(this, arguments); | ||
test.ok(true, 'Sub init called'); | ||
} | ||
}, Obj); | ||
Extended.test('Tester'); | ||
test.equal(Extended.name, 'Tester', 'Name set in prototype'); | ||
test.done(); | ||
}, | ||
mixin : function(test) { | ||
@@ -96,2 +123,28 @@ test.expect(3); | ||
mixinObject : function(test) | ||
{ | ||
test.expect(3); | ||
var Obj = { | ||
test : function(name) | ||
{ | ||
test.ok(true, 'Super test method called'); | ||
this.name = name; | ||
} | ||
}; | ||
Proto.mixin({ | ||
test : function() { | ||
this._super.apply(this, arguments); | ||
test.ok(true, 'Sub init called'); | ||
} | ||
}, Obj); | ||
Obj.test('Tester'); | ||
test.equal(Obj.name, 'Tester', 'Name set in prototype'); | ||
test.done(); | ||
}, | ||
proxy : function(test) { | ||
@@ -98,0 +151,0 @@ test.expect(1); |
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
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
218462
17
2282
1
213