Skip to content

Instantly share code, notes, and snippets.

@yoko
Created July 16, 2009 11:28
Show Gist options
  • Save yoko/148370 to your computer and use it in GitHub Desktop.
Save yoko/148370 to your computer and use it in GitHub Desktop.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Function.prototype.bind</title>
<link rel="stylesheet" type="text/css" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css"/>
<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
<script type="text/javascript" src="Function.prototype.bind.js"></script>
</head>
<body>
<h1 id="qunit-header">Number.prototype.toFormatString</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<script type="text/javascript">
module('Function.prototype.bind');
test('bind', function() {
var foo = function(callback) {
callback.call(foo, 'foo!');
};
var bar = function(callback) {
callback.call(bar);
};
foo(function() {
same(this, foo, 'no bind');
});
foo((function() {
same(this, foo, 'no bind');
}).bind());
foo((function() {
same(this, bar, 'bind to bar');
}).bind(bar));
foo((function(a, b, foo) {
same(this, bar, 'bind to bar');
equals(a, 'a', 'bind with args');
equals(b, 'b', 'bind with args');
equals(foo, 'foo!', 'original args');
}).bind(bar, 'a', 'b'));
});
</script>
</body>
</html>
Function.prototype.bind = function(self) {
if (!self) return this;
var f = this;
var args = Array.prototype.slice.call(arguments).slice(1);
return function() {
return f.apply(self, args.concat(Array.prototype.slice.call(arguments)));
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment