Sha256: f72e66ba17f718a19dc97ff346591b2777aeb9c6eb65b4e70388ace13ada17ec

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

# The JS module provides syntax sugar for calling native javascript
# operators (e.g. typeof, instanceof, new, delete) and global functions
# (e.g. parseFloat, parseInt).
module JS
  # Use delete to remove a property from an object.
  def delete(object, property)
    `delete #{object}[#{property}]`
  end

  # The global object
  def global
    `Opal.global`
  end

  # Use in to check for a property in an object.
  def in(property, object)
    `#{property} in #{object}`
  end

  # Use instanceof to return whether value is an instance of the function.
  def instanceof(value, func)
    `#{value} instanceof #{func}`
  end

  # Use new to create a new instance of the prototype of the function.
  def new(func, *args, &block)
    args << block if block
    f = `function(){return func.apply(this, args)}`
    f.JS[:prototype] = func.JS[:prototype]
    `new f()`
  end

  # 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.
  def typeof(value)
    `typeof #{value}`
  end

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

  # Call the global javascript function with the given arguments.
  def call(func, *args, &block)
    g = global
    args << block if block
    g.JS[func].JS.apply(g, args)
  end
  alias method_missing call

  extend self
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
opal-0.9.0.beta2 stdlib/js.rb
opal-0.9.0.beta1 stdlib/js.rb
opal-wedge-0.9.0.dev stdlib/js.rb