Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript原型继承自ActiveXObject,导致Internet Explorer出错_Javascript_Inheritance - Fatal编程技术网

Javascript原型继承自ActiveXObject,导致Internet Explorer出错

Javascript原型继承自ActiveXObject,导致Internet Explorer出错,javascript,inheritance,Javascript,Inheritance,我有一个从ActiveXObject继承的Javascript对象/类。但是当我在InternetExplorer(版本8)中运行代码时,我遇到了这个奇怪的错误 错误是:“对象不支持此属性或方法” 你能告诉我这个错误是什么意思吗?如何修复这个错误 我的代码是: function XMLHandler( xmlFilePath ) { this.xmlDoc = null; this.xmlFile = xmlFilePath; this.parseXMLF

我有一个从ActiveXObject继承的Javascript对象/类。但是当我在InternetExplorer(版本8)中运行代码时,我遇到了这个奇怪的错误

错误是:“对象不支持此属性或方法”

你能告诉我这个错误是什么意思吗?如何修复这个错误

我的代码是:

  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 );
  }

这个错误对我来说很明显。你所做的是:

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对象。

是的,您不能扩展(添加属性)主机对象。@toby特定于实现。您可以在现代浏览器中扩展大多数主机对象,也可以使用
Object.defineProperty