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.



68
69
70
71
72
# File 'opal/stdlib/js.rb', line 68

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

Instance Method Details

#[](name) ⇒ Object



64
65
66
# File 'opal/stdlib/js.rb', line 64

def [](name)
  `Opal.global[#{name}]`
end

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

Call the global javascript function with the given arguments.



58
59
60
61
62
# File 'opal/stdlib/js.rb', line 58

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.



8
9
10
# File 'opal/stdlib/js.rb', line 8

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

#globalObject

The global object



13
14
15
# File 'opal/stdlib/js.rb', line 13

def global
  `Opal.global`
end

#in(property, object) ⇒ Object

Use in to check for a property in an object.



18
19
20
# File 'opal/stdlib/js.rb', line 18

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

#instanceof(value, func) ⇒ Object

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



23
24
25
# File 'opal/stdlib/js.rb', line 23

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

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



29
30
31
32
33
# File 'opal/stdlib/js.rb', line 29

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.



47
48
49
# File 'opal/stdlib/js.rb', line 47

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

#void(expr) ⇒ Object

Use void to return undefined.



52
53
54
55
# File 'opal/stdlib/js.rb', line 52

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