New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`this` is weird when you use `#call` #39

Open
judofyr opened this Issue Jun 6, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@judofyr

judofyr commented Jun 6, 2018

require 'rhino'
p Rhino::VERSION  # => 2.0.4

ctx = Rhino::Context.new
ctx.eval('this.bar = 123');
p ctx.eval('(function() { return this.bar })()')     # => 123 (as expected)
p ctx.eval('this.bar')                               # => 123 (as expected)
p ctx.eval('(function() { return this.bar })').call  # => nil ???

Seems like there a different this when you invoke a function through #call.

@kares

This comment has been minimized.

Collaborator

kares commented Jun 6, 2018

believe this is proper JS -> in the last case this is the global object :)

@judofyr

This comment has been minimized.

judofyr commented Jun 7, 2018

Why isn't this the global object in all of the examples? If I bind to null and call the function I get 123. There seems to be two different global objects in play here?

require 'rhino'
p Rhino::VERSION  # => 2.0.4

ctx = Rhino::Context.new
ctx.eval('this.bar = 123');
p ctx.eval('(function() { return this.bar })()')     # => 123 (as expected)
p ctx.eval('this.bar')                               # => 123 (as expected)
p ctx.eval('(function() { return this.bar })').call  # => nil ???
p ctx.eval('(function() { return this.bar }).bind(null).call()') # => 123 (as expected)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment