如何在Javascript中正确访问类属性

Jos*_*eau 1 javascript jquery object

我有这个类定义:

$.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;
  }
}
Run Code Online (Sandbox Code Playgroud)

我用这个类创建了一个新对象,如下所示:

var note = new $.note('C');
Run Code Online (Sandbox Code Playgroud)

我可以在我的控制台中访问,如下所示: 注意

但是当我尝试访问note.getNote()时,我得到了未定义的响应: 在此输入图像描述

我是不是要错误地访问这些属性?我尝试过只使用note.note或note.ctrl,我得到同样的东西......

Poi*_*nty 7

如果不这样做,没有什么能够称之为"init"功能.

$.note = function(note) { this.init(note); }
Run Code Online (Sandbox Code Playgroud)

有些框架提供了一个使用类似构造函数辅助函数的对象系统,但普通的JavaScript却没有.