此刻我走到了這一步。如何在JavaScript中調用當前對象的公共方法?
function Class() {
var privateMethod = function() {
return 'private'
}
this.publicMethod = function() {
return 'public'
}
var _constructor = function() {
$(document).on('click', _onClick)
}
var _onClick = function() {
// My error is `this`, focus now on the click event, but I need the object itself
console.log(privateMethod())
console.log(this.publicMethod())
}
_constructor()
}
$(document).ready(init)
function init() {
new Class()
}
問題是,在點擊事件中,我無法調用publicMethod。 我可以調用私有方法。
我該如何做到這一點?
也許你應該嘗試javascript模塊模式。 http://briancray.com/posts/javascript-module-pattern – pbaris 2013-03-13 14:56:09
這看起來很混亂。但我會試試看,謝謝! – 2013-03-13 15:02:26