Skip to content

Instantly share code, notes, and snippets.

@GCheung55
Forked from subtleGradient/lateBind.js
Created November 6, 2009 01:39
Show Gist options
  • Save GCheung55/227600 to your computer and use it in GitHub Desktop.
Save GCheung55/227600 to your computer and use it in GitHub Desktop.
// -*- Mode: Javascript; tab-width: 4; -*-
/*
---
privides: [String.bound, String.prototype.bind]
description: Lets you bind to a method that doesn't exist yet, and then swap out methods without re-wiring all your codez
author: Thomas Aylott <subtlegradient.com>
copyright: 2009 Thomas Aylott
license: MIT Style
...
*/
(function(global){
this.String.prototype.bind = function(object, args){
var methodName = this;
var bound = function(){
var args = Array.prototype.slice.call(arguments);
if (!args.length) args = bound.args;
if (typeof bound.object[bound.methodName] == 'function')
return bound.object[bound.methodName].apply(bound.object, args);
return new Error('No such method "'+ bound.methodName +'" in object');
};
bound.displayName = bound.methodName = methodName;
bound.object = object || global;
bound.args = args;
return bound;
};
this.String.bound = function(methodName, object, args){
if (typeof methodName == 'function') return methodName.bound(object, args);
return String(methodName).bound(object, args);
};
}).call(this,this);
// -*- Mode: Javascript; tab-width: 4; -*-
function PASS(){ return 'PASS'; };
function FAIL(){ throw new Error('FAIL'); return 'FAIL FAIL'; };
// Basics
var myGlobalFunction_bound = 'myGlobalFunction'.bind();
console.assert(myGlobalFunction_bound.object == window, 'myGlobalFunction_bound object should be the globalObject');
console.assert(myGlobalFunction_bound.methodName == 'myGlobalFunction', 'myGlobalFunction_bound object should be the globalObject');
console.assert(typeof this['myGlobalFunction'] == 'undefined', 'Should NOT have a myGlobalFunction');
console.assert(myGlobalFunction_bound() instanceof Error, 'Should return an Error trying to call "myGlobalFunction" on the globalObject');
var myGlobalFunction = PASS;
console.assert(typeof this['myGlobalFunction'] == 'function', 'Should have a myGlobalFunction');
console.assert(myGlobalFunction_bound() === 'PASS', 'Should call "myGlobalFunction" on the globalObject');
// methodName
myGlobalFunction_bound.displayName = myGlobalFunction_bound.methodName = 'myGlobalFunction2';
console.assert(myGlobalFunction_bound.methodName == 'myGlobalFunction2', 'Should be able to change the methodName later');
console.assert(typeof this['myGlobalFunction2'] == 'undefined', 'Should NOT have a myGlobalFunction2');
console.assert(myGlobalFunction_bound() instanceof Error, 'Should return an Error trying to call "myGlobalFunction2" on the globalObject');
myGlobalFunction2 = FAIL;
console.assert(typeof this['myGlobalFunction'] == 'function', 'Should have a myGlobalFunction');
try{
FAILED = false;
myGlobalFunction_bound();
}catch(e){
FAILED = true;
}
console.assert(FAILED === true, 'When a bound method throws an error, it Should be thrown');
// Object
var anotherObject = {};
myGlobalFunction_bound.object = anotherObject;
console.assert(myGlobalFunction_bound.object != window, 'Should be able to change the object of a bound function later');
console.assert(myGlobalFunction_bound() instanceof Error, 'Should return an Error trying to call "myGlobalFunction2" on the globalObject');
anotherObject.myGlobalFunction2 = PASS;
console.assert(typeof anotherObject['myGlobalFunction2'] == 'function', 'Should have a myGlobalFunction2');
console.assert(myGlobalFunction_bound() === 'PASS', 'Should call "myGlobalFunction2" on anotherObject');
// Arguments
function methodWithArgs(){ return Array.prototype.slice.call(arguments); };
var methodWithArgs_bound = 'methodWithArgs'.bind();
console.assert(methodWithArgs_bound('arg1')[0] === 'arg1', 'Should pass arguments to the function');
console.assert(methodWithArgs_bound('arg1','arg2')[1] === 'arg2', 'Should pass arguments to the function');
var methodWithArgs_bound = 'methodWithArgs'.bind(this,['arg1','arg2']);
console.assert(methodWithArgs_bound()[0] === 'arg1', 'Should pass arguments to the function when binding');
console.assert(methodWithArgs_bound()[1] === 'arg2', 'Should pass arguments to the function when binding');
console.log('All Tests Ran');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment