不能在GWT中使用JS(Harmony)WeakMap:"删除"方法名称被视为关键字?

时间:2014-05-10 13:12:58

标签: javascript gwt

我正在尝试在JavaScript WeakMap上构建一个GWT包装器。 WeakMap API定义了一个名为" delete"的方法。 (例如,参见here)。但是使用此方法会导致GWT中的编译错误:

...
[INFO]          [ERROR] Line 77: missing name after . operator
[INFO] >             this.map.delete(key);
...

它看起来像(对我来说)好像GWT编译器(gwt-maven-plugin v2.6.0)解释了所有出现的单词" delete"作为"关键字"。

我该如何解决这个问题?

...
private native void init()
/*-{
    this.map = new window.WeakMap();
}-*/;

/** Constructor */
public MyWeakKeyMap() {
    init();
}
...
public native VALUE remove(final Object key)
/*-{
    var result = this.map.get(key);
    this.map.delete(key);
    return result;
}-*/;
...

2 个答案:

答案 0 :(得分:2)

我找到了解决它的方法,它在GWT中编译:

private native void init()
/*-{
    this.map = new $wnd.WeakMap();
    this.map.rmv = this.map['delete'];
}-*/;
public native VALUE remove(final Object key)
/*-{
    var result = this.map.get(key);
    this.map.rmv(key);
    return result;
}-*/;

答案 1 :(得分:1)

在GWT JSNI中应该$wnd而不是window

private native void init()
/*-{
   this.map = new $wnd.WeakMap();
}-*/;

或以这种方式尝试

// create a instance variable in your MyWeakKeyMap class for WeakMap 
// instead of creating it in JavaScript itself
protected JavaScriptObject nativeWeakMap;

private native void init()
/*-{
   var theInstance = this;
   theInstance.@com.x.x.z.MyWeakKeyMap::nativeWeakMap = new $wnd.WeakMap();
}-*/;

public native VALUE remove(final Object key)
/*-{
    var theInstance = this;
    var result = theInstance.@com.x.x.z.MyWeakKeyMap::nativeWeakMap.get(key);
    theInstance.@com.x.x.z.MyWeakKeyMap::nativeWeakMap.delete(key);
    return result;
}-*/;