Skip to content

Instantly share code, notes, and snippets.

@scripting
Created July 12, 2016 14:24
Show Gist options
  • Save scripting/c058fe247700ea58322eac2e78e7e283 to your computer and use it in GitHub Desktop.
Save scripting/c058fe247700ea58322eac2e78e7e283 to your computer and use it in GitHub Desktop.
Question about best practice with JS objects and internal method calls
messageOfTheDay = function (appName) {
this.appName = appName;
if (this.lastMessageText === undefined) {
this.lastMessageText = "";
}
this.sayHello = function () {
console.log ("hello");
}
this.checkForUpdate = function () {
var urlTextFile = "http://1999.io/testing/motd/" + this.appName + ".txt";
var motd = this;
readHttpFile (urlTextFile, function (s) {
if (s !== undefined) {
if (s !== this.lastMessageText) {
this.lastMessageText = s;
console.log ("messageOfTheDay.checkForUpdate: s == " + s);
motd.sayHello ();
}
}
});
}
};
@scripting
Copy link
Author

Allen, you're absolutely right about this.lastMessageText.

It clearly refers to an object that belongs to the callback to readHttpFile.

It's a mistake -- and I'll need to think about how it works and fix it.

BTW, re ES6, I'm not using any ES6 features yet.

I found a subset of JS that works for me, and then proceeded to develop the functionality I wanted. There's a lot of that so it hasn't left much time for exploring new language functionality. That's the way my brain works. It's why I don't like people figuring out how to fix "callback hell" -- I've made my peace with it, and would prefer not to learn another way to do something I already know how to do.

At age 61, I'm kind of "set in my ways" in this regard. ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment