-
Notifications
You must be signed in to change notification settings - Fork 480
Added to the scope JS 101 article #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
||
When a variable is declared inside of a function using the `var` keyword, it is only available to code inside of that function — code outside of that function cannot access the variable. On the other hand, functions defined inside that function will have access to to the declared variable. | ||
|
||
There are two types of scopes in JavaScript: Global and local. Lets talk about each of them in turn. | ||
|
||
The first scope is __Global Scope__. This is very easy to define. If a variable or function is _global_, it can be got at from anywhere. In a browser, the global scope is the `window` object. So if in your code you simply have: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things:
- suggest "it can be accessed from anywhere within a program" rather than "it can be got at from anywhere"
- this paragraph ends as though the thought will be continued after the code example, but it is not continued
@rmurphey thanks for your feedback, I guess that teaches me for doing the pull request in a bit of a rush! Based on your feedback I have reworded and reworked things. As for if the attribution is out of date, I am unsure. I presumed the contents of the file before I edited were from jQF, and I've not removed it. |
|
||
Once that variable is set, it exists on the global object. Once that variable had been defined, it could be referenced as `window.x`, but because it exists on the global object we can simply refer to it as `x`. | ||
|
||
The only other scope we can have is __Local Scope__. JavaScript scopes at a function level. For example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better:
JavaScript also creates a Local Scope inside each function body. For exampmle:
On reading this content in greater depth, I think it would be good to spend some more time integrating it into the existing content -- presently it seems like there's some duplication of content between the old and new content, and it seems like some of the examples at the end of the section are no longer necessary, or should have some prose explanation associated with them. I can try to take a look at this tomorrow, but I'm not sure how best to submit the changes -- as a pull request on @jackfranklin's repo, or as a pull request to this repo that simply builds upon what's here. @ajpiano? I've added a couple more comments inline. I'd also suggest:
|
Thanks for your feedback @rmurphey :) I will have a crack at integrating the two bits more closely, and bring the file up to date with the style guide. I've not got any immediate idea as to why it's not merge-able, unless the Original MD file has changed since I pulled. I'll have an explore as to why. |
Just did some more tweaking. I've no idea why Github says this isn't merge-able though! I made a start on integrating the two "bits" of the article and adding some prose around the larger code examples. Not perfect by any means but a start. |
@jackfranklin Try pulling from upstream master and merging into the branch, or rebasing the branch on the current master. I know I did a lot of changes in the formatting/example code in this article. |
@gnarf37 thanks, I did a pull from upstream and indeed there was a conflict in scopes.md which I fixed. This does mean that this pull request is now awfully messy though with that latest commit that fixed the conflicts. Would it be best to delete this one, and I'll re-fork, apply my changes and do a new pull request so there's a lot less noise? |
}; | ||
console.log(x); // ReferenceError: x is not defined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
console.log( x );
@jackfranklin the diff doesn't seem that noisy - You could start over if you think that will result in a better pull though, I won't stop you :) |
If you're happy I'll leave it :-) I will make those adjustments tomorrow. |
|
||
This is a bad idea. Any variable that is global can have its value changed by any other parts of a program or any other script. This is undesirable, as it could lead to unforseen side effects. | ||
|
||
Secondly, it's considered bad practise to clutter the global scope. You should add as fewer properties as you possibly can to the global object, and try to keep your program contained within its own scope. That's why you'll see libaries such as jQuery often do this: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a restatement of the first reason not to have global variables. I'm hesitant to just say a thing is a bad practice without saying why it's a bad practice, but in fact you've already said why it's a bad practice, so this paragraph seems redundant to me.
I'd simply point out that IIFEs provide a way to avoid polluting the global scope -- ensuring that variables can't be tampered with by other code -- and move on to the example below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
practise -> practice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rmurphey do you mean to remove the entire jQuery IIFE example, or just reword the line that starts "Secondly..." to mention IIFEs, then show the jQuery example, and then move onto the next example (which starts on line 59) ?
@rmurphey, with regards to this comment:
Do you mean to remove the entire jQuery IIFE example, or just reword the line that starts "Secondly..." to mention IIFEs, then show the jQuery example, and then move onto the next example (which starts on line 59) ? I agree it could do with reworking a little but not entirely clear on what you mean. Thanks for all your help so far :) |
cc @rmurphey for some further comments on the above when you get a chance :) |
@jackfranklin I'd change the paragraph that starts with "Secondly" to just explain that IIFEs provide a way to avoid global variables, and then show the example of the jQuery IIFE. |
… and less repetitive
@rmurphey sorry for the delay on getting back to this! I've made the edits to shrink that sentence down and make it less repetitive. |
So, this merge went grey somewhere along the way, can you rebase/merge it? |
@gnarf37 just pulled in from upstream and fixed the conflict - hopefully it's green now? |
It is. I plan on pinging @ajpiano today about this, hopefully we can merge it before it gets stale again :) |
* master: (21 commits) 0.2.7 Remove remaining trailing whitespace from all pages. Fixes jquery#313. Style and typography fixes, and code style adherence in the JavaScript 101 section. Fixes jquery#312. 0.2.6 Correct comment in :input selector in Selecting Elements article. Fixes jquery#306. Expand the JavaScript 101 'Scope' article with more useful information and explanations. Fixes jquery#201. Added 0 to the list of falsy values in JavaScript 101 Conditional Code article. Fixes jquery#300. Fixes jquery#271 Inserted missing word in the JavaScript 101 Arrays article. Fixes jquery#299. Fixed inconsistency in showLinkLocation example in Basic Plugin Creation article. Fixes jquery#307. Fix example and other style cleanup in Basic Plugin Creation article. Fixes jquery#310. Fixes jquery#311. Update list of reserved words in JavaScript. Fixes jquery#301. Style and typography fixes and code style adherence in the Events section. Fixes jquery#294. Style and typography fixes, and code style adherence in the Effects section. Fixes jquery#290. Style and typography fixes, and code style adherence in the Code Organization section. Fixes jquery#287. Remove double ampersands in README header. Fixes jquery#284. Code and prose style improvements to all articles in Ajax chapter. Fixes jquery#283. Style fixes on the About page. Fixes jquery#279. Style guide fixes for the index, contributing, and About jQuery articles. Fixes jquery#270 relabel queue/dequeue content as advanced Added a missing 'i' in the for loop. Fixes jquery#280. ... Conflicts: page/ajax/ajax-and-forms.md page/ajax/jquery-ajax-methods.md page/effects/custom-effects.md page/effects/intro-to-effects.md page/events/event-basics.md page/events/event-helpers.md page/events/introduction-to-custom-events.md page/events/triggering-event-handlers.md
…n and explanations. Fixes jquery#201.
Following on from #89 (comment), I finally found some time to quickly move over some of the content I wrote for my initial Scope and this in JS article and move it over into the Scopes article, reworking it slightly but not making any major changes.
Let me know what you think :)