Class: JavaScript::Function
- Inherits:
-
Struct
- Object
- Struct
- JavaScript::Function
- Defined in:
- lib/javascript.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#body ⇒ Object
Returns the value of attribute body.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #apply(target = nil, arg_values) ⇒ Object
- #arity ⇒ Object
- #bind(target) ⇒ Object
- #call(target = nil, *arg_values) ⇒ Object
-
#initialize ⇒ Function
constructor
A new instance of Function.
- #to_proc ⇒ Object
Constructor Details
#initialize ⇒ Function
Returns a new instance of Function.
241 242 243 244 |
# File 'lib/javascript.rb', line 241 def initialize(*) @scope = JavaScript.current_scope super end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args
240 241 242 |
# File 'lib/javascript.rb', line 240 def args @args end |
#body ⇒ Object
Returns the value of attribute body
240 241 242 |
# File 'lib/javascript.rb', line 240 def body @body end |
#name ⇒ Object
Returns the value of attribute name
240 241 242 |
# File 'lib/javascript.rb', line 240 def name @name end |
Instance Method Details
#apply(target = nil, arg_values) ⇒ Object
254 255 256 |
# File 'lib/javascript.rb', line 254 def apply(target = nil, arg_values) call(target, *arg_values) end |
#arity ⇒ Object
246 247 248 |
# File 'lib/javascript.rb', line 246 def arity args.length end |
#bind(target) ⇒ Object
258 259 260 |
# File 'lib/javascript.rb', line 258 def bind(target) BoundFunction.new(target, name, args, body) end |
#call(target = nil, *arg_values) ⇒ Object
250 251 252 |
# File 'lib/javascript.rb', line 250 def call(target = nil, *arg_values) ::Object.instance_method(:instance_exec).bind(target).call(*arg_values, &self) end |
#to_proc ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/javascript.rb', line 262 def to_proc parent_scope = @scope arg_names = args.map(&:name) unwrapped = body FunctionWrapper.new(arg_names) do |*arg_values| locals = Hash[ arg_names.zip(arg_values) ] locals[:arguments] = arg_values parent_scope.__spawn__(self, locals).__eval__(*arg_values, &unwrapped) end end |