2016-01-20 97 views
2

我想端口一些代码从JSHaxe是否可以重写Haxe中的js.html.Window类的函数?

的代码覆盖Window.getComputedStyle到workarround一个Firefox的问题(Bugzilla link):

window.oldGetComputedStyle = window .getComputedStyle; 
    window.getComputedStyle = function (element, pseudoElt) { 
     var t = window.oldGetComputedStyle(element, pseudoElt); 
     if (t === null) { 
     return {}; 
     } else{ 
     return t; 
     } 
    }; 

我该如何解决这个问题?

当我想,我得到了以下错误:

Cannot rebind this method : please use 'dynamic' before method declaration 
+0

问题你问需要澄清。 “我可以使用Haxe覆盖这个功能吗?”是或不是。改变你的问题,要求澄清你的错误将有助于你获得更好的信息。 – Nathan

回答

2

我已经找到了解决办法。 我需要的Window指定为untyped绕过编译器错误:

iframe = cast Browser.document.getElementById("iframe"); 
var window = untyped iframe.contentWindow; 
var oldGetComputedStyle = window.getComputedStyle; 
window.getComputedStyle = function (element, pseudoElt) { 
    var t = oldGetComputedStyle(element, pseudoElt); 
    if (t == null) { 
     return {}; 
    } else{ 
     return t; 
    } 
} 
+1

你也可以只用'untyped'作为前缀加以必要的赋值:'untyped window.getComputedStyle = function(element,pseudoElt){...'。这样会好一点,因为它限制了非类型化和不安全块的范围(并且你的'window'变量可以保持输入状态)。有关完整示例,请选中[this](http://try.haxe.org/#164F3)_Try Haxe_链接。 –

+0

良好的建议,并感谢您_Try Haxe_链接!很有用! –

相关问题