我試圖創建原型(我認爲是它叫什麼)&在Javascript進行繼承。希望它遵循Javascript的慣例。的JavaScript子類不起作用
應該怎樣改變,因此它的工作原理&所以這就是它的正確JavaScript編碼?代碼不編譯/工作現在
我試圖創建一個跨瀏覽器的XML解析器。所以我嘗試「子類化」XMLDocument對象,雖然我不確定該對象是否存在,但我試圖在調用函數xmlHttp.responseXML時子類化對象(從XMLHttpRequest()對象返回);
一個重要的事情是我要堅持到本地JavaScript &繞開的EcmaScript 5的現在,直到我學會了本地JavaScript的方式來創建&執行的傳承原型。
// I intend to use the XMLHandler object like so:
var xml = XMLHandler("myXMLFile.xml");
var slides = xml.getElementsByTageName("slide");
function XMLHandler(/*string*/ xmlFilePath)
{
this.getXMLFile = function()
{
return this.xmlFile;
}
this.xmlFile = xmlFilePath;
this.parseXMLFile(this.xmlFile);
}
XMLHandler.prototype = new XMLDocument(); // is this enough to make the object inherit from the XMLDocument
XMLHandler.prototype.constructor = XMLDocument; // make XMLHandler call the base class constructor when created
if (window.XMLHttpRequest)
{
XMLHandler.prototype.parseXMLFile = function(xmlFilePath)
{
this.xmlFile = xmlFilePath;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", this.xmlFile, false); //Open the file using the GET routine
xmlHttp.send(null); //Send request
this = xmlHttp.responseXML; //this object holds/is the document information now
}
}
else if (window.ActiveXObject) // if the current browser is an old version of IE
{
XMLHandler.prototype.parseXMLFile = function(xmlFilePath)
{
this.xmlFile = xmlFilePath;
var xmlHttp = new ActiveXObject("Microsoft.XMLDOM");
xmlHttp.async = "false"; // keep synchronous for now
this = xmlHttp.load(this.xmlFile);
}
}
「的代碼不編譯/工作現在」 - 什麼是你有確切的問題?什麼不起作用? –
通過爲'new'關鍵字加上前綴來初始化「類」:var xml = new XMLHandler(「myXMLFile.xml」);'' –