GWT到Javascript的转换

LPD*_*LPD 7 javascript gwt

在javascript控制台中,如果我这样做,

   a = [1,2,3]
   Object.prototype.toString.call(a) // gives me "[object Array]"
   typeof a  // gives me "object"
Run Code Online (Sandbox Code Playgroud)

如果我在GWT中创建一个arraylist并将其传递给本机方法并执行此操作,

// JAVA code
   a = new ArrayList<Integer>();
   a.push(1);
   a.push(2);

   //JSNI code
    Object.prototype.toString.call(a) // gives me "[object GWTJavaObject]"
    typeof a // returns "function"
Run Code Online (Sandbox Code Playgroud)

两者之间究竟有什么区别?是GWTJavaObject完全同样

为什么在纯JavaScript中typeof返回" 对象 "而在GWT中返回" 功能 "?

总结问题是,在Javascript中转换为GWT对象究竟是什么?完整代码在这里.

      public void onModuleLoad()
        {
                List<Integer> list = new ArrayList<Integer>();
            list.add( new Integer( 100 ) );
            list.add( new Integer( 200 ) );
            list.add( new Integer( 300 ) );

            Window.alert(nativeMethodCode( list ));
                Window.alert(nativeMethodCode2( list ));
        }

        public static final native Object nativeMethodCode( Object item )
        /*-{
            return Object.prototype.toString.call(item);
        }-*/;

        public static final native Object nativeMethodCode2( Object item )
        /*-{
            return typeof item;
        }-*/;
Run Code Online (Sandbox Code Playgroud)

Tho*_*yer 3

GWT 中的AnArrayList不会转换为纯 JS 数组:它是一个扩展AbstractList和实现一堆接口的类,并且在转换为 JS 时应保留此信息,以便instanceof检查(在 Java 代码中;例如instanceof Listinstanceof RandomAccess)仍然按预期工作。因此AnArrayList被实现为JS 数组的包装器,请参阅https://code.google.com/p/google-web-toolkit/source/browse/tags/2.5.0/user/super/com/google/gwt /emul/java/util/ArrayList.java

请注意,Java 数组被转换为 JS 数组,但要非常小心在 JSNI 中对其执行的操作,因为您可能会进一步破坏 Java 假设(例如,数组具有固定大小)。