.responseXML() 返回什么对象?

发布于 2024-12-13 18:05:03 字数 581 浏览 1 评论 0原文

我正在尝试创建一个继承自特定函数返回的对象的原型/类。但我不知道该对象的名称是什么?

对于实例 var xhr = XMLHttpRequest(); xhr.responseXML; 返回什么对象?是XMLDocument吗?或者XMLDOM
另外,如果我创建对象 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 并调用 xmlDoc.load("xmlFile.xml" ); 它将返回与 xhr.responseXML; 相同类型的对象?

我正在尝试执行以下操作:

function XMLHandler()
{
   this.xmlFile = "defaultXML.xml";
}

// Make XMLHandler inherit from Javascript object
XMLHandler.prototype = new XMLDocument();
XMLHandler.prototype.constructor = XMLDocument;

I am trying to create a prototype/class that inherits from the object that is returned by a specific function. But I dont know what that object's name is?

For the instance var xhr = XMLHttpRequest(); what object is returned by xhr.responseXML;? Is it XMLDocument? Or maybe XMLDOM?
Also if I create the object var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); and call xmlDoc.load("xmlFile.xml" ); will it return the same type of object as xhr.responseXML;?

I am trying to do the following:

function XMLHandler()
{
   this.xmlFile = "defaultXML.xml";
}

// Make XMLHandler inherit from Javascript object
XMLHandler.prototype = new XMLDocument();
XMLHandler.prototype.constructor = XMLDocument;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏林苍穹下 2024-12-20 18:05:03

您不应在同一篇文章中包含不相关的问题。要解决有关在“类”中声明函数的第二个问题:

您的第一个语法,在构造函数中声明它,将毫无意义地覆盖 myClass.prototype.publicFunct 以指向一个新创建的函数,每次构造函数被调用。也就是说,您将在每次调用构造函数时创建该函数的额外副本,并且 myClass.prototype.publicFunct 始终指向最近创建的副本 - 或者在调用构造函数之前未定义至少一次。不要这样做。

你的第二个选择是 JavaScript 中的语法无效。

首选您的第三种语法。这会将函数添加到原型一次

请记住:JavaScript 本身没有类,但如果您认为值得麻烦的话,您可以模拟它们。

You shouldn't include unrelated questions in the same post. To address your second question about declaring a function within a "class":

Your first syntax, declaring it within the constructor, will pointlessesly overwrite myClass.prototype.publicFunct to point to a newly created function every time the constructor is called. That is, you will be creating an extra copy of the function with each call to the constructor, with myClass.prototype.publicFunct always pointing to the most recently created copy - or undefined until the constructor has been called at least once. Don't do this.

Your second option is simply invalid syntax in JavaScript.

Your third syntax is prefered. This will add the function to the prototype once.

Remember: JavaScript doesn't have classes as such, though you can sort of simulate them if you think it is worth the bother.

小情绪 2024-12-20 18:05:03

从你最近的问题我可以看出你的思维方式像Java,但事实并非如此。

第一个问题:
responseXML 对于每个浏览器都是不同的。 Firefox 给出一个 nsIDOMDocumentIE 给出IXMLDOMDocument 和 Webkit 浏览器依赖于 responseType 设置,但可能是文档。既然你无法预测它会是什么,就停止尝试扩展它。在大多数情况下,浏览器的 API 无法提供该类型,因此 javascript 无论如何都无法扩展它。

此外,由于 JavaScript 的继承不是基于类的,因此您被迫这样做:

XMLHandler.prototype = new XMLDocument();

...这根本不符合您的目的。 XMLHandler 的任何实例都将构建在不相关的空文档上,而不是 responseXML 返回的文档上。你必须在这里使用包装器。


第二个问题:
在您的 3 个方法中,第一个方法与最后一个方法等效,但更浪费,因为它重复将相同的函数设置为相同的原型。第二个是无意义的,语法被破坏了。这些是您真正的选择:

// Instance method, every instance is given a copy of the function upon "new"
function MyClass()
{
    this.publicFunct = function()
    {
        alert("public function");
    };
}

// Prototypal method, only one copy of the function shared by all instances
function MyClass()
{
}

MyClass.prototype.publicFunct = function()
{
    alert("public function");
};

// Shorthand method, same as prototypal but handy for several members at once
// It prevents MyClass from being a descendent of another type
function MyClass()
{
}

MyClass.prototype = {
    // A colon is only acceptable in object notation
    publicFunct: function()
    {
        alert("public function");
    }
};

为了提高效率,我会使用原型方法,除非您需要有选择地向类添加函数。您对“公共函数”(也称为“类”)的使用似乎是 OOP 背景的另一个症状,JavaScript 中没有任何私有函数,因此“公共”没有位置,所有成员函数都是公共的。如果在某些时候你确实需要一个私有函数,你可以用闭包来伪造效果。

(function() {

    // Assignments are mostly global
    MyClass = function() {};

    MyClass.prototype.publicFunct = function()
    {
        privateFunct();
    };

    // These statements affect local scope
    var foo = 'bar';

    function privateFunct()
    {
        alert("public function");
    }

})(); // These extra brackets cause the contents to be executed immediately

话虽如此,很少需要私有函数,而且所有 JavaScript 都是可见的,所以它实际上并不是秘密。上面的内容可能会受到这样的阻碍:

thief = {};
MyClass.prototype.publicFunct.call(thief);
// privateFunct is called by publicFunct in the context of the thief

您不妨接受函数是公共的。你可以更进一步,完全放弃课程。对象只是碰巧具有某些功能的对象,这些功能甚至可以与完全不同的对象共享。

I can tell by your recent questions that you are thinking like Java, which this is not.

First question:
responseXML is different for each browser. Firefox gives a nsIDOMDocument, IE gives an IXMLDOMDocument and Webkit browsers depend on the responseType setting but will probably be Document. Since you cannot predict what it will be stop trying to extend it. In most cases the type isn't made available by the browser's API so javascript cannot extend it anyway.

Moreover, since JavaScript's inheritance is not class based you are forced into doing this:

XMLHandler.prototype = new XMLDocument();

...which simply does not work for your purpose. Any instance of XMLHandler will be built on an unrelated, empty document and not the document returned by responseXML. You have to use a wrapper here.


Second Question:
Of your 3 methods the first is equivalent to the last but more wasteful because it repeatedly sets the same function to the same prototype. The second is nonsensical, the syntax is broken. These are your real options:

// Instance method, every instance is given a copy of the function upon "new"
function MyClass()
{
    this.publicFunct = function()
    {
        alert("public function");
    };
}

// Prototypal method, only one copy of the function shared by all instances
function MyClass()
{
}

MyClass.prototype.publicFunct = function()
{
    alert("public function");
};

// Shorthand method, same as prototypal but handy for several members at once
// It prevents MyClass from being a descendent of another type
function MyClass()
{
}

MyClass.prototype = {
    // A colon is only acceptable in object notation
    publicFunct: function()
    {
        alert("public function");
    }
};

I would go with a prototypal method for efficiency unless you need to selectively add functions to a class. Your use of "public function" (also "class") seems like another symptom of an OOP background, there aren't any private functions in JavaScript so "public" has no place, all member functions are public. If at some point you do need a private function you can fake the effect with a closure.

(function() {

    // Assignments are mostly global
    MyClass = function() {};

    MyClass.prototype.publicFunct = function()
    {
        privateFunct();
    };

    // These statements affect local scope
    var foo = 'bar';

    function privateFunct()
    {
        alert("public function");
    }

})(); // These extra brackets cause the contents to be executed immediately

Having said that it is rare to need private functions and all JavaScript is visible anyway so it's not secret really. The above could be thwarted like this:

thief = {};
MyClass.prototype.publicFunct.call(thief);
// privateFunct is called by publicFunct in the context of the thief

You might as well accept that functions are public. You can go a step further and give up on classes altogether. Objects are just objects that happen to have some functions and those functions can even be shared with completely different objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文