Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
Created September 3, 2011 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelficarra/1191602 to your computer and use it in GitHub Desktop.
Save michaelficarra/1191602 to your computer and use it in GitHub Desktop.
bootstrapping a shim library with safe calls
delete Function.prototype.bind; // assume environment doesn't have `Function::bind`
var protoObject = {},
protoArray = [],
protoFunction = function(){},
nativeObject = Object,
nativeObjectCreate = Object.create,
nativeArraySlice = protoArray.slice,
nativeArrayConcat = protoArray.concat;
var nativeCall = protoFunction.call,
nativeApply = protoFunction.apply;
var apply, call;
if (typeof protoFunction.bind == "function") {
if (nativeCall.call !== nativeCall) {
var diffCallDotCall = true,
oldCallDotCall = nativeCall.call;
nativeCall.call = nativeCall;
}
call = nativeCall.call(protoFunction.bind, nativeCall, nativeCall);
apply = nativeCall.call(protoFunction.bind, nativeCall, nativeApply);
if (diffCallDotCall)
nativeCall.call = oldCallDotCall;
} else {
// if this path is taken, `Function::call` must have a `call` property
// (which may be inherited from `Function::`) that points to itself, so we
// should add it as an own property in case it is deleted from the
// prototype later on
nativeCall.call = nativeCall;
call = function (fn, context) {
return nativeCall.call(nativeApply, fn, context, nativeCall.call(nativeArraySlice, arguments, 2));
};
apply = function (fn, context, args) {
return nativeCall.call(nativeApply, fn, context, args);
};
Function.prototype.bind = function bind(that) {
var target = this;
// 2. If IsCallable(Target) is false, throw a TypeError exception.
if (typeof target != "function")
throw new TypeError();
var args = call(nativeArraySlice, arguments, 1); // for normal call
var bound = function () {
if (this instanceof bound) {
// When the [[Construct]] internal method of a function object,
// F that was created using the bind function is called with a
// list of arguments ExtraArgs the following steps are taken:
var F = function(){};
F.prototype = target.prototype;
var self = new F;
// equiv: target.apply(self, [...args, ...arguments])
var result = call(
nativeApply, target, self,
call(
nativeArrayConcat, args,
call(nativeArraySlice, arguments)
)
);
if (result !== null && call(nativeObject, null, result) === result)
return result;
return self;
} else {
// equiv: target.call(this, ...boundArgs, ...args)
return call(
nativeApply, target, that,
call(
nativeArrayConcat, args,
call(nativeArraySlice, arguments)
)
);
}
};
return bound;
};
}
// ... rest of file ...
function foo() { console.log(''+this); }
var g = foo.bind(Object(42));
delete Function.prototype.call
g(); // should output 42
Copyright (c) 2011, Michael Ficarra
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the project nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright holder be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages (including,
but not limited to, procurement of substitute goods or services; loss of use,
data, or profits; or business interruption) however caused and on any theory of
liability, whether in contract, strict liability, or tort (including negligence
or otherwise) arising in any way out of the use of this software, even if
advised of the possibility of such damage.
Copy link

ghost commented Sep 3, 2011

@michaelficarra This took me an hour to work through, but the solution is so intuitively obvious in retrospect that it's almost shameful. See https://gist.github.com/1191821 for the results of my investigation.

@michaelficarra
Copy link
Author

LICENSE file added at Mark Miller's request. Mark: this gist does contain portions of kriskowal/es5-shim, so that license may be of interest to you as well.

@michaelficarra
Copy link
Author

Copying over (the idea behind) my comment from Kit's gist:

If Function.prototype.bind doesn't exist at definition time, the function referenced by Function.prototype.call at definition time must have a call property (that doesn't have to be an own-property) that points to itself during runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment