-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.js
22 lines (22 loc) · 961 Bytes
/
bind.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var nativeBind = Function.prototype.bind;
/**
* Bind function to scope. Useful for events.
* @param {Function} fn function
* @param {Object} scope Scope of the function to be executed in
* @example $("div").fadeIn(100, W.bind(this, this.transitionDidFinish));
*/
function bind( fn, scope ) {
var bound, args;
if ( fn.bind === nativeBind && nativeBind ) return nativeBind.apply( fn, Array.prototype.slice.call( arguments, 1 ) );
args = Array.prototype.slice.call( arguments, 2 );
// @todo: don't link this
bound = function() {
if ( !(this instanceof bound) ) return fn.apply( scope, args.concat( Array.prototype.slice.call( arguments ) ) );
W.ctor.prototype = fn.prototype;
var self = new ctor();
var result = fn.apply( self, args.concat( Array.prototype.slice.call( arguments ) ) );
if ( Object( result ) === result ) return result;
return self;
};
return bound;
}