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:
parent
59508cc8a4
commit
7b85d0d6cf
1 changed files with 13 additions and 4 deletions
17
README.md
17
README.md
|
@ -31,11 +31,20 @@ evaluate some simple JavaScript
|
||||||
cxt = V8::Context.new
|
cxt = V8::Context.new
|
||||||
cxt.eval('7 * 6') #=> 42
|
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
|
embed values into the scope of your context
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue