You are reading a single comment by @allObjects and its replies.
Click here to read the full conversation.
-
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.
Thank you myownself and Gordon! Will know.