如何在javascript中使用类

How to use classes in javascript

本文关键字:javascript      更新时间:2023-09-26

嗨,当我试图从类外访问我的类时,警报中继续出现未定义的错误,我已经创建了一个主类,然后使用原型将一些对象添加到我的类中,我想通过创建新类从任何地方访问这些对象,请帮助我

    function initShell(){
    XMLLoader.prototype.loadXML('xml/scq.xml', initShell.prototype.loadData);
    alert(this.currentQue);
}
initShell.prototype.loadData = function(xmlRef){
    this.currentQue = 0;
    this.pageList = xmlRef.getElementsByTagName("mission");
}
var p1 = new initShell;
alert(p1.currentQue);

像这样尝试

编辑:

工作演示

// Define a class like this
function initShell(currentQue, pageList){
   // Add object properties like this
   this.currentQue= currentQue;
   this.pageList= pageList;
}
// Add methods like this.  All initShell objects will be able to invoke this
initShell.prototype.speak = function(){
    alert("Value of currentQue is : " + this.currentQue);
};
// Instantiate new objects with 'new'
var initShellObj = new initShell("A", "B");
// Invoke methods like this
initShellObj.speak();