variables == “pain in my butt”

I just don’t get this global variables and hoisting and scope business.

Actually, I do. I really do.

I just don’t understand why changing the value of a global variable in a function doesn’t change the value of the global variable.

You might say, “But …”

And I’ll say, “Nope … because this other person said the opposite and I know that the opposite is true.”

It doesn’t matter what would follow your “but” — the opposite is true. And that’s what confuses me.

All the articles and such that I read give contradictory information. Yet, apparently, they’re all correct and I can’t figure out why.

works.jpg

Best comic ever.

I’m back to [re]writing my javascript code so I can get my data into my database.

I thought that maybe, since global variables aren’t really global, I’d follow the advice of some documentation/articles/posts and use object methods instead — having all my functions use the variables they’re forcing to become local to build a javascript object at the global level sounds like a great idea.

Except functions are like black holes and don’t seem to allow information to escape them.

Here’s the latest code with which I’m experimenting:

code2.png

code.png

There’s also console.log(rockstar); at the top level on line 226.

All variables such as artistName, artistPop, and artistAlbums are declared globally at the top. That doesn’t seem to make a difference in the behavior of my code, however. Here are the results:

console.png

The problem I’ve been having is the result from line 226 but I at least thought I understood the problem. Little tests like lines 88 and 90 produced similar, expected results.

  • Line 88 gets the name and popularity because it’s super local
  • Line 90 can’t see either because it’s outside the function. Fine. I don’t like it and it’s a wee bit inconsistent with other results I’ve seen but, whatever, I’m just hoping I’ll start to understand why it works other places but not here. Moving on.

Then, just for kicks, I did line 109. I expected it to see artistAlbums but of course it wouldn’t see artistName and artistPop. No way.

  • How can line 90 — inside the same function (requestArtistInfo) as line 88 — see neither artistName nor artistPop yet line 109, in a completely different function, can?
  • If line 109 can see those values, why can’t line 226?

I want to pound myself in the face with a hammer.

WAIT A SECOND …

It looks like 90 is getting “hoisted” or, by any other name, completed first even though it comes after line 88 … when those variables don’t have values yet.

Even though line 226 is called after requestArtistInfo, requestArtistInfo still might not be done … I didn’t explicitly say for anything to “wait” until anything else was “done” before starting …

Line 88 logs after those values are assigned and there’s no getting around that so it works.

Line 109, in getArtistAlbums, isn’t going to happen until bunches of other shizzle is complete so that comes back with everything …

SO, brothers and sisters, everything is working (or would work) but some stuff is just happening (or trying to happen) too soon.

My tests and experiments have been failing but not for the reasons I thought.

Okay … this is awesome. Major learning experience if I’m correct that it isn’t the “where” that’s not working, it’s the “when.”

Advertisements

About jotascript

Aiming to please. Seeking to impress.
This entry was posted in Debugging, JavaScript. Bookmark the permalink.

3 Responses to variables == “pain in my butt”

  1. Chris says:

    I don’t play much with jQuery (which is where I believe your $ comes from)… On line 82, ‘function (json) {…}’ is that a callback? If it is line 90 is likely happening before 88 because the callBack isn’t done processing. I’d be interesting in looking at the whole file, do you have it on a github? or another page?

    Like

    • jotascript says:

      I figured that out while I was writing the post but thanks, definitely, for the feedback. A previous version is on GitHub — I only push ’em when they’re at a stage where everything is working. At the moment, I’m working on this new stage (creating the object with everything in it) so, once I get that working, I’ll upload it. And I’ll post a link.

      Like

  2. codeinfig says:

    i dont play much with jquery either– i tend to write my own javascript without using libraries.

    however, in python i almost never use globals. im not against them (if it works for you, go for it– scope is there for your convenience and others using your code, so you dont have to keep track of every identifier and value.)

    if a global isnt getting updated properly (what makes it global again? how it is set initially in both the global context and in the function? perhaps thats the trouble?) just use return p; to exit the function.

    change “p” accordingly.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s