From 54c84fb37a29f04c11ce056c41dc2fa7063cae9a Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 1 Jan 2012 22:24:48 -0500 Subject: [PATCH] Add an implementation of Function.bind() to fix usage in Safari. --- web_frontend/utils.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/web_frontend/utils.js b/web_frontend/utils.js index 991f339..f852bc4 100644 --- a/web_frontend/utils.js +++ b/web_frontend/utils.js @@ -28,3 +28,27 @@ $.extend({ return this(document.createElement(elm)); } }); + +// Not all browsers (notably Safari) support ES5 Function.bind(). This is a +// rough approximation from: +// . +if (!Function.prototype.bind) { + Function.prototype.bind = function (oThis) { + if (typeof this !== "function") { + // closest thing possible to the ECMAScript 5 internal IsCallable function + throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); + } + + var aArgs = Array.prototype.slice.call(arguments, 1); + var fToBind = this; + var fNOP = function () {}; + var fBound = function () { + return fToBind.apply(this instanceof fNOP ? this : oThis || window, + aArgs.concat(Array.prototype.slice.call(arguments))); + }; + + fNOP.prototype = this.prototype; + fBound.prototype = new fNOP(); + return fBound; + }; +} -- 2.22.5