简体   繁体   English

我收到未捕获的TypeError错误?

[英]I'm getting an error of Uncaught TypeError?

I am creating a simple quiz app but I am stuck at making an object constructor the error is Uncaught TypeError: Question.pushQuestion is not a function 我正在创建一个简单的测验应用程序,但我坚持使对象构造函数错误为未捕获的TypeError:Question.pushQuestion不是函数

function Question(question){
    this.question = [];
}

Question.prototype.pushQuestion = function(question){
    this.question.push(question);
}

var question1 = Question("is 1 = 1 ?");
Question.pushQuestion(question1);//error
var question1 = new Question("is 1 = 1 ?");

You've missed the new keyword. 您错过了new关键字。

Also, here you create instance of your object: 另外,在这里创建对象的实例:

var question1 = Question("is 1 = 1 ?");

So, you should use question1 in order to access any method from prototype chain, eg 因此,您应该使用question1来访问原型链中的任何方法,例如

question1.pushQuestion('How are you doing?');

When you define a function on an object prototype, you can invoke it by calling the function on a new-ed up instance of that object. 在对象原型上定义函数时,可以通过在该对象的新实例上调用该函数来调用它。

function Question(question){
    this.question = [];
}

Question.prototype.pushQuestion = function(question){
    this.question.push(question);
}

var question1 = new Question("is 1 = 1 ?");
question1.pushQuestion(question1);

What are you trying to achieve? 您想达到什么目的? Because I'm pretty sure you don't want to push the question itself into question.list . 因为我很确定您不想将问题本身推送到question.list

It looks like you're trying to generate a list of questions. 您似乎正在尝试生成问题列表。 I would approach like this: 我将这样处理:

function QuestionList() {
    this.questions = [];
    this.counter = 0;
}

QuestionList.prototype.pushQuestion = function (question) {
    this.questions.push(question);
}

var questionList = new QuestionList();

questionList.pushQuestion("is 1 = 1 ?");
questionList.pushQuestion("is 1 = 2 ?");

questionList.questions // [ "is 1 = 1 ?", "is 1 = 2 ?" ]

You can then write a new method to get the next question in the list: 然后,您可以编写一个新方法来获取列表中的下一个问题:

QuestionList.prototype.getNextQuestion = function (question) {
    return this.questions[this.counter++];
}

questionList.getNextQuestion(); // "is 1 = 1 ?"
questionList.getNextQuestion(); // "is 1 = 2 ?"

DEMO DEMO

暂无
暂无

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

相关问题 我收到此错误Uncaught TypeError:this.getElements不是一个函数 - I'm getting this error Uncaught TypeError: this.getElements is not a function 我收到错误Uncaught TypeError:无法读取未定义的属性'drawBox',有人可以发现问题吗? - I'm getting the error Uncaught TypeError: Cannot read property 'drawBox' of undefined, Can anyone spot the issue? 我在 axios 补丁 api 中收到“Uncaught (in promise) TypeError: Cannot read property 'data' of undefined”错误 - I'm getting “Uncaught (in promise) TypeError: Cannot read property 'data' of undefined” error in axios patch api 为什么会出现“未捕获的TypeError”错误? - Why am I getting 'Uncaught TypeError' error? 代码运行正常,但为什么我在控制台出现错误,Uncaught TypeError: Cannot read properties of null (reading 'style')? - Code is working and runs fine, but why I'm getting error at console , Uncaught TypeError: Cannot read properties of null (reading 'style')? 我正在使用focusin / focusout,并且得到:未捕获的TypeError:无法读取null的属性'addEventListener' - I'm using focusin/ focusout and I'm getting: Uncaught TypeError: Cannot read property 'addEventListener' of null 我正在尝试检索表数据,但出现此错误: Uncaught TypeError: result.rows.product is not a function at products.html:134 - I'm trying to retrieve table data and i'm getting this error: Uncaught TypeError: result.rows.product is not a function at products.html:134 Javascript:我收到错误“Uncaught SyntaxError:Unexpected token {” - Javascript: I'm getting the error “Uncaught SyntaxError: Unexpected token { ” 我收到了Uncaught TypeError:无法在jquery.main.js上读取null的属性“ length” - I'm getting an Uncaught TypeError: Cannot read property 'length' of null on jquery.main.js 我收到“未捕获的类型错误:无法设置未定义的属性“值”,但我的代码仍然有效 - I'm getting 'Uncaught TypeError: Cannot set property 'value' of undefined' but my code still works
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM