Situation
I was putting together a widget when I started to analyze a possible error, because the tests actually occurred:
Test
document.getElementById('teste').addEventListener('click', function(){
n.p.call(this);
setTimeout(function(){
document.getElementById('result').innerHTML = 'FAIL';
n.q.call(this);
},2000);
});
var n = {
options : {
move : true,
},
p : function(){
console.log(this);
document.getElementById('result').innerHTML = this;
},
q : function(){
if(this.options.move === true){
console.log()
document.getElementById('result').innerHTML = 'OK';
}
}
}
n.p();
setTimeout(function(){
n.q();
},2000);
<p id="teste">
teste
</p>
<div id="result">
</div>
Description
The n methods should at first be called via np () , ie function, however I remembered the call method and apply , which change the this , to the calling object.Doubt
- Should I always do objectName.var instead of this.var ?