Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb22cae

Browse files
committed
expanded some fundaments
1 parent 5f8ae98 commit fb22cae

File tree

5 files changed

+89
-39
lines changed

5 files changed

+89
-39
lines changed

VSCodeTips/README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
# VSCode Tips
22

3-
Although you are free to make you own choice of text/code editor to use during class and homework, we have good experiences with Microsoft's free Visual Studio Code (VSCode) editor.
4-
5-
From the [VSCode web site](https://code.visualstudio.com/):
6-
7-
> Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, macOS and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (such as C++, C#, Java, Python, PHP, Go) and runtimes (such as .NET and Unity).
8-
9-
Here are some tips for getting up to speed with VSCode as you progress through this course.
3+
Now that you're using VSCode here are some tips to use it effectively.
104

115
## Installation
126

fundamentals/assets/scopes.png

59.9 KB
Loading

fundamentals/map_filter.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const squares = numbers.map(num => num * num);
4040
console.log(squares); // -> [9, 25, 4, 49]
4141
```
4242

43-
For illustrative purposes we can add a `console.log` statement to our mapping function and see what we get passed as second and third argument:
43+
For illustrative purposes we can add a `console.log`<sup>†</sup> statement to our mapping function and see what we get passed as second and third argument:
4444

4545
```js
4646
const numbers = [3, 5, 2, 7];
@@ -66,6 +66,8 @@ For each of the first four lines in the output (from the `console.log` inside th
6666

6767
As is usual in JavaScript you do not necessarily have to use all the parameters that were passed to the `mapFn` function. In fact, in many cases you will only need the first argument (the current array element) as we saw in the first example.
6868

69+
<sup>†</sup>You should normally not use `console.log` inside the function you pass to the `map` (or `filter`) method. These functions should also **not modify** any variables outside their own function scope, although they can reference these variables for read-only purposes. In computer science terms, these functions should be [pure with no 'side-effects'](https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-pure-function-d1c076bec976).
70+
6971
## Array#filter()
7072

7173
The **filter()** method returns a new array with all elements that pass the test implemented by a user-supplied (predicate\*) function.

fundamentals/naming_conventions.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Background
44

5-
In programming you will need to come up with appropriate names for your variables and functions.
5+
In programming you will need to come up with appropriate names for your variables, functions and function parameters.
66

77
> _The most important consideration in naming a variable is that the name fully and accurately describes the entity the variable represents. An effective technique for coming up with a good name is to state in words what the variable represents. Often that statement itself is the best variable name. It’s easy to read because it doesn’t contain cryptic abbreviations, and it’s unambiguous. Because it’s a full description of the
88
entity, it won’t be confused with something else. And it’s easy to remember because the name is similar to the concept._
@@ -86,6 +86,11 @@ class Movie {
8686
}
8787
```
8888

89+
## Reserved keywords
90+
91+
Certain names are reserved by JavaScript for its own use. You cannot use the names for your variable. For example, you can't name a variable `if`.
92+
93+
For a complete list of reserved names, see the MDN page for [Keywords](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords).
8994

9095

9196

fundamentals/scope.md

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,50 @@ Definition from [Dictionary.com](http://www.dictionary.com/browse/scope):
99

1010
If you imagine yourself as the computer that is executing your JavaScript code, you can think of scope as meaning:
1111

12-
> what you can see from where your are
12+
> what you can see from where you are
1313
1414
In this case the 'things' that you are looking for are variables and functions. When we say, "What's in scope?", we mean "Which variables and functions can be accessed from the current point of execution in your code?" As it happens, in JavaScript there are three types of scope to consider.
1515

1616
## Global scope
1717

18-
Variables and functions defined at global scope are visible from any point of execution in your code. Sometimes that's a good thing (or even essential), but more often it's bad. Let's dive right in.
18+
Variables and functions defined at global scope are visible from any point of execution in your code. Sometimes that's a good thing (or even essential), but in general you must avoid creating variables and functions in global scope unless you have specific reasons to do so.
19+
20+
Consider the example below:
1921

2022
```js
21-
a = 10; // don't do this
23+
a = 'Hello'; // don't do this
2224
console.log(a);
2325
```
2426

25-
In this example we have assigned the value `10` to a variable that we forgot to declare (we forgot to use `let`, `const` or `var`). The JavaScript engine tries to be helpful and defines a variable `a` for us in **global** scope. More recently, the JavaScript community considered this friendliness to be a mistake and with ES5 introduced `strict` mode: place the string `'use strict';` at the top of your file.
27+
In this example we have assigned the value `'Hello'` to a variable that we forgot to declare (we forgot to use `let`, `const` or `var`). The JavaScript engine tries to be helpful and defines a variable `a` for us in **global** scope. More recently, the JavaScript community considered this friendliness to be a mistake and with ES5 introduced `strict` mode: place the string `'use strict';` at the top of your file. This now causes our code to fail with a run-time error when forgetting to 'declare' our variables.
2628

2729
```js
2830
'use strict';
29-
a = 10; // produces: ReferenceError: a is not defined
31+
a = 'Hello'; // produces: ReferenceError: a is not defined
3032
console.log(a);
3133
```
3234

3335
You can correct this by declaring your variable with `let`, `const` or `var`:
3436

3537
```js
3638
'use strict';
37-
let a = 10;
39+
var a = 'Hello';
3840
console.log(a);
3941
```
4042

41-
This still puts the variable `a` into the global scope. So why is global scope a problem? Because you cannot be sure that the variable names you choose do not conflict with names already present in global scope (global scope is a busy place). It is best to apply the principle of 'need to know'. Only expose your variables to other parts in the JavaScript ecosystem that need to know about them. This is where local scope and block scope come in.
43+
This still puts the variable `a` into the global scope. So why is global scope a problem? Because you cannot be sure that the variable names you choose do not conflict with names already present in global scope (global scope is a busy place).
44+
45+
It is best to apply the principle of 'need to know'. Only expose your variables to other parts in the JavaScript ecosystem that need to know about them. This is where local scope and block scope come in.
4246

4347
## Local scope
4448

45-
When you declare a function in JavaScript the function body represents a new, local scope, distinct from global scope. Variables defined in the function body are visible only inside that function: from the outside, you can't look in.
49+
When you declare a function in JavaScript the function body represents a new, local scope, distinct from global scope. Variables defined in the function body are visible in that function body only: from the outside, you can't look in.
4650

4751
```js
4852
'use strict';
4953

5054
function myFunction() {
51-
const a = 10;
55+
const a = 'Hello';
5256
console.log(a);
5357
}
5458

@@ -57,63 +61,108 @@ myFunction();
5761
// console.log(a); <= this would produce: ReferenceError: a is not defined
5862
```
5963

60-
But from the inside you can look out. In the example below the variable `b` is visible from inside the function body.
64+
But from the inside you can look out. In the example below the variable `a` is visible from inside the function body.
6165

6266
```js
6367
'use strict';
6468

65-
const b = 'Hello';
69+
const a = 'Hello';
6670

6771
function myFunction() {
68-
const a = 10;
69-
console.log(a);
70-
console.log(b);
72+
const b = ', world';
73+
console.log(a + b);
7174
}
7275

7376
myFunction();
7477
```
7578

76-
You might think that the variable `b` is in global scope. Actually, variables declared with either `let` or `const` have block scope, as will be discussed next. Note however that the function `myFunction` still resides in global scope. There is a way to even get `myFunction` out of global scope by using, what is called, an **IIFE**. See further down below.
79+
You might think that the variable `a` is in global scope. Actually, variables declared with either `let` or `const` have block scope, as will be discussed next. Note however that the function `myFunction` still resides in global scope. There is a way to get `myFunction` out of the global scope by using, what is called, an **IIFE** to create a **local scope** (or, with ES6, by placing the function definition in a block to create a **block scope**). See further down below.
7780

7881
## Block scope
7982

80-
The keywords `let` and `const` were introduced in ES6 as alternative to the older `var` keyword and we recommend that you use these newer keywords instead of `var`. They adhere to the rules for block scope, whereas `var` is completely oblivious of the concept.
83+
The keywords `let` and `const` were introduced in ES6 as alternatives to the existing `var` keyword. We recommend that you use these newer keywords instead of `var`. They adhere to the rules for **block scope**, whereas `var` is completely oblivious of the concept.
8184

82-
A new block scope (sometimes called _lexical_ scope) is created whenever you create a block of code inside a pair of curly braces. Variables defined with `let` and `const` at the file level (i.e., not inside a function) are considered to be in a (file-level) block scope. That's why the variable `b` in the previous code snippet is not in global scope. Had we replaced `const b` with `var b` then variable `b` _would be_ in global scope.
85+
A new block scope (sometimes called _lexical_ scope) is created whenever you create a block of code inside a pair of curly braces. (Exception: the curly braces used to enclose the body of a function definition do not create a block scope. Instead, a **local scope** is created as discussed in the previous section.)
8386

84-
## Guidance
87+
Variables defined with `let` and `const` at the file level (i.e., not inside a function or a block) are considered to be in a file-level block scope ('script' scope). That's why the variable `a` in the previous code snippet is not in global scope. Had we replaced `const a` with `var a` then variable `a` _would be_ in global scope.
88+
89+
## Scope Example
90+
91+
In the figure below we show an example bringing all scope types together. The code fragment on the right-hand side is executed by means of a `<script>` tag in `index.html`. The scope hierarchy shows the state of the scopes at the execution point indicated by the red triangle.
92+
93+
![Scopes](assets/scopes.png)
94+
95+
Let's go through these scopes from the top down:
96+
97+
- The `alert` function and the `document` object are placed in **global scope** by the browser at start-up (along with many other predefined 'system' variables). The `myFunction` function originates from our code snippet.
98+
- The next scope shown is a **block scope** at the file level (our file `app.js` loaded up with a `<script>` tag). Variables defined outside of a block or function in this file will be visible from any location within the file. This holds true for the variable `a`.
99+
- Next comes the **local scope** of the function `myFunction`. It contains the variables `b`, `h1` and `root`.
100+
- The most inner scope is the **block scope** exists within the `if` statement block only. This is the scope that holds the variable `c`.
101+
102+
### Variable resolution
103+
104+
Let's consider the statement below taken from the example code snippet and examine how the variable references `a`, `b` and `c` are resolved by 'walking the scope chain'.
105+
106+
```js
107+
h1.innerHTML = a + b + c;
108+
```
85109

86-
Following the principle of 'need to know', it is best to define variables at the point of need, i.e. just before you need to access these variables. This will either be in the same scope of the code where you access the variable, or, in case you need to access the variable from multiple places, the closest scope that is common to the points of need.
110+
Variable resolution proceeds by attempting to resolve a variable reference from the current scope (i.e., at the point of reference), through intermediate scopes and ultimately ending in global scope, in the direction of the arrows.
87111

88-
Sometimes however, you cannot avoid global scope. This is for instance the case when you want to access the [DOM](https://developer.mozilla.org/en-US/docs/Glossary/DOM) in the browser. As you will learn, you can create an HTML element (e.g. an `h1` element) in JavaScript using this code:
112+
| Variable | Resolution |
113+
| -------- | ---------- |
114+
| `c` | This variable is defined in the current (block) scope and is resolved immediately. |
115+
| `b` | After failing to resolve this variable from the current (block) scope, JavaScript walks up the scope chain and finds its definition in the local (function) scope. |
116+
| `a` | To resolve this variable the JavaScript engine must hop up to the block scope at the file level. |
117+
118+
For the statement below, taken again from the code snippet in the picture, the JavaScript engine must walk the scope chain starting from the local (function) scope up to the global scope to find the definition of `document`.
89119

90120
```js
91-
const h1 = document.createElement('h1');
121+
const root = document.getElementById('root')
92122
```
93123

94-
The `document` object resides in global scope, conveniently provided there by the browser for access by any piece of JavaScript that needs to manipulate the DOM.
124+
## Guidance
125+
126+
Following the principle of 'need to know', it is best to define variables at the point of need, i.e. just before you need to have access to these variables. This will either be in the same scope of the code where you access the variable, or, in case you need to access the variable from multiple places, the nearest scope that is common to the points of need.
95127

96-
## IFFE
128+
## IIFE
97129

98-
We were left with the issue that functions defined at the file level still end up in global scope. The traditional method of solving this in JavaScript is to use an Immediately Invoked Function Expression (IIFE). We present it here for info. When you build web applications with modern tools such as React and Node, you do not need to use IFFEs.
130+
We were left with the issue that functions defined at the file level still end up in global scope. The traditional method of solving this in JavaScript is to use an Immediately Invoked Function Expression (IIFE). We present it here for info. When you build web applications with modern tools such as React and Node, you do not need to use IIFEs.
99131

100-
To use an IFFE you must wrap all of your JavaScript code in a function body of an anonymous function (i.e., a function with no name) and immediately call that function (as indicated by the empty set of parentheses on the last line). This creates a local scope for your code. Now even the `myFunction` function is in local scope.
132+
To use an IIFE you must wrap all of your JavaScript code in a function body of an anonymous function (i.e., a function with no name) and immediately call that function (as indicated by the empty set of parentheses on the last line). This creates a local scope for your code. Now even the `myFunction` function is in local scope.
101133

102134
```js
103135
(function () {
104136
'use strict';
105137

106-
const b = 'Hello';
138+
const a = 'Hello';
107139

108140
function myFunction() {
109-
const a = 10;
110-
console.log(a);
111-
console.log(b);
141+
const b = ', world!';
142+
console.log(a + b);
112143
}
113144

114145
myFunction();
115146
})();
116147
```
117148

118-
More info on MDN: [IFFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE)
149+
More info on MDN: [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE)
150+
151+
## IIFE alternative (ES6)
152+
153+
In ES6 you can replace the IIFE by a simple block, like this, to achieve the same effect.
154+
155+
```js
156+
{
157+
'use strict';
158+
159+
const a = 'Hello';
119160

161+
function myFunction() {
162+
const b = ', world!';
163+
console.log(a + b);
164+
}
165+
166+
myFunction();
167+
};
168+
```

0 commit comments

Comments
 (0)