-
• #2
Yes, this is normal.
This looked right to me when I read your question, but didn't really know why so I looked it up. I think this explains it better than I can: https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#the_this_problem -
• #3
yes, it's because
this
is set based on how the function is called.O.M(); // this==O var A = {}; A.M = O.M; A.M(); // this==A var f = O.M(); f(); // this==undefined <- what is happening with setInterval
You can always just do:
setInterval(() => O.M(), 1000)
-
• #4
Thank you myownself and Gordon! Will know.
setInterval(() => O.M(), 1000) - looks cool
-
• #5
Mark_M
this is an alternative of 'wrapping' - .bind() :
const O = { a:"", b:0, m : function(){ this.a = this.a + "*"; this.b = this.b + 1; print(this.a," - ", this.b); } }; var O_M = O.m.bind(O); // binding O as context for 'this' in function / method m // returns a new function with the right context set of this for O. setInterval(O_M,1000);
For
Function.prototype.bind()
see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind (and take also a look at the referenced.apply(...)
).Btw, in JS same upper/lower/camel-casing as Java is useful. Constructor functions - like Classes in Java (and other 'like-minded' languages) - have an uppercase initial.
I do not have much experience with JavaScript (have quite a bit with Java).
It is weird issue from my perspective. If I assign an object's method call to event, it runs in empty object instance. All fields are empty. But if I wrap object's method into a function, and put into event, it runs ok. Is it normal?
Example:
Output of direct method assignment
Output of wrapped method assignment