Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformatted proto.js to fit UMD spec #9

Merged
merged 1 commit into from Jun 14, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
168 changes: 80 additions & 88 deletions lib/proto.js
@@ -1,102 +1,94 @@
/* global define, exports, module, window */

/**
* A base object for ECMAScript 5 style prototypal inheritance.
*
* @see https://github.com/rauschma/proto-js/
* @see http://ejohn.org/blog/simple-javascript-inheritance/
* @see http://uxebu.com/blog/2011/02/23/object-based-inheritance-for-ecmascript-5/
*/
(function () {
/**
* AMD module shim
* @param {Function} fn The callback function
*/
var def = (typeof define === "undefined") ? function (fn) {
var res = fn();
if (typeof exports === "undefined" && typeof window !== "undefined") {
window.Proto = res;
} else {
module.exports = res;
}
} : define;

def(function () {
return {
/**
* Create a new object using Object.create. The arguments will be
* passed to the new instances init method or to a method name set in
* __init.
*/
create: function () {
var instance = Object.create(this),
init = typeof instance.__init === 'string' ? instance.__init : 'init';
if (typeof instance[init] === "function") {
instance[init].apply(instance, arguments);
}
return instance;
},
/**
* Mixin a given set of properties
* @param prop The properties to mix in
* @param obj [optional] The object to add the mixin
*/
mixin: function (prop, obj) {
var self = obj || this,
fnTest = /\b_super\b/,
_super = Object.getPrototypeOf(self) || self.prototype,
_old;
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require());
} else {
root.Proto = factory();
}
}(this, function () {
return {
/**
* Create a new object using Object.create. The arguments will be
* passed to the new instances init method or to a method name set in
* __init.
*/
create: function () {
var instance = Object.create(this),
init = typeof instance.__init === 'string' ? instance.__init : 'init';
if (typeof instance[init] === "function") {
instance[init].apply(instance, arguments);
}
return instance;
},
/**
* Mixin a given set of properties
* @param prop The properties to mix in
* @param obj [optional] The object to add the mixin
*/
mixin: function (prop, obj) {
var self = obj || this,
fnTest = /\b_super\b/,
_super = Object.getPrototypeOf(self) || self.prototype,
_old;

// Copy the properties over
for (var name in prop) {
// store the old function which would be overwritten
_old = self[name];
// Check if we're overwriting an existing function
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;
// Copy the properties over
for (var name in prop) {
// store the old function which would be overwritten
_old = self[name];
// Check if we're overwriting an existing function
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];
// 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;
// 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 ret;
};
})(_old, name, prop[name]) : prop[name];
}

return self;
},
/**
* Extend the current or a given object with the given property
* and return the extended object.
* @param prop The properties to extend with
* @param obj [optional] The object to extend from
* @returns The extended object
*/
extend: function (prop, obj) {
return this.mixin(prop, Object.create(obj || this));
},
/**
* Return a callback function with this set to the current or a given context object.
* @param name Name of the method to proxy
* @param args... [optional] Arguments to use for partial application
*/
proxy: function (name) {
var fn = this[name],
args = Array.prototype.slice.call(arguments, 1);
return self;
},
/**
* Extend the current or a given object with the given property
* and return the extended object.
* @param prop The properties to extend with
* @param obj [optional] The object to extend from
* @returns The extended object
*/
extend: function (prop, obj) {
return this.mixin(prop, Object.create(obj || this));
},
/**
* Return a callback function with this set to the current or a given context object.
* @param name Name of the method to proxy
* @param args... [optional] Arguments to use for partial application
*/
proxy: function (name) {
var fn = this[name],
args = Array.prototype.slice.call(arguments, 1);

args.unshift(this);
return fn.bind.apply(fn, args);
}
};
});
})();
args.unshift(this);
return fn.bind.apply(fn, args);
}
};
}));