Search code examples
javascriptsamsung-smart-tv

SquirrelFish seems to miss "bind()", how to bind a JS callback to "this" without it?


anybody knows who to bind a JS callback to "this" without "bind()"?

according to Samsung specs:

2013 is on V8 : works fine (see linked shot, too large to add)

2012 is on SquirrelFish : gets exception: "bind()" is not available as a function (see linked shot, too large to add)

the offending code is this:

		var extJsCallBacks = [
          function(){this._setupSystemInfo();}.bind(this), 
          function(){this._setupConfigInfo();}.bind(this),
          function(){this._setupRepository();}.bind(this), 
          function(){this._setupContents();}.bind(this)
          ];

any hints?


Solution

  • Polyfill found on MDN

    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),
            fToBind = this,
            fNOP    = function() {},
            fBound  = function() {
              return fToBind.apply(this instanceof fNOP
                     ? this
                     : oThis,
                     aArgs.concat(Array.prototype.slice.call(arguments)));
            };
    
        if (this.prototype) {
          // Function.prototype doesn't have a prototype property
          fNOP.prototype = this.prototype; 
        }
        fBound.prototype = new fNOP();
    
        return fBound;
      };
    }