简体   繁体   English

为什么会出现Uncaught TypeError:无法读取未定义的属性'displayQuestion'?

[英]Why am i getting Uncaught TypeError: Cannot read property 'displayQuestion' of undefined?

I tried to find the answer to my problem but really non of the answers i found here helped me. 我试图找到问题的答案,但实际上我在这里找不到的答案对我有帮助。 I know this is commonly asked question but i couldn't find right answer. 我知道这是常见问题,但我找不到正确的答案。 Here is my code: 这是我的代码:

    //Constructor for creating questions
function Question(question, answers, correct) {
    this.question = question;
    this.answers = answers;
    this.correct = correct;
}
//Creating question
var q1 = Question('Is JavaScript the best programming lenguage?', ['Yes', 'No'], 0);
var q2 = Question('What is the name of your teacher?', ['Mike', 'John', 'Jonas'], 2);
var q3 = Question('How would you best describe coding?', ['Boring', 'Fun', 'Tedius', 'Hard'], 1);

//Displaying question and answers
Question.prototype.displayQuestion = function(){

    console.log(this.question);

    for (var i = 0; i < this.answers.length; i++) {
        console.log(i + '. ' + this.answers[i]);
    }
}


//Choosing random question
var questions = [q1, q2, q3];
var n = Math.floor(Math.random() * questions.length);

questions[n].displayQuestion();

I keep getting Uncaught TypeError: Cannot read property 'displayQuestion' of undefined at challenge.js:27 Thanks in advance :) 我不断收到Uncaught TypeError:无法读取Challenge.js中未定义的属性“ displayQuestion”:27提前感谢:)

You are calling Question(...) but you really mean to call new Question(...) . 您正在呼叫Question(...)但实际上是要呼叫new Question(...)

When you call Question(...) without new , it returns undefined because it has no return value (also, this is window instead of a new object). 当你调用Question(...)没有new ,它返回undefined ,因为它没有return值(也, thiswindow ,而不是一个新的对象)。

When you use new , this is set to a newly-created object, and if there's no return value, the new Question call returns the newly-created this object. 当您使用newthis被设置为一个新创建的对象,并且如果没有return值,则new Question调用将返回新创建的this对象。 That's certainly what you expect your code to do. 当然,这就是您期望代码执行的操作。

Question()之前使用new关键字

暂无
暂无

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

相关问题 为什么我会收到Uncaught TypeError:无法在分配时读取未定义的属性“ 0”? - why am I getting Uncaught TypeError: Cannot read property '0' of undefined on assign? 我为什么会收到错误:Uncaught TypeError:无法读取未定义的属性“ left” - Why am I getting the error: Uncaught TypeError: Cannot read property 'left' of undefined 在 Firebase 中,我收到 Uncaught TypeError: Cannot read property &#39;initializeApp&#39; of undefined,但不确定为什么未定义“firebase”? - In Firebase I am getting an Uncaught TypeError: Cannot read property 'initializeApp' of undefined, but not sure why 'firebase' is not defined? 为什么我得到 Uncaught (in promise) TypeError: Cannot read property &#39;type&#39; of undefined? - Why am I getting Uncaught (in promise) TypeError: Cannot read property 'type' of undefined? 为什么我收到此错误:“未捕获的TypeError:无法读取未定义的属性'标题'”? - Why am i getting this error: “Uncaught TypeError: Cannot read property 'Title' of undefined”? 为什么我收到“Select.js:1555 Uncaught TypeError:无法读取未定义的属性‘isSelectOptGroup’” - Why I am getting "Select.js:1555 Uncaught TypeError: Cannot read property 'isSelectOptGroup' of undefined" 为什么我会收到“未捕获的类型错误:无法读取未定义的属性 &#39;body&#39;”? - Why am I getting "Uncaught TypeError: Cannot read property 'body' of undefined"? 为什么我收到此错误:未捕获的TypeError:无法读取undefined的属性&#39;john&#39; - Why am I getting this error: Uncaught TypeError: cannot read property 'john' of undefined 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM