簡體   English   中英

如何正確訪問Java類的屬性

[英]How to Properly Access Class Properties in Javascript

我有這個類的定義:

$.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;
  }
}

我用此類創建了一個新對象,如下所示:

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

我可以在控制台中這樣訪問: 注意

但是,當我嘗試訪問note.getNote()時,響應未定義: 在此處輸入圖片說明

我會錯誤地訪問這些屬性嗎? 我已經嘗試只使用note.note或note.ctrl,但我得到了同樣的東西...

如果沒有的話,什么也不會調用該“ init”函數。

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

一些框架提供了使用這樣的構造函數幫助器函數的對象系統,但是普通的JavaScript卻沒有。

嘗試這個:

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

或者您應該調用init函數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM