Most of the time, we don't know what is "this"? Is it a "window", jQuery object, or what is it. It is difficult to debug and make the code to work.
One of the easiest solution that I found is to capture the context into "self" and then all public functions to be declared with the object function. For example, sayHelo() will always referring to the same context as you call the setMyName().
function myClass() {
var self = this;
var name;
this.setMyName = function (s) { name = s; };
this.getMyName = function() { return name; };
this.sayHelo = function() { return self.getMyName(); };
}
var c = new myClass();
c.setMyName('Mickey');
console.log(c.sayHelo());
Wednesday, 12 July 2017
Subscribe to:
Comments (Atom)
We are moving
We are moving this blog to our new blog site: https://ciysys.com/blog/nodejs.htm
-
Overview Usually, we start developing a new system with designing database and then writing codes. If you are not a fan of ORM, you will h...
-
Below is the sample HTML which will hide the URL at the page header when printing with Javascript. <html moznomarginboxes > ...
-
You can draw a simple pie chart & doughnut chart with SVG + CSS. This is quite simple and straight forward. But the limitation is that i...