1

I have this class definition:

$.note = function() {}
$.note.prototype = {
  init: function(note) {
    this.note = note;
    this.ctrl = document.getElementById(note);
  },
  // I have these getter functions because I was getting errors using
  // myObject.note or myObject.ctrl
  getNote: function() {
    return this.note;
  },
  getCtrl: function() {
    return this.ctrl;
  }
}

I created a new object with this class like this:

var note = new $.note('C');

Which I can access in my console like this: Note

But when I try and access note.getNote(), I get undefined as the response: enter image description here

Am I going about accessing these properties incorrectly? I've tried using just note.note or note.ctrl, and I get the same thing...

2
  • 1
    I don'nt see you calling the init function Commented Dec 21, 2013 at 16:14
  • The code you are putting in your init prototype is typically code you’d want in your constructor. Commented Dec 21, 2013 at 16:29

2 Answers 2

7

Nothing's going to call that "init" function if you don't.

$.note = function(note) { this.init(note); }

Some frameworks provide an object system that uses constructor helper functions like that, but plain JavaScript doesn't.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

$.note = function(note) { this.note = note;}

or you should call init function:

var note = new $.note();
note.init('C');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.