JavaScript Learning – Chapter 1 Basic Knowledge (Part 1)

foreword

First of all, I am not a code master. Although I know the front end, the foundation is not very solid. The effect code that is partially implemented in the webpage is also the result of my own research after looking for tutorials on the Internet or after asking someone for help.
Nowadays, due to my increasingly complex effect requirements, my existing knowledge is not enough to support my rigorous development, so I re-learn the JavaScript language with reference to the tutorial website, and write this series of notes on this basis.

Tutorial for this article: Modern JavaScript Tutorial

This chapter corresponds to Tutorial – Part 1 The JavaScript Programming Language – Basics of JavaScript – 2.1 Hello World! to 2.3 Modern Mode, use strict

The development software I use: Visual Studio Code

start

script tag

This tag can be inserted almost anywhere in an HTML document.
There are two ways to use it.

 //使用标签包裹代码,像这样: <script> alert('This is example code.') </ script> //或者使用外部脚本,利用` script`标签的src 属性引用外部脚本,像这样: <script src="./ script.js"> //同样的,若使用多个脚本,可以使用多个标签。

PS: script cannot have both internal code and src attribute. In some old versions of code, you can see the type and language attributes of the script tag, which are not required in today’s HTML standard.

code structure

Statements are syntactic structures and commands that perform actions.
Use semicolons to separate statements.
Code comments in JavaScript can be written in two ways:
A single-line comment starting with //:

 //这是一个单行注释

and a multi-line comment enclosed with /**/:

 /*这是一个多行注释它可以包含多行文本并将其注释掉包含在注释内的代码并不会执行*/

Considerations and Additional Notes:
1. In the tutorial, there is such a description:

When a newline character is present, the semicolon can be omitted in most cases.
JavaScript interprets newlines as implicit semicolons, also known as automatic semicolon insertion.
In most cases, a newline means a semicolon, but “mostly” doesn’t mean “always”.
But there are cases where JavaScript can’t determine whether it really needs to automatically insert a semicolon.
Errors that occur in this situation are difficult to find and fix.

Or get into the habit of using semicolons to separate a complete statement to avoid such hidden bugs that are hard to find and fix
2. Comments cannot be nested.
3. Under Windows, most editors support shortcut key annotation, usually Ctrl + / key.

Modern mode, use strict

(Individually, based on the content and translation, it may be more appropriate to call it strict mode? But it is still based on tutorials.)
During the development of JavaScript, new features are constantly added, but the old features remain unchanged.
In order to be compatible with old versions of code, most of the new language features and modified features added in the ES5 specification do not take effect by default. We can use use strict to activate these features.

Use modern patterns in scripts

At the top of the script enter:

 'use strict';或者是"use strict";

With this code at the top of the script, the entire script will work in “modern mode”.

Note:

  1. This code must appear at the very top of the script when modern mode is enabled.
  2. Once this mode is enabled, it cannot be canceled.

Use modern mode in browser console

Like scripting, the browser console also does not enable modern mode by default.
If we need to enable this mode, we also need to put use strict at the top of the code.

 'use strict';或者是"use strict";//输入完成之后使用`Shift + Enter`换行//然后输入你需要运行的代码//输入完成之后使用`Enter`以运行代码

If it cannot be enabled normally, such as in older browsers, you can use this code to enable modern mode:

 (function() { 'use strict'; // ...你的代码... })()

Thinking: Should use strict be enabled?

  1. First, the code specification has changed, and we need to adapt our code to the latest standard to avoid huge code changes in case the browser or compiler no longer supports the old specification or standard.
  2. As the tutorial says, modern JavaScript supports “class” and “module” — high-level language constructs that automatically enable use strict .
  3. Use use strict to strictly regulate your own code, and may have a better programming experience.

This article is reprinted from: https://quest.myxxts.club/archives/142/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment