2011-11-06 52 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