0
當用戶點擊一個從我的Class中調用Function的按鈕時,我需要調用一些私有變量。Javascript:從「private Method」調用「pubilc Variable」
這裏是我的代碼: -
class01 = new MyClass('Tom Marvolo Riddle');
function MyClass(name){
this.name = name;
var draw = function(){
var newHTML ='<input type="button" value="hello" />';
$(".ctn").append(function(){
return $(newHTML).click(hello);
});
}
var hello = function(){
alert ('hello, my name is '+this.name+'.')
}
draw();
}
您通過引入新的範圍丟失的上下文。緩存'this'例如:'var self = this'然後使用'self'。 – elclanrs
或者,嘗試'var hello = function(){alert('hello,我的名字是'+ this.name +'。'); } .bind(this);' - bind保持上下文。 –
@BenjaminGruenbaum發表回覆? –