Skip to content

Commit

Permalink
let -> const (mdn#18697)
Browse files Browse the repository at this point in the history
* update module to use const

* use const in control flow and error handling

* use const in reference/operators/function

* reverted var examples

* Update files/en-us/web/javascript/guide/modules/index.md

* Update files/en-us/web/javascript/guide/modules/index.md

* Update files/en-us/web/javascript/guide/modules/index.md

* Update files/en-us/web/javascript/reference/operators/function/index.md

Co-authored-by: Carrie Macfarlane <[email protected]>
Co-authored-by: Joshua Chen <[email protected]>
  • Loading branch information
3 people authored Jul 24, 2022
1 parent 7761f01 commit 4f19b21
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions files/en-us/web/javascript/guide/modules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ You can see such lines in action in [`main.js`](https://github.com/mdn/js-exampl
Once you've imported the features into your script, you can use them just like they were defined inside the same file. The following is found in `main.js`, below the import lines:

```js
let myCanvas = create('myCanvas', document.body, 480, 320);
let reportList = createReportList(myCanvas.id);
const myCanvas = create('myCanvas', document.body, 480, 320);
const reportList = createReportList(myCanvas.id);

let square1 = draw(myCanvas.ctx, 50, 50, 100, 'blue');
const square1 = draw(myCanvas.ctx, 50, 50, 100, 'blue');
reportArea(square1.length, reportList);
reportPerimeter(square1.length, reportList);
```
Expand Down Expand Up @@ -371,7 +371,7 @@ import * as Triangle from './modules/triangle.js';
In each case, you can now access the module's imports underneath the specified object name, for example:

```js
let square1 = Square.draw(myCanvas.ctx, 50, 50, 100, 'blue');
const square1 = Square.draw(myCanvas.ctx, 50, 50, 100, 'blue');
Square.reportArea(square1.length, reportList);
Square.reportPerimeter(square1.length, reportList);
```
Expand Down Expand Up @@ -413,7 +413,7 @@ import { Square } from './modules/square.js';
And then use the class to draw our square:

```js
let square1 = new Square(myCanvas.ctx, myCanvas.listId, 50, 50, 100, 'blue');
const square1 = new Square(myCanvas.ctx, myCanvas.listId, 50, 50, 100, 'blue');
square1.draw();
square1.reportArea();
square1.reportPerimeter();
Expand Down Expand Up @@ -492,15 +492,15 @@ In this example we've only made changes to our [`index.html`](https://github.com
Over in `main.js` we've grabbed a reference to each button using a [`Document.querySelector()`](/en-US/docs/Web/API/Document/querySelector) call, for example:

```js
let squareBtn = document.querySelector('.square');
const squareBtn = document.querySelector('.square');
```

We then attach an event listener to each button so that when pressed, the relevant module is dynamically loaded and used to draw the shape:

```js
squareBtn.addEventListener('click', () => {
import('./modules/square.js').then((Module) => {
let square1 = new Module.Square(myCanvas.ctx, myCanvas.listId, 50, 50, 100, 'blue');
const square1 = new Module.Square(myCanvas.ctx, myCanvas.listId, 50, 50, 100, 'blue');
square1.draw();
square1.reportArea();
square1.reportPerimeter();
Expand Down Expand Up @@ -562,19 +562,19 @@ Let's include this module in our [`main.js`](https://github.com/mdn/js-examples/
import colors from './modules/getColors.js';
import { Canvas } from './modules/canvas.js';

let circleBtn = document.querySelector('.circle');
const circleBtn = document.querySelector('.circle');

//
```

We'll use `colors` instead of the previously used strings when calling our shape functions:

```js
let square1 = new Module.Square(myCanvas.ctx, myCanvas.listId, 50, 50, 100, colors.blue);
const square1 = new Module.Square(myCanvas.ctx, myCanvas.listId, 50, 50, 100, colors.blue);

let circle1 = new Module.Circle(myCanvas.ctx, myCanvas.listId, 75, 200, 100, colors.green);
const circle1 = new Module.Circle(myCanvas.ctx, myCanvas.listId, 75, 200, 100, colors.green);

let triangle1 = new Module.Triangle(myCanvas.ctx, myCanvas.listId, 100, 75, 190, colors.yellow);
const triangle1 = new Module.Triangle(myCanvas.ctx, myCanvas.listId, 100, 75, 190, colors.yellow);
```

This is useful because the code within [`main.js`](https://github.com/mdn/js-examples/blob/master/module-examples/top-level-await/main.js) won't execute until the code in [`getColors.js`](https://github.com/mdn/js-examples/blob/master/module-examples/top-level-await/modules/getColors.js) has run. However it won't block other modules being loaded. For instance our [`canvas.js`](https://github.com/mdn/js-examples/blob/master/module-examples/top-level-await/modules/canvas.js) module will continue to load while `colors` is being fetched.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ console.log(notHoisted) // undefined
// even though the variable name is hoisted, the definition isn't. so it's undefined.
notHoisted(); // TypeError: notHoisted is not a function

var notHoisted = function() {
var notHoisted = function () {
console.log('bar');
};
```
Expand All @@ -86,7 +86,7 @@ If you want to refer to the current function inside the function body, you need
This also avoids using the non-standard {{jsxref("Functions/arguments/callee", "arguments.callee")}} property.

```js
let math = {
const math = {
'factit': function factorial(n) {
console.log(n)
if (n <= 1) {
Expand Down

0 comments on commit 4f19b21

Please sign in to comment.