2016-01-20 92 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_鏈接!很有用! –

相關問題