Skip to content

Commit 07c5f81

Browse files
Markus Amalthea Magnusonajpiano
Markus Amalthea Magnuson
authored andcommitted
Various small fixes on the JavaScript 101 Scope page. Fixes jquery#355.
1 parent 169f90a commit 07c5f81

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

page/javascript-101/scope.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ attribution:
66
- jQuery Fundamentals
77
---
88

9-
"Scope" refers to the variables that are available to a piece of code at a given time. A lack of understanding of scope can lead to frustrating debugging experiences. The idea of "scope" is that it's where certain functions or variables are accessible from in our code, and the context in which they exist and are executed in.
9+
"Scope" refers to the variables that are available to a piece of code at a given time. A lack of understanding of scope can lead to frustrating debugging experiences. The idea of scope is that it's where certain functions or variables are accessible from in our code, and the context in which they exist and are executed in.
1010

1111
There are two types of scopes in JavaScript: global and local. Let's talk about each of them in turn.
1212

@@ -28,6 +28,7 @@ JavaScript also creates a __Local Scope__ inside each function body. For example
2828
function myFunc() {
2929
var x = 5;
3030
};
31+
3132
console.log( x ); // ReferenceError: x is not defined
3233
```
3334

@@ -40,22 +41,23 @@ If you declare a variable and forget to use the `var` keyword, that variable is
4041
```
4142
function myFunc() {
4243
x = 5;
43-
});
44+
};
45+
4446
console.log( x ); // 5
4547
```
4648

4749
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.
4850

49-
Secondly, Immediately-Invoked Funcion Expressions provide a way to avoid global variables. You'll see many libraries such as jQuery often use these:
51+
Secondly, Immediately-Invoked Function Expressions provide a way to avoid global variables. You'll see many libraries such as jQuery often use these:
5052

5153
```
5254
(function() {
53-
var jQuery = { /* all my methods go here */ };
54-
window.jQuery = jQuery.
55+
var jQuery = { /* All my methods go here. */ };
56+
window.jQuery = jQuery;
5557
})();
5658
```
5759

58-
Wrapping everything in a function which is then immediately invoked means all the variables within that function are bound to the _local scope_. At the very end you can then expose all your methods by binding the `jQuery` object to the `window`, the _global object_. To read more about Immediatly-Invoked Functions, check out Ben Alman's [Immediately-Invoked Function Expression](http://benalman.com/news/2010/11/immediately-invoked-function-expression/) article.
60+
Wrapping everything in a function which is then immediately invoked means all the variables within that function are bound to the _local scope_. At the very end you can then expose all your methods by binding the `jQuery` object to the `window`, the _global object_. To read more about Immediately-Invoked Functions, check out Ben Alman's [Immediately-Invoked Function Expression](http://benalman.com/news/2010/11/immediately-invoked-function-expression/) article.
5961

6062
Because local scope works through functions, any functions defined within another have access to variables defined in the outer function:
6163

@@ -67,7 +69,7 @@ function outer() {
6769
console.log( x );
6870
}
6971
70-
inner(); // 5
72+
inner(); // 5
7173
}
7274
```
7375

@@ -82,7 +84,7 @@ function outer() {
8284
var y = 10;
8385
}
8486
85-
inner(); // 5
87+
inner(); // 5
8688
8789
console.log( y ); // ReferenceError: y is not defined
8890
}

0 commit comments

Comments
 (0)