1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

add additional examples of accessing JavaScript values

This commit is contained in:
Charles Lowell 2014-01-03 12:02:04 +02:00
parent 59508cc8a4
commit 7b85d0d6cf

View file

@ -31,11 +31,20 @@ evaluate some simple JavaScript
cxt = V8::Context.new
cxt.eval('7 * 6') #=> 42
call JavaScript functions
access values inside your JavaScript context from Ruby
cxt.eval 'var val = {num: 5, fun: function isTruthy(arg) { return !!arg }}'
val = cxt[:val] #=> V8::Object
cxt[:val] == cxt.scope.val #=> true
val.num #=> 5
val.isTruthy(1) #=> true
this includes references to JavaScript functions
truthy = val[:isTruthy] #=> V8::Function
truthy.call(' ') #=> true
truthy.call(0) #=> false
cxt.eval "function isTruthy(arg) { return !!arg }"
cxt[:isTruthy].call(' ') #=> true
cxt.scope.isTruthy(0) #=> false
embed values into the scope of your context