I used to get a compiler suggestion about using js.lib.Object instead of js.Object as it is deprecated and would probably be removed from future version.
However I see that syntax of js.lib.Object is not easy.
And there is no sufficient documentation. How can I use it in most compact way?
https://haxefoundation.github.io/hxnodejs/js/lib/Object.html
For example using js.Object was as easy as:
var obj = {a:'hello world'} ;
Vanilla Haxe works as you might hope I think:
https://try.haxe.org/index.php#3b2A7
Ya. I know that js.Object works.
But is js.Object deprecated for future version?
If yes then how to use js.lib.Object?
I am trying to use js.lib.Object , but it shows compiler errors related to wrong syntax.
Sorry if I still may not be understanding you, but you may note that I did not import either of those classes (js.lib.Object or js.Object) in the example. I simply used the syntax you mentioned and Haxe compiled it to a proper JavaScript object. Do you need any of the methods shown in the documentation?
I am noting now that try.haxe.org is not running Haxe 4 yet.
This seems to work in Haxe 4:
class Test {
static function main() {
var obj = new js.lib.Object( {a:'hello world'} );
}
}
Ok. Did you try accessing the value for ‘a’ ?
Try using :
trace( obj.a )
You will get the error
I tried testing each one separately:
package myapp;
import flash.display.*;
import openfl.events.*;
import openfl.utils.Object ;
class Main extends Sprite
{
var stage_St:Stage ;
public var my_Obj:Object;
public function new()
{
super();
my_Obj = {label:"click me", data:"my data on click"};
addEventListener(Event.ADDED_TO_STAGE , ats);
}
function ats(e):Void
{
stage_St = this.stage ;
stage_St.addEventListener(MouseEvent.CLICK, function(e)
{
trace(my_Obj.label,my_Obj.data );
}
);
}
}
When I use js.lib.Object,
my_Obj = new Object( {label:"click me", data:"my data on click"});
the error is :
myapp/Main.hx:32: characters 16-21 : js.lib.Object has no field label
myapp/Main.hx:32: characters 29-33 : js.lib.Object has no field data
When I use js.Object,
my_Obj = new Object( {label:“click me”, data:“my data on click”});
the error is :
myapp/Main.hx:14: characters 21-27 : Warning : This typedef is deprecated in favor of js.lib.Object
myapp/Main.hx:19: characters 16-22 : Warning : This typedef is deprecated in favor of js.lib.Object
myapp/Main.hx:32: characters 16-21 : js.Object has no field label
myapp/Main.hx:32: characters 29-33 : js.Object has no field data
The last thing that seems working is: openfl.utils.Object
my_Obj = {label:“click me”, data:“my data on click”};
Thanks for pressing on with this because I am learning myself. This seems to work:
class Test {
static function main() {
var obj = js.lib.Object.create( {a:'hello world yes'} );
trace (obj.a);
//console will log "hello world yes"
}
}
Yes. Using ‘create()’ should work.
However I think openfl.utils.Object is the right way when using Openfl API.
All the confusion was caused mainly because the HaxeDevelop IDE automatically added the js-package while coding. And from there I was using it all the time thinking it as a part of Openfl API.
1 Like
I tend to use Dynamic
var object:Dynamic = { a: "hello world yes" };
3 Likes