Skip to content

Commit ac48f8a

Browse files
committed
JS formatting
1 parent 72ce951 commit ac48f8a

File tree

1 file changed

+56
-48
lines changed

1 file changed

+56
-48
lines changed

README.md

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ function razzle() {
5757
}
5858
```
5959

60-
Functions can be passed arguments, given default arguments, etc. Here's a
60+
Functions can be passed arguments, given default arguments, etc. Here's a
6161
brief code synopsis:
6262

6363
```js
64-
function razzle(lawyer="Billy", target="'em") {
64+
function razzle(lawyer = "Billy", target = "'em") {
6565
console.log(`${lawyer} razzle-dazzles ${target}!`);
6666
}
6767
razzle(); //=> Billy razzle-dazzles 'em!
@@ -111,13 +111,13 @@ Uncaught SyntaxError: Function statements require a function name
111111
The problem is that, when the function expression appears by itself as shown
112112
above, **JavaScript does not recognize it as a function expression**; it instead
113113
interprets it as a function declaration that's missing its name. One way to tell
114-
the JavaScript engine that it's a function expression is to use the `grouping
115-
operator ()` to wrap the entire thing:
114+
the JavaScript engine that it's a function expression is to use the
115+
`grouping operator ()` to wrap the entire thing:
116116

117117
```js
118-
(function() {
118+
(function () {
119119
console.log("Yet more razzling");
120-
})
120+
});
121121
```
122122

123123
Recall that the grouping operator is usually used in arithmetic operations to
@@ -155,8 +155,8 @@ a callback function. For example, you'll often see anonymous functions passed as
155155
an argument to an event listener:
156156

157157
```js
158-
const button = document.getElementById('button');
159-
button.addEventListener('click', function() {
158+
const button = document.getElementById("button");
159+
button.addEventListener("click", function () {
160160
console.log("Yet more razzling");
161161
});
162162
```
@@ -172,9 +172,9 @@ declaring a variable and assigning the function as its value. Recall that any
172172
expression can be assigned to a variable; this includes function expressions:
173173

174174
```js
175-
const fn = function() {
175+
const fn = function () {
176176
console.log("Yet more razzling");
177-
}
177+
};
178178
```
179179

180180
The code above defines our function using a function expression. If we ask
@@ -190,9 +190,9 @@ to _invoke_ or _call_ the function. We do this by adding `()` to the end of our
190190
"pointer", the variable name:
191191

192192
```js
193-
const fn = function() {
193+
const fn = function () {
194194
console.log("Yet more razzling");
195-
} //=> undefined
195+
}; //=> undefined
196196
fn; //=> ƒ () { console.log("Yet more razzling") }
197197
fn(); // "Yet more razzling"
198198
```
@@ -201,7 +201,7 @@ Also as with a function declaration, if we need to pass arguments to the
201201
function, we would include those in the parentheses when we call the function.
202202

203203
We now know how to define a function as a function expression. Very importantly,
204-
***function expressions are not hoisted***. The same is true for any variable
204+
**_function expressions are not hoisted_**. The same is true for any variable
205205
assignment: if we assign a `String` or the result of an arithmetic expression to
206206
a variable, those assignments are not hoisted either.
207207

@@ -219,7 +219,9 @@ Another way to invoke an anonymous function is by creating what's known as an
219219
As a thought experiment, consider what happens here:
220220

221221
```js
222-
(function(baseNumber){ return baseNumber + 3 })(2); //=> ???
222+
(function (baseNumber) {
223+
return baseNumber + 3;
224+
})(2); //=> ???
223225
```
224226

225227
We recognize the first `()` as the grouping operator that tells the JavaScript
@@ -234,7 +236,9 @@ those parentheses return immediately after defining it. Try it out in the
234236
browser console:
235237

236238
```js
237-
(function(baseNumber){ return baseNumber + 3; })(2); //=> 5
239+
(function (baseNumber) {
240+
return baseNumber + 3;
241+
})(2); //=> 5
238242
```
239243

240244
Interestingly, any variables, functions, `Array`s, etc. that are defined
@@ -260,10 +264,12 @@ start by seeing it in action.
260264
Consider this code:
261265

262266
```js
263-
function outer(greeting, msg="It's a fine day to learn") { // 2
264-
const innerFunction = function(name, lang="Python") { // 3
267+
function outer(greeting, msg = "It's a fine day to learn") {
268+
// 2
269+
const innerFunction = function (name, lang = "Python") {
270+
// 3
265271
return `${greeting}, ${name}! ${msg} ${lang}`; // 4
266-
}
272+
};
267273
return innerFunction("student", "JavaScript"); // 5
268274
}
269275

@@ -281,7 +287,7 @@ Let's break this down:
281287
default value of `"Python"`. The function expression itself is saved in the
282288
local variable `innerFunction`.
283289
4. Inside `innerFunction` we make use of its parameters, `name` and `lang`,
284-
***as well as*** the `greeting` and `msg` parameters defined in
290+
**_as well as_** the `greeting` and `msg` parameters defined in
285291
innerFunction's containing (parent) function, `outer`. `innerFunction` has
286292
access to those variables via the scope chain.
287293
5. Finally, inside `outer`, we invoke `innerFunction`, passing arguments that
@@ -299,10 +305,10 @@ return the result of calling `innerFunction`, let's have it return the function
299305
itself:
300306

301307
```js
302-
function outer(greeting, msg="It's a fine day to learn") {
303-
const innerFunction = function(name, lang="Python") {
308+
function outer(greeting, msg = "It's a fine day to learn") {
309+
const innerFunction = function (name, lang = "Python") {
304310
return `${greeting}, ${name}! ${msg} ${lang}`;
305-
}
311+
};
306312
return innerFunction;
307313
}
308314
```
@@ -331,7 +337,7 @@ storedFunction("student", "JavaScript");
331337
```
332338

333339
Note that we are no longer calling `innerFunction` from inside `outer`.
334-
Amazingly, the code works ***exactly the same***: it ***still*** has access to
340+
Amazingly, the code works **_exactly the same_**: it **_still_** has access to
335341
those parent function's variables. It's like a little wormhole in space-time to
336342
the `outer`'s scope!
337343

@@ -340,13 +346,13 @@ expression to `innerFunction` and returning that, let's just return the function
340346
expression.
341347

342348
```js
343-
function outer(greeting, msg="It's a fine day to learn") {
344-
return function(name, lang="Python") {
349+
function outer(greeting, msg = "It's a fine day to learn") {
350+
return function (name, lang = "Python") {
345351
return `${greeting}, ${name}! ${msg} ${lang}`;
346-
}
352+
};
347353
}
348354

349-
outer("Hello")("student", "JavaScript")
355+
outer("Hello")("student", "JavaScript");
350356
//=> "Hello, student! It's a fine day to learn JavaScript"
351357
```
352358

@@ -366,8 +372,8 @@ parentheses and passing the arguments `"student"` and `"JavaScript"`, which were
366372
stored in `name` and `lang`. This filled in the final two values inside of the
367373
template string and returned:
368374

369-
```bash
370-
"Hello, student! It's a fine day to learn JavaScript"
375+
```js
376+
"Hello, student! It's a fine day to learn JavaScript";
371377
```
372378

373379
## Define `Closure`
@@ -382,15 +388,17 @@ wanted to let just tiny bits of information leak back out, we might want to
382388
pass that information back out, through a closure.
383389

384390
```js
385-
const array = (
386-
function(thingToAdd) {
387-
const base = 3;
388-
return [
389-
function() { return base + thingToAdd; },
390-
function() { return base; }
391-
];
392-
}
393-
)(2)
391+
const array = (function (thingToAdd) {
392+
const base = 3;
393+
return [
394+
function () {
395+
return base + thingToAdd;
396+
},
397+
function () {
398+
return base;
399+
},
400+
];
401+
})(2);
394402
```
395403

396404
Note that the value on the right of the `=` in the first line is a function
@@ -402,10 +410,10 @@ variable.
402410
Go ahead and copy the code above into your browser console and take a look at the
403411
values of the two elements of `array`. You should see the following:
404412

405-
```js
413+
```js
406414
array[0]; //=> ƒ () { return base + thingToAdd; }
407415
array[1]; //=> ƒ () { return base; }
408-
```
416+
```
409417

410418
However, if you try looking at the value of `base` in the console you'll get a
411419
reference error: the value of `base` is not accessible outside the function it's
@@ -430,16 +438,16 @@ parent (and grandparent) scopes' variables. Here's a simple example:
430438

431439
```js
432440
function demoChain(name) {
433-
const part1 = 'hi'
434-
return function() {
435-
const part2 = 'there'
436-
return function() {
441+
const part1 = "hi";
442+
return function () {
443+
const part2 = "there";
444+
return function () {
437445
console.log(`${part1.toUpperCase()} ${part2} ${name}`);
438-
}
439-
}
446+
};
447+
};
440448
}
441449

442-
demoChain("Dr. Stephen Strange")()() //=> HI there Dr. Stephen Strange
450+
demoChain("Dr. Stephen Strange")()(); //=> HI there Dr. Stephen Strange
443451
```
444452

445453
When it is called, the innermost function has access to `name`, `part1`, and
@@ -465,7 +473,7 @@ Implement a function called `wrapAdjective`:
465473
Thus a total call should be:
466474

467475
```js
468-
wrapAdjective("%")("a dedicated programmer") //=> "You are %a dedicated programmer%!"
476+
wrapAdjective("%")("a dedicated programmer"); //=> "You are %a dedicated programmer%!"
469477
```
470478

471479
Run `learn` to verify you've gotten this set of tests passing.

0 commit comments

Comments
 (0)