[CoffeeScript] Polyfill for missing bind() in PhantomJS

Did receive unexplicable errors when running JS tests through Karma on a PhantomJS server, like:
TypeError: 'undefined' is not a function (evaluating 'this.proxy[method].bind(this.proxy)')

Looks like the problem is that PhantomJS is built with an old version of the JavaScript core where the bind method is not part of the Function prototype. There's a polyfill for that further down this page.
That adds the method to the Function prototype. Here now is the same translated to CoffeeScript:


if !Function::bind
  Function::bind = (oThis) ->
    if typeof @ != 'function'
      throw new TypeError 'Function.prototype.bind - what is trying to be bound is not callable'

    aArgs   = [
      Array::slice.call(arguments, 1),
      fToBind = this,
      fNOP    = ->,
      fBound  = ->
        fToBind.apply (if @ instanceof fNOP then @ else oThis), aArgs.concat Array::slice.call arguments
    ]

    if @prototype
      fNOP.prototype = @prototype; 
    fBound.prototype = new fNOP()
    fBound

Kommentare

Beliebte Posts aus diesem Blog

Angular is trash

Drawing a line after an arrow that follows a path in a Flash MovieClip

Dropping Clips on Sprite Graphics V2