Here is the modular way of js literal object. In the 'display' function, 'this' refers normally. But in 'addEvent' this refers to something else. Since the object where the click event occurred becomes 'this', this pointing to a member function cannot be used. How can I solve this problem?
let s_text = {};
var currentPanel = function(){
var prefix = '';
var subtext = '';
return {
setText: function(text){
subtext = text;
},
getText: function(){
return subtext;
},
display: function(text){
this.setText(text);
return '<span>'+subtext+'</span>'
},
addEvent: function(){
document.getElementById('canvas'+id).onclick = function(){
let args = {};
var id = $(this).attr('data-root');
args['page' = $(this).attr('data-page');
args['c_id'] = $(this).attr('data-ole');
args['panel'] = $(this).attr('data-target');
if(this.getText(args['page'])){
/*****************************
* here, 'This' is not object
******************************/
args['length'] = this.getText(args['page']).length;
}
return args;
}
}
}
}
var foo = Object.create(currentPanel());
s_text = foo.addEvent();
Via Active questions tagged javascript - Stack Overflow https://ift.tt/iKLH3bx
Comments
Post a Comment