How can I access the new javascript keyword from scala.js?

Yoel Garcia

I am porting a JavaScript library to Scalajs. The JS Objects are created with the new keyword on the JavaScript side, so this is what I do in most cases.

trait Point extends js.Object {
  def normalize(length: Double): Point = js.native
}

This seems to work well for methods, however, this doesn't work for constructors.

@JSName("paper.Point")
object PointNative extends js.Object {
  def apply(props: RectProps): Rectangle = js.native
}

The above code does not work. It passes type checks and compiles, but at runtime it returns undefined.

If I modified PointNative like this then all is good.

import js.Dynamic.{ global => g, newInstance => jsnew }
object PointNative {
  def apply(props: RectProps): Rectangle = jsnew(g.paper.Point)(props).asInstanceOf[Point]
}

Is there a way to use @JSName and js.native with the new keyword?

Thanks!

sjrd

Since, in the JavaScript API, paper.Point needs to be instantiated with the new keyword, you need to define PointNative as a class:

@JSName("paper.Point")
class PointNative(props: PointProps) extends js.Object {
  ...
}

which allows to instantiate it with

new PointNative(props)

just like you would have done in JavaScript.

If you also want to be able to instantiate it with just

PointNative(props)

then you need to define an additional, non-js.Object companion with an apply() method:

object PointNative {
  def apply(props: PointProps): PointNative = new PointNative(props)
}

Note that, if you need the companion PointNative to be a js.Object (because you also want to define static methods of paper.Point in it), then you can pimp apply() on it with an implicit class instead:

implicit class PointNativeCompanionOps(val self: PointNative.type) extends AnyVal {
  def apply(props: PointProps): PointNative = new PointNative(props)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I access a method defined with new keyword in a derived class

From Dev

GWT: How can I access its webservice from pure js?

From Dev

How can I access a variable in a javascript function from the rest of the script?

From Dev

How can I access a list from a controller in JavaScript?

From Dev

How can I access the default value for a keyword argument in Python?

From Dev

Why I can't access a JavaScript function defined in a .js file from another .js file?

From Dev

How can I get Javascript Source(js file) from url?

From Dev

How can I access iPhone's new 3D touch in Safari with Javascript?

From Dev

How can I only show the exact lines from a file with a keyword?

From Dev

How can I have keyword argument from string parameter?

From Dev

how can I access functions from another program into my new program

From Dev

Can I inherit access modifiers from super class in Scala?

From Dev

how is the 'this' keyword of javascript is different from 'this' keyword of java?

From Dev

how is the 'this' keyword of javascript is different from 'this' keyword of java?

From Dev

How can I access a value in an object in javascript?

From Dev

Where are scala js dependencies (specified with %%%) downloaded from, and how can I search for them?

From Dev

How we can access a div from JavaScript?

From Dev

How can I access args from trait?

From Dev

How can I access the attribute from the template?

From Dev

How can I access ObservableCollection from a DataTemplate

From Dev

How can I access the attribute from the template?

From Dev

How I can access instance from MethodInfo

From Dev

How can I access the gyroscope from onResume?

From Dev

How I can access the smb from linux?

From Dev

Why can't I access properties of an anonymous type returned from a function via the dynamic keyword?

From Java

How can you call JavaScript builtins from Perl 6 with the new JS backend?

From Dev

JS: How can I add items to an new Objects array, instantiated from a constructor?

From Dev

How do I access this keyword In a different class

From Dev

How do I access this keyword In a different class

Related Related

  1. 1

    How can I access a method defined with new keyword in a derived class

  2. 2

    GWT: How can I access its webservice from pure js?

  3. 3

    How can I access a variable in a javascript function from the rest of the script?

  4. 4

    How can I access a list from a controller in JavaScript?

  5. 5

    How can I access the default value for a keyword argument in Python?

  6. 6

    Why I can't access a JavaScript function defined in a .js file from another .js file?

  7. 7

    How can I get Javascript Source(js file) from url?

  8. 8

    How can I access iPhone's new 3D touch in Safari with Javascript?

  9. 9

    How can I only show the exact lines from a file with a keyword?

  10. 10

    How can I have keyword argument from string parameter?

  11. 11

    how can I access functions from another program into my new program

  12. 12

    Can I inherit access modifiers from super class in Scala?

  13. 13

    how is the 'this' keyword of javascript is different from 'this' keyword of java?

  14. 14

    how is the 'this' keyword of javascript is different from 'this' keyword of java?

  15. 15

    How can I access a value in an object in javascript?

  16. 16

    Where are scala js dependencies (specified with %%%) downloaded from, and how can I search for them?

  17. 17

    How we can access a div from JavaScript?

  18. 18

    How can I access args from trait?

  19. 19

    How can I access the attribute from the template?

  20. 20

    How can I access ObservableCollection from a DataTemplate

  21. 21

    How can I access the attribute from the template?

  22. 22

    How I can access instance from MethodInfo

  23. 23

    How can I access the gyroscope from onResume?

  24. 24

    How I can access the smb from linux?

  25. 25

    Why can't I access properties of an anonymous type returned from a function via the dynamic keyword?

  26. 26

    How can you call JavaScript builtins from Perl 6 with the new JS backend?

  27. 27

    JS: How can I add items to an new Objects array, instantiated from a constructor?

  28. 28

    How do I access this keyword In a different class

  29. 29

    How do I access this keyword In a different class

HotTag

Archive