This repository has been archived by the owner. It is now read-only.

Function.prototype.bind is undefined #10522

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

Comments

Projects
None yet
@kpozin

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

This comment has been minimized.

Show comment
Hide comment
@kpozin

kpozin 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.

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

This comment has been minimized.

Show comment
Hide comment
@ariya

ariya Jul 18, 2012

Owner

j...@thereitis.com commented:

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

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

This comment has been minimized.

Show comment
Hide comment
@creationix

creationix Aug 7, 2012

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 commented Aug 7, 2012

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

This comment has been minimized.

Show comment
Hide comment
@creationix

creationix Aug 7, 2012

t...@creationix.com commented:

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

creationix commented Aug 7, 2012

t...@creationix.com commented:

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

@yanfalies

This comment has been minimized.

Show comment
Hide comment
@ariya

This comment has been minimized.

Show comment
Hide comment
@ariya

ariya Sep 28, 2012

Owner

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

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

Owner

ariya commented Sep 28, 2012

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

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

@ariya

This comment has been minimized.

Show comment
Hide comment
@ariya

ariya Jan 3, 2013

Owner

bradwill...@gmail.com commented:

I hit this issue too, still in 1.8

Owner

ariya commented Jan 3, 2013

bradwill...@gmail.com commented:

I hit this issue too, still in 1.8

@tupton

This comment has been minimized.

Show comment
Hide comment
@tupton

tupton 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?

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

This comment has been minimized.

Show comment
Hide comment
@creationix

creationix Apr 9, 2013

@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

creationix commented Apr 9, 2013

@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

This comment has been minimized.

Show comment
Hide comment
@tupton

tupton 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.

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

Add shim for `Function.prototype.bind()`
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 #10522.
@molily

This comment has been minimized.

Show comment
Hide comment
@molily

molily Apr 11, 2013

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

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

This comment has been minimized.

Show comment
Hide comment
@ariya

ariya Apr 11, 2013

Owner

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.

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

This comment has been minimized.

Show comment
Hide comment
@jfirebaugh

jfirebaugh Apr 11, 2013

@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?

jfirebaugh commented Apr 11, 2013

@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

This comment has been minimized.

Show comment
Hide comment
@ariya

ariya Apr 11, 2013

Owner

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

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

Fix source-map breaking on PhantomJS where bind isn't available
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

This comment has been minimized.

Show comment
Hide comment
@matenadasdi

matenadasdi commented Nov 5, 2013

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

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Nov 5, 2013

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

mitar commented Nov 5, 2013

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

@KKrisu

This comment has been minimized.

Show comment
Hide comment
@KKrisu

KKrisu 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! :)

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

This comment has been minimized.

Show comment
Hide comment
@frankcortes

frankcortes Dec 19, 2013

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

frankcortes commented Dec 19, 2013

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

@chovy

This comment has been minimized.

Show comment
Hide comment
@chovy

chovy Feb 5, 2014

Still seeing on 1.9.7 mac (installed with npm install -g phantomjs)

$ phantomjs --version
1.9.7
user@host:~/projects/foo
$ phantomjs
phantomjs> Function.prototype.bind
undefined

chovy commented Feb 5, 2014

Still seeing on 1.9.7 mac (installed with npm install -g phantomjs)

$ phantomjs --version
1.9.7
user@host:~/projects/foo
$ phantomjs
phantomjs> Function.prototype.bind
undefined
@bensquire

This comment has been minimized.

Show comment
Hide comment
@bensquire

bensquire Feb 12, 2014

Still seeing on 1.9.7 mac (installed with npm install -g phantomjs)

Ditto...

bensquire commented Feb 12, 2014

Still seeing on 1.9.7 mac (installed with npm install -g phantomjs)

Ditto...

@pixelsonly

This comment has been minimized.

Show comment
Hide comment
@pixelsonly

pixelsonly Feb 17, 2014

Confirmed. Still seeing this on 1.9.7 as well

pixelsonly commented Feb 17, 2014

Confirmed. Still seeing this on 1.9.7 as well

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Feb 17, 2014

People, no need for comments saying that it still doesn't work. Of course it still doesn't work because if it was not addressed. If it was, there would be a comment here saying that it was. PhantomJS is built upon old QtWebKit implementation and until they migrate to new version it will simply not work. In meantime you can use a polyfill (I recommend using underscore's bind).

mitar commented Feb 17, 2014

People, no need for comments saying that it still doesn't work. Of course it still doesn't work because if it was not addressed. If it was, there would be a comment here saying that it was. PhantomJS is built upon old QtWebKit implementation and until they migrate to new version it will simply not work. In meantime you can use a polyfill (I recommend using underscore's bind).

@pixelsonly

This comment has been minimized.

Show comment
Hide comment
@pixelsonly

pixelsonly Feb 17, 2014

@mitar I respect that, commenting isn't going to address the situation, but thanks for the background info. Your recommendation is helpful too.

pixelsonly commented Feb 17, 2014

@mitar I respect that, commenting isn't going to address the situation, but thanks for the background info. Your recommendation is helpful too.

@felixhammerl

This comment has been minimized.

Show comment
Hide comment
@felixhammerl

felixhammerl Mar 3, 2014

Function.prototype.bind seemed to work in a "normal" context, but it is broken in a Worker...

Version: 1.9.7

felixhammerl commented Mar 3, 2014

Function.prototype.bind seemed to work in a "normal" context, but it is broken in a Worker...

Version: 1.9.7

jessebeach added a commit to quailjs/quail that referenced this issue Mar 18, 2014

I had to polyfill Function.prototype.bind because phantomjs' JS engin…
…e doesn't support it. And that meant removing newcap from .jshintrc because it was complaining about Function not being preceded by a new operator.

See ariya/phantomjs#10522

Signed-off-by: J. Renée Beach <splendidnoise@gmail.com>
@mmucklo

This comment has been minimized.

Show comment
Hide comment
@mmucklo

mmucklo Jul 16, 2014

FWIW, I had success with es5-shim: https://github.com/es-shims/es5-shim

(at least it took care of Function.prototype.bind issue).

Although, granted, it includes a lot of other potential polyfills that might not be needed...

mmucklo commented Jul 16, 2014

FWIW, I had success with es5-shim: https://github.com/es-shims/es5-shim

(at least it took care of Function.prototype.bind issue).

Although, granted, it includes a lot of other potential polyfills that might not be needed...

@DominicTobias

This comment has been minimized.

Show comment
Hide comment
@DominicTobias

DominicTobias Jul 21, 2014

IMO shouldn't be necessary to shim such a fundamental function that's been around since ECMAScript 5.1.. at least on a dev tool?

DominicTobias commented Jul 21, 2014

IMO shouldn't be necessary to shim such a fundamental function that's been around since ECMAScript 5.1.. at least on a dev tool?

@JamesMGreene

This comment has been minimized.

Show comment
Hide comment
@JamesMGreene

JamesMGreene Jul 21, 2014

Collaborator

Blah blah blah, opinions and wishes aren't driving this issue anywhere helpful. This is fixed in the impending PhantomJS v2.0 and won't be fixed before then.

Collaborator

JamesMGreene commented Jul 21, 2014

Blah blah blah, opinions and wishes aren't driving this issue anywhere helpful. This is fixed in the impending PhantomJS v2.0 and won't be fixed before then.

@tpendragon tpendragon referenced this issue Jul 26, 2014

Merged

Fixes #18 #71

@ariya

This comment has been minimized.

Show comment
Hide comment
@ariya

ariya Jul 30, 2014

Owner

This is now working on the master branch (still unstable as of now). See also #10448.

Owner

ariya commented Jul 30, 2014

This is now working on the master branch (still unstable as of now). See also #10448.

@ariya ariya closed this 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

Use Poltergeist as a default driver for Capybara
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/ddf-ui that referenced this issue Nov 29, 2014

pklinef added a commit to pklinef/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

This comment has been minimized.

Show comment
Hide comment
@andreaugusto

andreaugusto Dec 10, 2014

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!

andreaugusto commented Dec 10, 2014

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

This comment has been minimized.

Show comment
Hide comment
@JamesMGreene

JamesMGreene Dec 10, 2014

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).

Collaborator

JamesMGreene commented Dec 10, 2014

@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

This comment has been minimized.

Show comment
Hide comment
@benlesh

benlesh Dec 12, 2014

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

benlesh commented Dec 12, 2014

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

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Dec 13, 2014

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

mitar commented Dec 13, 2014

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

@amercier

This comment has been minimized.

Show comment
Hide comment
@amercier

amercier 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

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

This comment has been minimized.

Show comment
Hide comment
@Vitallium

Vitallium Feb 3, 2015

Collaborator

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

Collaborator

Vitallium commented Feb 3, 2015

@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

- add support for phantomjs
 - 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

Josh Prince
Add React.js
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.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.