annotate lib/bind.js @ 105:fb50311997b5

Add EventEmitter.js lib
author Sonny Piers <sonny.piers@gmail.com>
date Wed, 13 Jun 2012 12:48:06 +0200
parents 5c6056f6da0a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
85
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
1 //js-core doesn't support bind, polyill taken from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
2 if (!Function.prototype.bind) {
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
3 Function.prototype.bind = function (oThis) {
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
4 if (typeof this !== "function") {
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
5 // closest thing possible to the ECMAScript 5 internal IsCallable function
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
6 throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
7 }
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
8
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
9 var aArgs = Array.prototype.slice.call(arguments, 1),
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
10 fToBind = this,
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
11 fNOP = function () {},
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
12 fBound = function () {
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
13 return fToBind.apply(this instanceof fNOP
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
14 ? this
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
15 : oThis || window,
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
16 aArgs.concat(Array.prototype.slice.call(arguments)));
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
17 };
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
18
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
19 fNOP.prototype = this.prototype;
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
20 fBound.prototype = new fNOP();
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
21
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
22 return fBound;
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
23 };
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
24 }