为 XML 原型函数定义 JavaScript 的原型属性

发布于 2024-11-16 18:25:24 字数 2339 浏览 4 评论 0原文

我正在使用此链接 (http://km0.la/js/mozXPath/) 提供的自定义 JavaScript 函数来在 FireFox 中实现特定的 XML 功能。

代码如下:

// mozXPath
// Code licensed under Creative Commons Attribution-ShareAlike License 
// http://creativecommons.org/licenses/by-sa/2.5/
if( document.implementation.hasFeature("XPath", "3.0") ) {
    if( typeof XMLDocument == "undefined" ) { XMLDocument = Document; }
    XMLDocument.prototype.selectNodes = function(cXPathString, xNode) {
        if( !xNode ) { xNode = this; }
        var oNSResolver = this.createNSResolver(this.documentElement);
        var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                     XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
        var aResult = [];
        for( var i = 0; i < aItems.snapshotLength; i++) {
            aResult[i] = aItems.snapshotItem(i);
        }
        return aResult;
    }
    XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) {
        if( !xNode ) { xNode = this; } 
        var xItems = this.selectNodes(cXPathString, xNode);
        if( xItems.length > 0 ){ return xItems[0]; }
        else{ return null; }
    }
    Element.prototype.selectNodes = function(cXPathString) {
        if(this.ownerDocument.selectNodes) { 
            return this.ownerDocument.selectNodes(cXPathString, this);
        }
        else { throw "For XML Elements Only"; }
    }
    Element.prototype.selectSingleNode = function(cXPathString) {   
        if(this.ownerDocument.selectSingleNode) {
            return this.ownerDocument.selectSingleNode(cXPathString, this);
        }
        else { throw "For XML Elements Only"; }
    }
}

假设 XML 对象已定义并加载了 XML 内容,下面是如何访问名为“cd_rank”的 XML 标记的示例:

var cd_rank_XMLObj = XMLObj.selectSingleNode("cd_rank");

我想要做的是将属性“nodeTypedValue”添加到selectSingleNode() 函数,但我不知道如何执行此操作。在 Element.prototype.selectSingleNode 函数中,我尝试添加:

this.prototype.nodeTypedValue = this.textContent;

但是,它给了我一个错误,说它未定义。我什至尝试将它添加到函数之外,只是为了简化它并获得概念,它还说它是未定义的:

var XMLObj.selectSingleNode.prototype.nodeTypedValue = XMLObj.textContent;
alert(XMLObj.selectSingleNode("cd_rank").nodeTypedValue);

我想,本质上我想做的是向原型函数添加原型属性。但我需要一些帮助。如何添加“nodeTypedValue”以便编写“XMLObj.selectSingleNode(Path).nodeTypedValue”?

I am using custom javascript functions provided at this link (http://km0.la/js/mozXPath/) to implement particular XML functionality in FireFox.

Here is the code:

// mozXPath
// Code licensed under Creative Commons Attribution-ShareAlike License 
// http://creativecommons.org/licenses/by-sa/2.5/
if( document.implementation.hasFeature("XPath", "3.0") ) {
    if( typeof XMLDocument == "undefined" ) { XMLDocument = Document; }
    XMLDocument.prototype.selectNodes = function(cXPathString, xNode) {
        if( !xNode ) { xNode = this; }
        var oNSResolver = this.createNSResolver(this.documentElement);
        var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                     XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
        var aResult = [];
        for( var i = 0; i < aItems.snapshotLength; i++) {
            aResult[i] = aItems.snapshotItem(i);
        }
        return aResult;
    }
    XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) {
        if( !xNode ) { xNode = this; } 
        var xItems = this.selectNodes(cXPathString, xNode);
        if( xItems.length > 0 ){ return xItems[0]; }
        else{ return null; }
    }
    Element.prototype.selectNodes = function(cXPathString) {
        if(this.ownerDocument.selectNodes) { 
            return this.ownerDocument.selectNodes(cXPathString, this);
        }
        else { throw "For XML Elements Only"; }
    }
    Element.prototype.selectSingleNode = function(cXPathString) {   
        if(this.ownerDocument.selectSingleNode) {
            return this.ownerDocument.selectSingleNode(cXPathString, this);
        }
        else { throw "For XML Elements Only"; }
    }
}

Assuming the XML object has been defined and loaded with XML content, here is an example of how one would access a an XML tag named "cd_rank":

var cd_rank_XMLObj = XMLObj.selectSingleNode("cd_rank");

What I want to do is add the property "nodeTypedValue" to the selectSingleNode() function, but I'm not sure how to do this. In the Element.prototype.selectSingleNode function, I tried adding:

this.prototype.nodeTypedValue = this.textContent;

However, it's giving me an error saying it's undefined. I even tried adding it outside of the function, just to dumb it down and get the concept, and it also says it's undefined:

var XMLObj.selectSingleNode.prototype.nodeTypedValue = XMLObj.textContent;
alert(XMLObj.selectSingleNode("cd_rank").nodeTypedValue);

Essentially what I'm trying to do, I suppose, is add a prototype property to a prototype function. But I need some help with this. How can i add "nodeTypedValue" such that I write "XMLObj.selectSingleNode(Path).nodeTypedValue"?

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

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

发布评论

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

评论(1

夏日浅笑〃 2024-11-23 18:25:24

好吧,我想我知道如何将它添加到函数中,这可能更多是由于运气而不是逻辑:

Element.prototype.selectSingleNode = function(cXPathString){    
    if(this.ownerDocument.selectSingleNode) {
        var result = this.ownerDocument.selectSingleNode(cXPathString, this);
        if (result != null) {
            result.nodeTypedValue = result.textContent;
        }
        return result;
    }
    else{throw "For XML Elements Only";}
}

Okay, I think I figured out how to add it inside the function, probably due more to luck than logic:

Element.prototype.selectSingleNode = function(cXPathString){    
    if(this.ownerDocument.selectSingleNode) {
        var result = this.ownerDocument.selectSingleNode(cXPathString, this);
        if (result != null) {
            result.nodeTypedValue = result.textContent;
        }
        return result;
    }
    else{throw "For XML Elements Only";}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文