Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Function.prototype.bind is undefined #10522

Closed
kpozin opened this issue May 2, 2012 · 41 comments
Closed

Function.prototype.bind is undefined #10522

kpozin opened this issue May 2, 2012 · 41 comments
Milestone

Comments

@kpozin
Copy link

kpozin commented May 2, 2012

kpozin@gmail.com commented:

Which version of PhantomJS are you using? Tip: run 'phantomjs --version'.
1.5.1 (development)

What steps will reproduce the problem?

  1. Run phantomjs in REPL mode.
  2. Type "Function.prototype.bind" and hit Return

What is the expected output? What do you see instead?
Expected: "[Function]"
Actual: undefined

Which operating system are you using?
Win 7 x64

Did you use binary PhantomJS or did you compile it from source?
Binary

Please provide any additional information below.
Function.prototype.bind is also undefined when running scripts (non in REPL mode).

Disclaimer:
This issue was migrated on 2013-03-15 from the project's former issue tracker on Google Code, Issue #522.
🌟   24 people had starred this issue at the time of migration.

@kpozin
Copy link
Author

kpozin commented May 2, 2012

kpozin@gmail.com commented:

Looks like this is failing because PhantomJS is built with an old version of JavaScriptCore that is missing the "bind" implementation.

@ariya
Copy link
Owner

ariya commented Jul 18, 2012

j...@thereitis.com commented:

Reproduced issue with version 1.6.0, Mac OS X 10.7.4, compiled from source.

@creationix
Copy link

t...@creationix.com commented:

I was just bit by this bug too. We have crazy new stuff like DataView and binary websockets, but we don't have basic ES5 functions!?

phantomjs 1.6.1 on Ubuntu 12.04 x64

@creationix
Copy link

t...@creationix.com commented:

I take back what I said about binary websockets. That doesn't appear to work. Sorry for the confusion.

@yanfalies
Copy link

@ariya
Copy link
Owner

ariya commented Sep 28, 2012

ariya.hi...@gmail.com commented:

This is likely solved once we fix issue #10031 (update WebKit).

@ariya
Copy link
Owner

ariya commented Jan 3, 2013

bradwill...@gmail.com commented:

I hit this issue too, still in 1.8

@tupton
Copy link

tupton commented Apr 9, 2013

Is there a current workaround for this while we wait for #10031? Simply creating a shim in my phantom runner script using this code from MDN doesn't seem to apply to the Function object that phantomjs uses. Is there somewhere else I should be trying to insert this shim?

@creationix
Copy link

@tupton When I created a Function.prototype.bind polyfill, it worked fine for me. https://github.com/c9/smith/blob/master/tests/public/test.js#L2-L7

@tupton
Copy link

tupton commented Apr 9, 2013

@creationix Thanks for that pointer. I'm extremely new to phantomjs and still trying to figure out everything in context. I was trying to define Function.prototype.bind in my actual phantom script, not in my tests or test page. Moving it to my test runner page worked.

execjosh added a commit to execjosh/phantomjs that referenced this issue Apr 11, 2013
This change adds a shim for `Function.prototype.bind()`, which can
be an intermediate step until we get the native implementation from
a newer WebKit.

This fixes ariya#10522.
@molily
Copy link

molily commented Apr 11, 2013

“Why PhantomJS doesn't have Function.prototype.bind” by @ariya: https://groups.google.com/forum/#!msg/phantomjs/r0hPOmnCUpc/uxusqsl2LNoJ

@ariya
Copy link
Owner

ariya commented Apr 11, 2013

My long explanation is posted in https://groups.google.com/forum/#!msg/phantomjs/r0hPOmnCUpc/uxusqsl2LNoJ.

The summary:

We never intended this to happen. PhantomJS relies on QtWebKit in Qt and the last stable version of Qt (which has updated QtWebKit) was slow to emerge (for a lot of technical and non-technical reasons).

We already tried some attempts to rectify the situation. Unfortunately it's a lot of work and all the combined spare-time of our contributors is not enough to bring the solution to the table.

@jfirebaugh
Copy link

@ariya, thank you for your hard work, and thanks to the other volunteers. Your efforts are much appreciated. The long explanation helps.

With respect to the port to Qt 5, where does that get us exactly? How fresh is its fresher QtWebKit?

@ariya
Copy link
Owner

ariya commented Apr 11, 2013

For Qt 5 stuff, please track issue #10448. It's too early to say but what I can confirm is that bind is there.

abe33 referenced this issue in abe33/source-map Jul 30, 2013
PhantomJS (1.9.1 and below) does not provides the `Function.prototype.bind` method. Since PhantomJS is already
used in production in CI environment such TravisCI it may be simpler for source-map to have a shim rather than waiting for
PhantomJS to fix that.
@matenadasdi
Copy link

Hi,

Are there any news on this?

This polyfill can handle the issue, but it would be great if we could get a newer JS Core in PHJS:)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FFunction%2Fbind

@mitar
Copy link

mitar commented Nov 5, 2013

For me polyfill didn't work. But underscore's bind did.

@KKrisu
Copy link

KKrisu commented Nov 7, 2013

I have added mentioned shim, but
console.log.bind(console);
still didn't work, so I've also added this reference:
if(!console.log.bind) { console.log.bind = Function.prototype.bind; }
And now it works! :)

@frankcortes
Copy link

In my case I can replace native bind() for Underscore's bind and works properly.

@ariya ariya closed this as completed Jul 30, 2014
sectore pushed a commit to sectore/todomvc-famous that referenced this issue Sep 1, 2014
MoOx added a commit to MoOx/pjax that referenced this issue Oct 14, 2014
jgonera added a commit to jgonera/howwrong that referenced this issue Nov 18, 2014
So that Travis build passes. Also don't use Function.bind because oddly
enough PhantomJS still barfs on it, not sure why though:
ariya/phantomjs#10522
pklinef added a commit to pklinef/archive-ddf-ui that referenced this issue Nov 29, 2014
pklinef added a commit to pklinef/archive-ddf-ui that referenced this issue Nov 29, 2014
pklinef added a commit to codice-archives/archive-ddf-ui that referenced this issue Dec 3, 2014
@andreaugusto
Copy link

Extracted from @creationix post. The full solution is here:

// PhantomJS doesn't support bind yet
Function.prototype.bind = Function.prototype.bind || function (thisp) {
    var fn = this;
    return function () {
        return fn.apply(thisp, arguments);
    };
}; 

Append this on top of your tests and it will fix the issue.

Worked like a charm for me. Thank you!

@JamesMGreene
Copy link
Collaborator

@andreaugusto / @creationix:
That polyfill will support the most common usage of bind (binding the context argument) in whatever single scope it is executed, which may be all that some users need.

However, there are also a number of shortcomings with this workaround that will prevent from working for everyone's needs:

  • It is an incomplete implementation of bind (missing argument currying/binding, for example)
  • It adds a new enumerable property to Function.prototype
  • It must exist in every context and every page load/navigation prior to JS execution: PhantomJS outer context, WebPage instances' client context, child windows, child frames, child iframes, etc.

The first two can be addressed with a better polyfill implementation (Mozilla's MDN one is pretty good). However, the third seems to be elusive despite our having creates various hooks into the page lifecycle(s).

@benlesh
Copy link

benlesh commented Dec 12, 2014

@andreaugusto, you might wish to use the polyfill from MDN which is more complete, but lists its caveats.

@mitar
Copy link

mitar commented Dec 13, 2014

For me polyfill from MDN didn't work well in all cases, but underscore _.bind did.

@amercier
Copy link

amercier commented Feb 3, 2015

Any chances to see this fixed? PhantomJS seems to be the last browser not to support Function.prototype.bind.

By the way, if you are using Bower, bind-polyfill by @kdimatteo provides Mozilla's MDN polyfill:

bower install bind-polyfill

@vitallium
Copy link
Collaborator

@amercier, This issue is fixed in PhantomJS 2 :)

Repository owner locked and limited conversation to collaborators Feb 4, 2015
guy-mograbi-at-gigaspaces added a commit to cloudify-cosmo/cloudify-js that referenced this issue Jun 11, 2015
 - fix Function.bind phantomjs issue: ariya/phantomjs#10522
 - make tests more stable
 - fix tests for jquery and vanilla

 CFY-2106
jprince pushed a commit to jprince/sleepers-and-keepers that referenced this issue Jun 26, 2015
Converts one simple view to React. Remaining .erb views will be converted in a
subsequent commit.

Includes a polyfill for `Function.prototype.bind` in Poltergeist/PhantomJS
(apparently it will be addressed in PhantomJS v2.0).

Source: ariya/phantomjs#10522.
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.