annotate polyfill.js @ 92:b211a00efa7f

Various implementation improvements.
author Sonny Piers <sonny.piers@gmail.com>
date Thu, 05 Apr 2012 17:36:43 +0200
parents d9804e206393
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 }