2011-11-06 27 views
0

我有一個從ActiveXObject繼承的Javascript對象/類。但是當我在Internet Explorer(版本8)中運行代碼時,出現這個奇怪的錯誤。Javascript Prototype繼承自ActiveXObject導致Internet Explorer中的錯誤

的錯誤是:「對象不支持此屬性或方法」

你能告訴我是什麼錯誤意味着&如何解決這個問題?

我的代碼是:

function XMLHandler(xmlFilePath) 
    { 
    this.xmlDoc = null; 
    this.xmlFile = xmlFilePath; 
    this.parseXMLFile(this.xmlFile); 

    this.getXMLFile = function() 
    { 
     return this.xmlFile; 
    } 
    } 

    XMLHandler.prototype    = new ActiveXObject("Microsoft.XMLDOM"); 
    XMLHandler.prototype.constructor = ActiveXObject;   // Error occurs here in IE. The error is: "Object doesn't support this property or method" 
    XMLHandler.prototype.parseXMLFile = function(xmlFilePath) // If I comment out the above line then the exact same error occurs on this line too 
    { 
    this.xmlFile = xmlFilePath; 
    this.async="false"; // keep synchronous for now 
    this.load(this.xmlFile); 
    } 
+0

什麼版本的IE? – SLaks

+0

@SLaks我的版本是IE 8 –

回答

1

的錯誤是很obvouis給我。你在做什麼:

var x = new ActiveXObject("Microsoft.XMLDOM"); 
x.extendIt = 42; 

它拋出一個(神祕)錯誤,說你不能用一個新的屬性擴展一個ActiveXObject的實例。

現在ActiveXObject是一個主機對象,並且他們被稱爲充滿未定義的行爲。不要擴展它。請使用它。

var XMLHandler = { 
    XMLDOM: new ActiveXObject("Microsoft.XMLDOM"), 
    parseXMLFile: function(xmlFilePath) { 
     this.xmlFile = xmlFilePath; 
     this.XMLDOM.load(xmlFilePath); 
    }, 
    getXMLFile: function() { 
     return this.xmlFile; 
    } 
}; 

var xmlHandler = Object.create(XMLHandler); 
xmlHandler.parseXMLFile(someFile); 

(我搞掂你的代碼,你將需要對傳統平臺支持的ES5墊片)。

當然,如果你現在看你的代碼,你可以看到你無緣無故地爲.load創建了一個代理。您不妨直接使用XMLDOM對象。

+0

是的,你不能擴展(添加屬性)主機對象。 – airportyh

+1

@toby實現特定。您可以在現代瀏覽器中擴展大多數主機對象,並且可以使用Object.defineProperty擴展IE8中的DOM – Raynos