3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

haxeで作ったjsに外部からアクセスする

Last updated at Posted at 2014-10-17

haxeでjsを書き出した際、デフォルトのコンパイルでは
無名関数によってラップされるので外部から参照できない。
参照したいときは、下記のような方法がある。

【方法1】コンパイルオプションをclassicにする

-D js-classic
こうすると無名関数でラップされなくなるが
グローバルな変数が汚染されてしまい、恐ろしい。
http://qiita.com/k-motoyan/items/d9c516bba525379d3cda#2-2

【方法2】exposeを使う

class表記の上に@:exposeを書くと、そのクラス参照だけグローバルになる。
下記の例だと、MyExternalClassクラスが、外部からはnew someLib.SomeName();みたいに使えるようになる。

expose.hx
@:expose("someLib.SomeName")
class MyExternalClass { }

方法1よりこちらの方法がよいだろう。

参考

javascriptで名前空間を有効活用する方法
http://tech.kayac.com/archive/javascript-variable-scope.html
名前空間を閉じるテクニックが載っている。勉強になる。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?