Module: JS

Extended by:
JS
Included in:
JS
Defined in:
opal/stdlib/js.rb

Overview

The JS module provides syntax sugar for calling native javascript operators (e.g. typeof, instanceof, new, delete) and global functions (e.g. parseFloat, parseInt).

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject

Call the global javascript function with the given arguments.



61
62
63
64
65
# File 'opal/stdlib/js.rb', line 61

def call(func, *args, &block)
  g = global
  args << block if block
  g.JS[func].JS.apply(g, args)
end

Instance Method Details

#call(func, *args, &block) ⇒ Object Also known as: method_missing

Call the global javascript function with the given arguments.



56
57
58
59
60
# File 'opal/stdlib/js.rb', line 56

def call(func, *args, &block)
  g = global
  args << block if block
  g.JS[func].JS.apply(g, args)
end

#delete(object, property) ⇒ Object

Use delete to remove a property from an object.



6
7
8
# File 'opal/stdlib/js.rb', line 6

def delete(object, property)
  `delete #{object}[#{property}]`
end

#globalObject

The global object



11
12
13
# File 'opal/stdlib/js.rb', line 11

def global
  `Opal.global`
end

#in(property, object) ⇒ Object

Use in to check for a property in an object.



16
17
18
# File 'opal/stdlib/js.rb', line 16

def in(property, object)
  `#{property} in #{object}`
end

#instanceof(value, func) ⇒ Object

Use instanceof to return whether value is an instance of the function.



21
22
23
# File 'opal/stdlib/js.rb', line 21

def instanceof(value, func)
  `#{value} instanceof #{func}`
end

#new(func, *args, &block) ⇒ Object



27
28
29
30
31
# File 'opal/stdlib/js.rb', line 27

def new(func, *args, &block)
  args.insert(0, `this`)
  args << block if block
  `new (#{func}.bind.apply(#{func}, #{args}))()`
end

#typeof(value) ⇒ Object

Use typeof to return the underlying javascript type of value. Note that for undefined values, this will not work exactly like the javascript typeof operator, as the argument is evaluated before the function call.



45
46
47
# File 'opal/stdlib/js.rb', line 45

def typeof(value)
  `typeof #{value}`
end

#void(expr) ⇒ Object

Use void to return undefined.



50
51
52
53
# File 'opal/stdlib/js.rb', line 50

def void(expr)
  # Could use `undefined` here, but this is closer to the intent of the method
  `void #{expr}`
end