JavaScript Javascript class constructed object is undefined javascript class constructor example,javascript class constructor arguments,javascript class

New with JavaScript. Can someone help me understand why calling print() returns undefined?

class Quizer {
    constructor(quizObj) {
        this.quiz = quizObj;
    }
    print() {
        console.log(quiz.title);
    }
};
var quizObjects = {
    title: "Quiz1"
};

Constructing:

var quiz = new Quizer(quizObjects);
quiz.print(); //undefined
Answer:1

The problems with your code are,

class Quizer {
    constructor(quizObj) {
      this.quiz = quizObj;
    }
    print() {
      console.log(quiz.title);
      //You are not using the `this` context here to access the quiz
      //you have a variable quiz outside the class declaration that points the instance of this class.
     //That will get hoisted and will be accessed here.

    }
};

var quizObjects = { title: "Quiz1" };
var quiz = new Quizer(quizObjects);
quiz.printAllQuestions(); //undefined
//--------^^^^ printAllQuestions is not a member function of Quizer

Solution:

class Quizer {
    constructor(quizObj) {
      this.quiz = quizObj;
    }
    print() {
      console.log(this.quiz.title);
    }
};

var quizObjects = { title: "Quiz1" };
var quiz = new Quizer(quizObjects);
quiz.print(); //Quiz1
Answer:2

I'm inherited an HTML form that uses the js with() function: with (document.images.myImage.style) { left = x + "px"; top = y + "px"; visibility = "visible"; } This code "moves" an ...

I'm inherited an HTML form that uses the js with() function: with (document.images.myImage.style) { left = x + "px"; top = y + "px"; visibility = "visible"; } This code "moves" an ...

  1. javascript with function parameter
  2. javascript with function
  3. javascript with function scope
  4. javascript function with return
  5. javascript function with return value
  6. javascript function with optional parameters
  7. javascript function with arguments
  8. javascript function with callback
  9. javascript function with array parameter
  10. javascript function with string parameter
  11. javascript function with default parameters
  12. javascript function with multiple parameters
  13. javascript function with variable
  14. javascript function with variable arguments
  15. javascript function with object parameter
  16. javascript function with callback parameter
  17. javascript function with return type
  18. javascript function with default value
  19. javascript function with asterisk
  20. javascript function with parameter default value

It's easy to set attribute for a given element: el.setAttribute('rel', 'nofollow'); But for some attributes, such as rel in the example, multiple values are allowed. In case of rel, its values ...

It's easy to set attribute for a given element: el.setAttribute('rel', 'nofollow'); But for some attributes, such as rel in the example, multiple values are allowed. In case of rel, its values ...

  1. multiple values attribute magento
  2. multiple values attribute beautifulsoup
  3. multiple attribute values in xml
  4. multiple attribute values in html
  5. xpath multiple values attribute
  6. multiple values in attribute
  7. css multiple attribute values
  8. multiple values in data attribute
  9. jquery multiple attribute values
  10. xslt multiple attribute values
  11. multiple values in data attribute html
  12. ldap multiple attribute values
  13. multiple values in class attribute
  14. xsl multiple attribute values
  15. multiple values in style attribute
  16. select multiple attributes values
  17. css selector multiple attribute values
  18. xpath select multiple attribute values
  19. jquery selector multiple attribute values
  20. css select multiple attribute values

I just read this SO question/answer and it said to change the color, use console.log("%cYour Message Here", "color:(your color);font-weight:bold;"); I have tested this and messed around with it a ...

I just read this SO question/answer and it said to change the color, use console.log("%cYour Message Here", "color:(your color);font-weight:bold;"); I have tested this and messed around with it a ...

  1. multiple personality eye color change
  2. select2 multiple change color
  3. multiple personality disorder eye color change
  4. change multiple color
  5. mat select multiple change color

I'm using BackboneJS to populate my table with multiple data sources. You do not need to know Backbone to assist in this question, as the issue is mainly a DataTables issue. I initialise my Datatable ...

I'm using BackboneJS to populate my table with multiple data sources. You do not need to know Backbone to assist in this question, as the issue is mainly a DataTables issue. I initialise my Datatable ...