Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
Scala.js | Use JavaScript object as function parameter
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="MayoMayn" data-source="post: 429380" data-attributes="member: 71840"><p>So basically I'm exporting a top level object in Scala.js where the <strong>start</strong> method takes in a JS object as function argument.</p><p>After some <a href="https://stackoverflow.com/questions/38891704/javascript-object-as-function-parameter?rq=1" target="_blank">research</a> I discovered that defining a trait for the object would solve the issue, but all I get is still "[Object object]"</p><p></p><p>This is my <strong>main.scala</strong> file:</p><p>[PHP]</p><p>package stormengine</p><p></p><p>import scala.scalajs.js.annotation._</p><p>import org.scalajs.dom</p><p></p><p>import scala.scalajs.js</p><p></p><p>@js.native</p><p>trait Config extends js.Object {</p><p> val host: js.UndefOr[String] = js.native</p><p>}</p><p></p><p>@JSExportTopLevel("StormEngine")</p><p>object StormEngine {</p><p></p><p> @JSExport</p><p> def start(config: Config): Unit = {</p><p> println(config)</p><p> }</p><p>}</p><p>[/PHP]</p><p></p><p>Then from my <strong>index.html</strong> I call the method:</p><p>[PHP]</p><p><!DOCTYPE html></p><p><html></p><p><head></p><p> <title>Example Scala.js application</title></p><p> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></p><p> <script type="text/javascript" src="http://localhost:12345/workbench.js"></script></p><p></head></p><p><body style="margin: 0px"></p><p></p><p><div id="game" /></p><p></p><p><script type="text/javascript" src="../storm-engine-fastopt.js"></script></p><p></p><p><script></p><p> StormEngine.start({</p><p> host: '127.0.0.1'</p><p> });</p><p></script></p><p></body></p><p></html></p><p>[/PHP]</p><p></p><p>Any clue how to solve this?</p><p>[doublepost=1519940397,1519940040][/doublepost]My bad it actually did work.</p><p>You just cant println the JS object and see the object structure, but when I logged "config.host" instead it did show "127.0.0.1"</p></blockquote><p></p>
[QUOTE="MayoMayn, post: 429380, member: 71840"] So basically I'm exporting a top level object in Scala.js where the [B]start[/B] method takes in a JS object as function argument. After some [URL='https://stackoverflow.com/questions/38891704/javascript-object-as-function-parameter?rq=1']research[/URL] I discovered that defining a trait for the object would solve the issue, but all I get is still "[Object object]" This is my [B]main.scala[/B] file: [PHP] package stormengine import scala.scalajs.js.annotation._ import org.scalajs.dom import scala.scalajs.js @js.native trait Config extends js.Object { val host: js.UndefOr[String] = js.native } @JSExportTopLevel("StormEngine") object StormEngine { @JSExport def start(config: Config): Unit = { println(config) } } [/PHP] Then from my [B]index.html[/B] I call the method: [PHP] <!DOCTYPE html> <html> <head> <title>Example Scala.js application</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <script type="text/javascript" src="http://localhost:12345/workbench.js"></script> </head> <body style="margin: 0px"> <div id="game" /> <script type="text/javascript" src="../storm-engine-fastopt.js"></script> <script> StormEngine.start({ host: '127.0.0.1' }); </script> </body> </html> [/PHP] Any clue how to solve this? [doublepost=1519940397,1519940040][/doublepost]My bad it actually did work. You just cant println the JS object and see the object structure, but when I logged "config.host" instead it did show "127.0.0.1" [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
Scala.js | Use JavaScript object as function parameter
Top