Learning JavaScript

Teaching myself JavaScript.
Doesn’t seem too difficult, but a couple of odd things when looking at other’s code.

What’s all the self. and this. about?

Every function seems to start with

var self = this;

And then it seems like most every function and variable needs to have

self.

in front of it.

Why not just type

this.

in front of everything?

Also, seems like a lot of extra typing to have to have all those self. or this.'s everywhere?

Good question. It seems to be about getting the value in the correct context. If you don’t grab ‘this’ at the right time you might not get the correct ‘this’:

It also explains why they use var instead of let:

Thanks, @HalGumbert. Seems like they’ve made it pretty easy to make hard-to-find errors. Not sure why it’s that way, but I now understand better at least.

Most code I’ve looked at uses var instead of let even though it seems like let would be more “correct”. Old habits, I guess!

Ok here’s a little more code that doesn’t make sense to me.

var receivebuffer = '';

self.socket.on('data', function (chunk) {
			receivebuffer += chunk;
			line = receivebuffer.substr(0, receivebuffer.length);

			if (receivebuffer.indexOf('NOTIFY') == '-1'){
				debug("Received from device: "+ line.toString());
			}

line = receivebuffer.substr(0, receivebuffer.length); Wouldn’t this be the same as line = receivebuffer ?

debug("Received from device: "+ line.toString()); Isn’t line already a string, why would .toString() be added?

I’ve been using PHPStorm which has taught me a bit of these tips. It had inline tips suggesting to use let over var in functions.

In that receivebuffer example, they are using substr to get what has been received at that point in time as the receivebuffer could be updated in the milliseconds between socket events. I’m not sure why that matters.

I’m not sure why they use tostring. Maybe they want to guarantee it’s not to be added as a number?

Could there be several copies of this function running at the same time (and more than one copy of the variables) if you have more than one webpage open using the same javascript code?
My thought would be that each open page would have it’s own copy of the javascript code and variables, but is there sharing going on like multi-threaded apps that you have to be worried about which instance is updating global variables, etc.?

I think I need to look more into how JS works with multiple pages open and when it actually runs, things like that - does it re-run the JS on refresh or does it have an event loop or what?

JS runs locally so they won’t conflict. On an individual page an event can call a function over and over though.

Thanks, @HalGumbert. So, are you saying it’s just one copy no matter how many pages are open from the same JS code? The variable values will be the same on every page?

So what’s the normal procedure for reading/writing data files? My understanding is that JS can’t access the filesystem? Do people use a database or is there some other “norm” for reading/writing data in JS on a webpage?

Each page is a separate instance. So if you had three tabs open to the same page, the JS vars would start the same and then diverge.

1 Like

I’m not sure what most people do, but I use PHP which runs on the server to read and write to mysql and files as needed. So if User loads a Page to edit a Contact:

  1. PHP queries sql to get the data and creates the Page HTML and JS.
  2. User receives the page.
  3. User edits the First Name.
  4. The input js onChange event fires which uses js ajax to asynchronously post the data.
  5. The posted data is received by PHP which then uses sql to Update the record and returns Success or Fail.
  6. The User then sees the returned message via js.

I haven’t tried doing all of that in pure js.

Copy that. Looks like the fs library works. Not sure why folks are saying you can’t read/write local data files…

const fs = require("fs");

// Asynchronous read
fs.readFile('input.txt', function (err, data) {
  if (err) {
    return console.error(err);
  }
  console.log("Asynchronous read: " + data.toString());
});

// Synchronous read
var data = fs.readFileSync('input.txt');
console.log("Synchronous read: " + data.toString());