From 9b411c555841b3321b4a26ed047dd9bc070467ba Mon Sep 17 00:00:00 2001 From: Raider Zane Date: Fri, 13 Jul 2012 02:05:05 +0800 Subject: [PATCH] add base function for jump 1. support AMD and CJS 2. add bind function from jquery or underscore --- jumper.js | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 jumper.js diff --git a/jumper.js b/jumper.js new file mode 100644 index 0000000..fcce851 --- /dev/null +++ b/jumper.js @@ -0,0 +1,78 @@ +// ## Multiple environment support +;(function(name, definition){ + var Jumper = definition(this), + hasDefine = typeof define === 'function' && define.amd, + hasExports = typeof moudule !== 'undefined' && moudule.exports; + + if(hasDefine){/*AMD Module*/ + define(Jumper); + } + else if(hasExports){/*Node.js Module*/ + moudule.exports = Jumper; + } + else{ + /*Assign to common namespaces or simply the global object (window)*/ + this.Jumper = Jumper; + } +})('Jumper', function(global, undef){ + "use strict"; + + var ver = '0.0.1' + , slice = Array.prototype.slice + , nativeBind = Function.prototype.bind + , noop + , bind + , isFunction; + + + if(global.jQuery){ + noop = global.jQuery.noop; + } + else{ + noop = function(){}; + } + + if(global._){ + bind = _.bind; + isFunction = _.isFunction; + } + else if(global.jQuery){ + bind = jQuery.proxy; + isFunction = global.jQuery.isFunction; + } + else{ + isFunction = function(obj) { + return toString.call(obj) == '[object Function]'; + }; + bind = function(func, context){ + var bound, args; + + if(func.bind === nativeBind && nativeBind){ + return nativeBind.apply(func, slice.call(arguments, 1)) + } + + if(!isFunction(func)){ + throw new TypeError; + } + + args = slice.call(arguments, 2); + return bound = function() { + if(this instanceof bound) { + noop.prototype = func.prototype; + var self = new noop(); + var result = func.apply(self, args.concat(slice.call(arguments))); + + if (Object(result) === result){ + return result; + } + return self; + } + return func.apply(context, args.concat(slice.call(arguments))); + } + }; + } + + var Jumper = function(){ + + }; +});