简体   繁体   English

如何在javascript中使用类

[英]How to use classes in javascript

Hi i am continue getting undefined error in alert when i am trying to access my class veriable from out side of class, i have made a main class and then use prototype to add some object into my class which i want to access from anywhere by creating new class please help me 嗨,当我尝试从类的外部访问我的类时,我继续收到警报中未定义的错误,我制作了一个主类,然后使用原型将一些对象添加到我的类中,我想通过创建从任何地方访问新班请帮帮我

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

Try like this 这样尝试

Edit : 编辑:

Working demo 工作演示

// 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM