如何在javascript中将Integer转换为int

How to Convert Integer to int in javascript

本文关键字:转换 int Integer 中将 javascript      更新时间:2023-09-26

假设我在GWT(JSNI)中有用Java脚本编写的方法,它接受包装数据类型Integer,并且我需要在JS中转换为Primitive data类型。

    public static native void nativeMethod(Integer index)/*-{
// how to check the null values
//how to convert into primitive type
}-*/;

有什么建议吗?

public static native void nativeMethod(Integer index)/*-{
  if (index == null) {
    // well, index is null…
  } else {
    var i = index.@java.lang.Integer::intValue()();
    // now 'i' is the int value of 'index'
  }
}-*/;
 public static native void nativeMethod(Integer index)/*-{
var converted 
if(index!=null){
    converted = Number(hindeIndex);
    }else{
    converted=null;
    }
}-*/;

这奏效了。。进行了null检查,如果没有,则将其转换为var类型