JavaScript Fundamentals¶
1. The JavaScript runtime¶
1.1 Dynamic typing¶
In JavaScript, “dynamic typing” means that the type of the variable itself is not fixed. Instead, the type is determined by the value that the variable currently holds at runtime.
let value = 10; // The current value is "number".
value = "hello"; // The value is now a string.
value = true; // The value is now a boolean.
A variable can hold values of different types over time. Use typeof to inspect a value at runtime:
console.log(typeof 10); // "number"
console.log(typeof "hello"); // "string"
console.log(typeof true); // "boolean"
It should be noted that “dynamic type” does not mean “no type”. In JavaScript, values like still have a specific type, such as. For example, number, string, boolean, object, etc. are all specific types. It’s just that there isn’t a permanent binding between variables and types.
It’s very flexible, but it can also lead to errors being exposed only during runtime.
function add(a, b) {
return a + b;
}
add(1, 2); // 3
add("1", 2); // “12”, because of the type conversion that occurred.
Therefore, TypeScript is commonly used in actual development to add static type checking on top of JavaScript.
1.2 The eight JavaScript value types¶
There are seven basic types in JavaScript, along with one object type.
- Basic types: undefined, null, boolean, number, bigint, string, symbol
- Object type: object
The types of the following objects are all object:
However, the function is a bit special:
In terms of the language model, the function still belongs to the object. However, typeof is specifically used to return "function" for the function.
Common typeof results:
typeof 123 // "number"
typeof "hello" // "string"
typeof true // "boolean"
typeof undefined // "undefined"
typeof 10n // "bigint"
typeof Symbol() // "symbol"
typeof {} // "object"
typeof [] // "object"
typeof function(){} // "function"
A legacy issue from the past: the value returned by typeof null is "object". This doesn’t mean that null is actually an object. It’s a behavior that remained from the early design of JavaScript.
Therefore, to determine whether null is null, you can simply write: value === null.
To determine whether a value is an array, use: Array.isArray(value). Do not use typeof value === "array", as this condition will never be true.
(1) The difference between undefined and null
Among them, both undefined and null can represent “no value”, but their semantics are different.
undefined: Usually means "not provided" or "not yet assigned". Refer specifically to the following examples of codes:
let result;
console.log(result); // undefined
function greet(name) {
console.log(name);
}
greet(); // undefined
null: Usually, it is an empty value set by the developer voluntarily.
In summary, it can be understood as follows:
- undefined: There is no value here, or value is not provided.
- null: This is explicitly set to null.
1.3 const and let¶
Both const and let are used to state variables, the core difference being whether the variable can be revalued.
However, the object or array denoted by const can still have its content modified.
const user = { name: "小明" };
user.name = "小红"; // Yeah.
const numbers = [1, 2];
numbers.push(3); // Yeah.
Do not replace the entire variable with another object or array:
Simple comprehension:
let: Used when re-valued after variable.const: The variable is used when it is not revalued.
Priority is usually given to const and let is used when determining the need for re-allocation.
There are also some commonalities between the two:
- All are block-level scopes.
- Neither of them can be declared repeatedly within the same scope.
- Any access before the declaration will result in an error.
constThe declaration must be given immediate value:
1.4 Strict and loose equality¶
Core distinction:
==: It’s a loose equality comparison. The types may be automatically converted before the comparison.===: Exact equality. No type conversion is performed. Both the type and the value are compared.
More examples:
0 == false // true
0 === false // false
"" == false // true
"" === false // false
null == undefined // true
null === undefined // false
The comparison between objects and arrays is based on whether the references are the same:
[1, 2] === [1, 2] // False. Two different array objects.
const a = [1, 2];
const b = a;
a === b // true, pointing to the same array.
In actual development, it is recommended to use === and !== by default. This makes the results clearer and helps avoid unexpected situations caused by implicit type conversions.
Only use == when it is indeed necessary to have relaxed comparison rules and when the result of type conversion is clear.
2. Scope and Closures¶
2.1 What is scope?¶
The scope determines where a variable can be accessed.
const appName = "Demo";
function start() {
const version = "1.0";
console.log(appName);
console.log(version);
}
start();
console.log(appName);
// console.log(version); // ReferenceError
Here:
appNameis located in the outer field, and the internal function can access it.versionThe function external cannot access the funnel shop located atstart.
It can be painted as:
When JavaScript looks for a variable, it starts looking from the current position to the outward layer:
This lookup relationship is called the scope chain.
Common scopes in JavaScript
- In the global scope, declared at the outermost layer of a regular script:
In real projects, modules are typically used. Therefore, each module has its own top-level scope, preventing all variables from actually entering the global environment.
- The scope of a function, parameters of a function, and variables declared within the function belong to the function itself:
Here, price, count and total can only be accessed within calculate.
- Block shop, formed by a pair of brackets, for example:
if (true) {
const message = "hello";
let count = 1;
}
// The variables `message` and `count` are not accessible here.
(2) Variable masking
Inner scopes can declare variables with the same name as those in the outer scope:
const config = "global config";
function initialize() {
const config = "local config";
console.log(config);
}
initialize();
console.log(config);
Output:
function config covers the outer layer config. This is called variable masking, or shadowing.
(3) The scope differences of var, let, and const
Modern projects mainly use const and let but var may still appear in old projects, compile output and depend on codes.
let and const are block-level scopes.
if (true) {
let count = 1;
const name = "Alice";
}
// You cannot access the `count` and `name` variables here.
var is function shop
The output result is: 1
The brackets of if do not limit var. In this example, count belongs to the entire test function.
If it is not surrounded by a function:
This is also why modern code usually avoids using var: its scope boundaries are not intuitive enough.
2.2. Variable Hoisting¶
JavaScript deals with declarations in a field before it is implemented.
Therefore, some declarations can be used before they appear in the code. This phenomenon is commonly referred to as hoisting.
However, the improvement behaviors of different declarations are different.
(1) The promotion of function declarations
When using function declarations, you can call the function before declaring it:
You can roughly think of it as follows: When the scope is entered, JavaScript has already created the greet function.
This behavior is often used to place the main logic above the file, while placing the auxiliary functions below the file:
export function processData(data) {
const normalized = normalize(data);
return validate(normalized);
}
function normalize(data) {
return data.trim();
}
function validate(data) {
return data.length > 0;
}
Even if the helper functions are written later, the processData in front can still call them.
(2) Function expressions do not work the same way as function declarations.
The following code will cause an error:
The same applies to arrow functions:
Since what is being upgraded here is the variable declaration, rather than assigning function values to variables in advance.
The essence of these two writings is:
Only after the assignment statement is executed does greet actually store the value.
So, it should be written as:
2.3 What is a closure?¶
Let’s look at an example where a function returns another function:
function createCounter() {
let count = 0;
return function increment() {
count += 1;
return count;
};
}
const counter = createCounter();
console.log(counter()); // 1
console.log(counter()); // 2
console.log(counter()); // 3
According to common intuition, createCounter() has already been executed, and its local variable count should also disappear.
However, the returned increment function still references count. Therefore, count will continue to exist.
This is what a closure is: a function plus the outer lexical environment that it has access to when defining that function.
It can be imagined as follows:
As long as counter is still accessible, this closure and the outer variable it needs can still be accessed.
(1) Each function call generates a new lexical environment.
function createCounter() {
let count = 0;
return function () {
count += 1;
return count;
};
}
const first = createCounter();
const second = createCounter();
console.log(first()); // 1
console.log(first()); // 2
console.log(second()); // 1
first and second do not share count.
Since createCounter() is called twice, a new local environment will be created each time.
In other words, each call results in a separate state.
(2) The classic closure problem of var in the loop
const functions = [];
for (var i = 0; i < 3; i++) {
functions.push(function () {
console.log(i);
});
}
functions[0]();
functions[1]();
functions[2]();
The output is 3, 3, 3.
The reason is:
varis function shop, not black shop.- The three functions that reference the same
iare all closures. - After the loop ends, this
ihas been replaced by3. - When the functions are called, they read the current value of
i, not the snapshot taken when the functions were created.
Use let:
const functions = [];
for (let i = 0; i < 3; i++) {
functions.push(function () {
console.log(i);
});
}
The output is: 0, 1, 2
When using let in the loop within for, JavaScript will create a binding for the loop variable for each iteration.
3. Functions¶
3.1. How to create functions¶
(1) Function Declaration
(2) Function Expressions
First, a function value is created, and then it is assigned to the variable add.
Since add is a variable of type const, it cannot be called before being assigned to anything.
(3) Arrow functions
If the function body consists of only one expression, the curly braces and return can be omitted.
When there is only one parameter, the parameter parentheses can be omitted:
Where no parameters or multiple parameters are available, brackets must be retained:
3.2 Rest parameters with ...args¶
The remaining parameters can be collected into an array for convenience:
function sum(...numbers) {
let total = 0;
for (const number of numbers) {
total += number;
}
return total;
}
sum(1, 2, 3); // 6
sum(1, 2, 3, 4, 5); // 15
numbers is a true array:
The remaining parameters can also be used together with regular parameters:
function log(level, ...messages) {
console.log(level, messages);
}
log("info", "Server started", "Port:", 3000);
The parameters received for level and messages are as follows:
The remaining parameters must be the last parameter.
3.3 Return values¶
function greet() {
console.log("Hello");
}
const result = greet();
console.log(result); // undefined
3.4. Callback functions¶
The function passed as a parameter to another function is usually called a callback function.
execute is the receiving function, anrow operation is a callback.
4. How this is bound¶
this is one of the most misunderstood mechanisms in JavaScript.
Remember one important point: the this keyword in regular functions is usually determined by the way the function is called. In contrast, the this in arrow functions is determined by the location where the function is defined.
4.1. The this keyword for regular functions¶
Consider an object:
const user = {
name: "Alice",
introduce() {
console.log(this.name);
}
};
user.introduce(); // "Alice"
The call point is located to the left of user. Therefore, in this call:
It can be approximated as follows:
However, the introduce function does not permanently belong to user.
When the same function is called by different objects, the value of this also changes.
4.2. The this keyword in arrow functions¶
The most important sentence in an arrow function is:
Arrow functions do not have their own
this. Instead, they use thethisfrom the outer scope in which they are defined.
function createArrow() {
return () => {
console.log(this.name);
};
}
const user = {
name: "小明",
createArrow
};
const fn = user.createArrow();
fn(); // Xiao Ming
The execution process is as follows:
- Call
user.createArrow() createArrowis a regular function, so itsthisisuser.- When the arrow function is created, the
thisincreateArrowis captured. - Even if
fn()is executed separately later on, it still uses the previously captureduser.
(1) Pitfalls in using arrow functions in object methods
The object literal {} does not create a new this scope. This arrow function will capture the this outside of the object, rather than the user.
It can be approximated as follows:
const outerThis = this;
const user = {
name: "小明",
sayName: () => {
console.log(outerThis.name);
}
};
The correct way to write it is to use the regular method:
Or:
(2) Arrow functions are suitable for nested callbacks.
One of the important issues that arrow functions initially addressed was that callback functions would lose the outer this context.
For issues related to regular function callbacks, please refer to the following code:
const user = {
name: "小明",
introduce() {
setTimeout(function () {
console.log(this.name);
}, 1000);
}
};
user.introduce();
When user.introduce() is called, this in introduce is equal to user.
However, the function passed to setTimeout is another ordinary function. When it is called by a timer, it doesn’t go through:
The call is made, so its this is no longer user.
However, using arrow functions can solve this problem:
const user = {
name: "小明",
introduce() {
setTimeout(() => {
console.log(this.name);
}, 1000);
}
};
user.introduce(); // Output “Xiao Ming” after 1 second.
Here:
introduceis a regular method.user.introduce()Makethisinintroduceuser- The timer arrow function captures
thisfromintroduce. - Therefore,
usercan still be accessed during the callback.
In the past, when arrow functions weren’t available, this was often written like this:
const user = {
name: "小明",
introduce() {
const self = this;
setTimeout(function () {
console.log(self.name);
}, 1000);
}
};
5. Prototypes, prototype chains, and classes¶
JavaScript is a “prototype-based” language. Even when using class, the underlying mechanism still primarily relies on prototypes and the prototype chain to achieve property sharing, method reuse, and inheritance.
Summarize it in one sentence:
Each ordinary object can be associated with another object as its prototype. When accessing an object’s properties, if the property does not exist within the object itself, JavaScript will continue searching along the prototype chain until the property is found or it reaches
null. This search path is known as the prototype chain.
5.1. What is a prototype?¶
Each ordinary object usually has an internal prototype link.
It can be approximated as:
user
├── Own property: name
└── [[Prototype]] ──→ Object.prototype
├── toString
├── valueOf
├── hasOwnProperty
└── ...
[[Prototype]] is the internal concept used in the norm and not the attribute name used directly by the ordinary code.
Through:
Reading the prototype of the object:
(1) The process of finding attributes¶
When executed:
JavaScript will:
- Check whether
useritself containsname. - Return directly to
"Alice"when found
When executed:
JavaScript will:
- Check whether
useritself containstoString. - No such item was found.
- Go to the prototype of
user - Find
toStringinObject.prototype. - Return this function.
This is the prototype chain attribute search.
If there is no such prototype currently, continue searching upwards until you encounter:
The complete path is roughly as follows:
If null is still not found after reaching it, then return:
(2) The difference between in and Object.hasOwn()¶
Please first look at the following sample code:
const parent = {
type: "父对象"
};
const child = Object.create(parent);
child.name = "子对象";
console.log("name" in child); // true
console.log("type" in child); // true
console.log("toString" in child); // true
console.log(Object.hasOwn(child, "name")); // true
console.log(Object.hasOwn(child, "type")); // false
console.log(Object.hasOwn(child, "toString")); // false
- Use
into check: the object itself and the entire prototype chain. - Use
Object.hasOwn()to check only the object itself.
(3) Attribute masking¶
Objects can define their own properties with the same name as the prototype properties:
When searching for toString:
- Find it on your own at
user - Do not continue searching
Object.prototype.toString
It's called attribute cover.
If the attribute itself is deleted:
Access again:
The version on the prototype will be found.
(4) How to obtain the prototype of an object?¶
Recommended for use:
For example:
It can also be done through:
Read it, but __proto__ is a legacy access method and is not recommended to be used in official code.
Recommended for writing:
However, frequent use of Object.setPrototypeOf() to modify the prototype of existing objects may affect JavaScript engine optimization. When designing object structures, it is usually necessary to determine the prototype during the creation phase.
5.2. Manually creating prototype relationships¶
(1), Object.create¶
Object.create() functions as to create a new object and point the new object __proto__ (internal prototype pointer) to the transferred parameter object.
See the code below:
const animal = {
breathe() {
console.log("Breathing");
}
};
const dog = Object.create(animal);
dog.name = "Lucky";
dog.bark = function () {
console.log("Woof");
};
Prototype relationships:
5.3, Prototype Chain¶
(1) Multi-layer prototype chain¶
For more details, see the code below:
function User(name) {
this.name = name;
}
User.prototype.introduce = function () {
console.log(this.name);
};
const alice = new User("Alice");
The prototype chain is roughly as follows:
Reading: alice.name, found at alice.
Reading: alice.introduce, found at User.prototype.
Reading: alice.toString, in:
(2) The array’s prototype chain¶
items did not save a copy of map:
However, you can call it like this:
Because the prototype chain is roughly like this:
items
↓
Array.prototype
├── map
├── filter
├── reduce
├── push
└── ...
↓
Object.prototype
├── toString
└── ...
↓
null
Verify:
This explains why all arrays can share array methods, without the need for each array to store a separate map.
(3) The prototype chain of the function¶
greet is itself a function object, so:
This is why it can be called:
Because these methods usually come from:
The relationship is roughly as follows:
(4) The instanceof check for the prototype chain¶
Results:
The core issue with instanceof is: does the prototype of the constructor appear in the prototype chain of the object?
For example: dog instanceof Animal
Approximate check: Does Animal.prototype exist in the prototype chain of dog?
Because:
So the result is true.
5.4, class¶
(1) Basic class definitions¶
See the sample code below:
class User {
//
负责初始化实例自身属性
constructor(name) {
this.name = name;
}
//The introduce function will be defined as follows: User.prototype
introduce() {
console.log(`I am ${this.name}`);
}
}
const alice = new User("Alice");
alice.introduce();
It can be verified as follows:
Object.hasOwn(alice, "name"); // true
Object.hasOwn(alice, "introduce"); // false
Object.hasOwn(User.prototype, "introduce"); // true
So:
(2), extends¶
For more details, please refer to the code below:
class Animal {
constructor(name) {
this.name = name;
}
introduce() {
console.log(`I am ${this.name}`);
}
move() {
console.log(`${this.name} is moving`);
}
}
class Dog extends Animal {
bark() {
console.log(`${this.name}: Woof`);
}
}
const dog = new Dog("Lucky");
dog.introduce();
dog.move();
dog.bark();
Dog does not define introduce and move, but examples can be found through prototype plan.
The prototype relationships are roughly as follows:
(3) Subclass constructors and super()¶
Subcategory constructor must call before using this:
super(name) Calls the parent class constructor and initializes the current instance using the parent class.
Note that it does not create a separate instance of Animal and then insert it into Dog.
Dog There are still only one examples:
Same dog instance
├── name initialized by the Animal constructor
└── breed initialized by the Dog constructor
(4) Getters and Setters¶
An example of a getter function is as follows:
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
get area() {
return this.width * this.height;
}
}
const rectangle = new Rectangle(4, 5);
console.log(rectangle.area); // 20
The access form appears to be property: rectangle.area, but in reality, it is a getter function that is called.
Do not write it as: rectangle.area(). The Getter is suitable for calculating derived properties.
An example of the setter function is as follows:
class User {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
set name(value) {
this._name = value.trim();
}
}
const user = new User("Alice");
user.name = " Bob ";
console.log(user.name); // "Bob"
Assigning values: user.name = "Bob"; will call the setter function.
Reading: user.name will trigger a getter method.
_name just name deal, not really private.
6. Arrays, collections, iterators¶
6.1 Array basics¶
const users = ["Alice", "Bob", "Charlie"];
// Arrays access its elements through numerical indices:
users[0]; // "Alice"
users[1]; // "Bob"
users[2]; // "Charlie"
// Arrays are also objects:
typeof users // "object"
// To determine whether a value is an array, use Array.isArray.
Array.isArray(users) // true
6.2 Methods that modify the original array¶
(1) push and pop¶
push: Adds an element at the end of the array and returns the new length:
const items = ["a", "b"];
// Note that the `push` method returns the length, not the array itself.
const length = items.push("c");
console.log(items); // ["a", "b", "c"]
console.log(length); // 3
pop: Removes and returns the last element.
const items = ["a", "b", "c"];
const last = items.pop();
console.log(last); // "c"
console.log(items); // ["a", "b"]
(2) unshift and shift¶
unshift: Adds an element at the beginning of the array.
shift: Remove and return the first element.
const items = ["a", "b", "c"];
const first = items.shift();
console.log(first); // "a"
console.log(items); // ["b", "c"]
Since other elements at the beginning usually require reindexing, using shift and unshift frequently to handle large arrays may be more costly than operating at the end.
(3), splice¶
splice Elements may be removed, inserted or replaced and the original array may be modified.
Delete:
const items = ["a", "b", "c", "d"];
const removed = items.splice(1, 2);
console.log(removed); // ["b", "c"]
console.log(items); // ["a", "d"]
Insert:
Replace:
(4), sort¶
sort will modify the original array:
const numbers = [3, 1, 2];
const result = numbers.sort();
console.log(numbers); // [1, 2, 3]
console.log(result === numbers); // true
The numerical sorting should provide a comparison function:
Ascending order:
Descending:
6.3 Methods to Return a New Array¶
(1), slice¶
slice Intercepts some elements, returns a new array, does not modify the original array.
const items = ["a", "b", "c", "d"];
const selected = items.slice(1, 3);
console.log(selected); // ["b", "c"]
console.log(items); // ["a", "b", "c", "d"]
The range includes the starting index, but does not include the ending index.
(2), concat¶
const first = [1, 2];
const second = [3, 4];
const combined = first.concat(second);
console.log(combined); // [1, 2, 3, 4]
You can also use the expansion syntax:
Neither approach would modify the original array.
6.4 Array transformations¶
(1), map¶
map converts each element into a new value and returns a new array.
const numbers = [1, 2, 3];
const doubled = numbers.map(number => number * 2);
console.log(doubled); // [2, 4, 6]
Convert object:
Conversion structure:
The return value of the callback map is very important.
// Example of error
const names = users.map(user => {
user.name;
});
// Since there is no return statement, the result is:
[undefined, undefined, ...]
// Correct example
const names = users.map(user => {
return user.name;
});
// Or:
const names = users.map(user => user.name);
(2), filter¶
filter Elements that return a true value in the callback are retained as is.
The original array remains unchanged and returns the new array.
Common ways to handle null values for filtering:
const values = [0, 1, null, 2, undefined, 3];
const result = values.filter(Boolean);
// Results
[1, 2, 3]
(3), flat¶
Only one level is expanded by default:
Specify the depth:
Full rollout of common writing:
However, structures that are very deep or have circular references require careful handling.
(4), flatMap¶
flatMap is roughly equivalent to first map, and then expanding one layer further:
const sentences = [
"hello world",
"javascript language"
];
const words = sentences.flatMap(
sentence => sentence.split(" ")
);
Results:
Approximate equivalent:
6.5, Map¶
(1) The basic methods for Map¶
Add or update:
Reading:
Determine whether the key exists:
Delete:
Clear:
Quantity:
6.6 Iterators¶
(1) The iterator next()¶
An iterator is an object that has a next() method.
Each call returns:
For example:
const iterator = ["a", "b"][Symbol.iterator]();
iterator.next();
// { value: "a", done: false }
iterator.next();
// { value: "b", done: false }
iterator.next();
// { value: undefined, done: true }
for...of The internal call will be kept to next() until:
(2), Symbol.iterator¶
An object that has:
And since this method returns an iterator, it is considered an iterable object.
Manual implementation:
const range = {
start: 1,
end: 3,
[Symbol.iterator]() {
let current = this.start;
const end = this.end;
return {
next() {
if (current <= end) {
const value = current;
current += 1;
return {
value,
done: false
};
}
return {
value: undefined,
done: true
};
}
};
}
};
Now it can be done:
(3) Iterable objects and iterators are not exactly the same concept.¶
- Iterable objects: objects that can create iterators.
- Iterator: Generates results one by one using next().
Arrays are iterable objects:
Call:
Get an iterator.
An iterable object can be traversed multiple times, as a new iterator is created each time.
And a specific iterator is usually stateful and moves forward in a single direction:
It remembers the current traversal position.
7. Modules and project structure¶
7.1. Why are modules necessary?¶
If all the code is written in one file:
const users = [];
const config = {};
const cache = new Map();
function createUser() {
}
function deleteUser() {
}
function startServer() {
}
As the project grows, the following issues will occur:
- Variable name conflicts occurred.
- The internal implementation is exposed.
- The responsibilities of the files are mixed up.
- The dependency relationships are difficult to understand.
- Testing is difficult.
- It’s unclear what changes will be made to the scope of influence.
The module breaks the program into several files and explicitly defines:
For example:
Each file has its own module scope.
Other modules can only access the exported config. They cannot directly access the internal secret.
7.2, ES Modules¶
ES Modules, abbreviated as ESM, is a standard JavaScript module system that uses import and export.
Node.js currently supports ESM in a stable manner, while also retaining the CommonJS module system. Node.js ESM Official Documentation
7.3 Named exports¶
// math.js
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
export const PI = 3.14159;
Import:
The names in the parentheses must correspond to the exported names:
The module loading usually fails when the following names do not exist:
(1) Declare first, and then export them uniformly.¶
The following two writings function in the same way.
Export directly:
Harmonized export:
In real projects, the implementation is often defined first, and then exported collectively at the bottom of the file.
(2). Rename at Import¶
Here:
Usage:
Often used to resolve naming conflicts:
import {
create as createUser
} from "./user.js";
import {
create as createOrder
} from "./order.js";
(3) Rename on Export¶
What is seen in other files is:
Won't see the name internalAdd.
7.4 Default Export¶
(1), export default¶
A module can have at most one default export:
// logger.js
export default function createLogger() {
return {
log(message) {
console.log(message);
}
};
}
Do not use brackets when importing default export:
The default import name is determined by the party that imports it.
Both makeLogger and createLogger are acceptable, as the default export does not require the importer to use a fixed local name.
(2) Default export and named export can exist simultaneously.¶
// logger.js
export const LOG_LEVEL = "info";
export function formatMessage(message) {
return `[LOG] ${message}`;
}
export default function createLogger() {
}
Import:
Of which:
(3) The meaning of curly braces¶
// Indicates that the default export is to be imported.
import logger from "./logger.js";
// Indicates importing a named export whose name must be "logger".
import {
logger
} from "./logger.js";
The two are completely different.
Assumption:
It can only be imported in this way:
7.5. Namespace Import¶
import * as
Assume that math.js is exported as follows:
So, it can be:
math is a module namespace object, containing the exports of that module.
This notation is commonly used in tool modules:
However, namespace objects are not ordinary configuration objects that can be modified at will. The importing party cannot reassign the exported module.
7.6. Re-exporting¶
(1) Export directly from another module¶
This also completes the process of obtaining exports from user-service.js, and then exporting them to external locations through index.js.
If the current module does not itself need to use createUser, import before export is not necessary.
(2), export *¶
It will re-export the named exports of the target module. However, the default exports are not automatically re-exported by export *.
The default export requires explicit processing:
(3) Barrel files¶
The following aggregated output file is often referred to as “barrel”.
// services/index.js
export {
UserService
} from "./user-service.js";
export {
OrderService
} from "./order-service.js";
export {
PaymentService
} from "./payment-service.js";
Other files can be:
Or according to the parsing configuration:
7.7 Static and Dynamic Importing¶
(1) Static import¶
Static imports have a fixed syntax structure, and they cannot be written directly within ordinary conditional statements:
This is not a valid static import syntax. Static imports usually must be located at the top level of the module.
(2) Dynamic import()¶
Dynamic import is an expression that returns a Promise.
Import named export:
It can also be deconstructed:
Import default export:
(3) Conditional loading¶
if (config.enableAnalytics) {
const {
initializeAnalytics
} = await import("./analytics.js");
initializeAnalytics();
}
It is only loaded when the conditions are met. For example:
async function loadDatabase(driver) {
if (driver === "postgres") {
return import("./postgres-driver.js");
}
if (driver === "mysql") {
return import("./mysql-driver.js");
}
throw new Error(`Unsupported driver: ${driver}`);
}
7.8 package.json¶
package.json can be understood as the “ID + dependency list + command entry + tool configuration” of a front-end project.
npm, pnpm, Yarn, Node.js, Vite, Webpack, and various engineering tools will all read this. It must be in the strict JSON format: no comments allowed, no trailing commas, and attribute names must be enclosed in double quotes. For a complete list of fields in npm, refer to Documentation for package.json.
This is explained below in terms of the importance of the actual front-end projects.
(1) A typical front-end project configuration¶
Taking a Vue 3 + Vite + TypeScript project as an example:
{
"name": "my-vue-app",
"version": "1.0.0",
"private": true,
"description": "一个基于 Vue 3 和 Vite 的后台管理系统",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vue-tsc -b && vite build",
"preview": "vite preview",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"test": "vitest run",
"test:watch": "vitest",
"type-check": "vue-tsc --noEmit"
},
"dependencies": {
"axios": "^1.7.0",
"pinia": "^2.2.0",
"vue": "^3.5.0",
"vue-router": "^4.4.0"
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.1.0",
"@vue/tsconfig": "^0.5.0",
"eslint": "^9.0.0",
"typescript": "^5.6.0",
"vite": "^5.4.0",
"vitest": "^2.0.0",
"vue-tsc": "^2.1.0"
},
"engines": {
"node": ">=20.0.0",
"npm": ">=10.0.0"
},
"packageManager": "pnpm@9.12.0"
}
The actual project may not include all fields, but the most common ones are:
nameversionprivatescriptsdependenciesdevDependencies
(2) Basic project information¶
1. name¶
Name of the project or npm package:
If you plan to publish the package to npm, it must follow npm's package naming conventions. Typically, lowercase letters and hyphens are used for naming packages.
Namespace package names:
Here @my-company is the name of the organization or domain.
For regular frontend applications that are developed only within the company and not released to npm, the name is mainly used for:
- Identify the item;
- Displays in npm/pnpm command output;
- Reference to the Monorepo workspace;
- Generates construction information.
2. version¶
Project version:
It generally follows a semantic versioning approach, that is:
For example:
1.0.0: The first official version;1.0.1: Fixing the bug;1.1.0: Added backward-compatible new features;2.0.0: Major changes that are incompatible;2.0.0-beta.1: Test version.
If you want to publish an npm package, name and version are the most important identifier fields. The version must follow the semantic versioning format. Official npm description remains as it is.
3. private¶
This indicates that it is a private project, and the npm publish cannot be published to the npm public repository.
It is recommended to add the following to ordinary business front-end projects:
It can prevent developers from accidentally releasing company projects.
Note: The term “private” here does not mean that the code will be automatically encrypted, nor does it mean that others cannot access the code. It simply prevents npm from publishing the code.
4. description¶
It describes the functionality of the project. When the npm package is released, it will also be displayed in the npm search results.
5. keywords¶
It is mainly used for npm package searching. It can be omitted for regular business projects.
6. author¶
You can also use objects:
{
"author": {
"name": "Zhang San",
"email": "zhangsan@example.com",
"url": "https://example.com"
}
}
Team projects sometimes use:
7. license¶
Common licenses:
MITApache-2.0GPL-3.0ISCUNLICENSED
For projects that are internal to the company and not allowed to be used publicly, you can write:
(3) scripts: Project command entry¶
scripts is used to define the executable command for the project:
{
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"test": "vitest run",
"lint": "eslint ."
}
}
Means of implementation:
Or use pnpm:
npm will automatically add the node_modules/.bin to the command search path of the script. So you can write it directly:
Do not write:
npm supports any custom script, and pre, post Back and forth scripts.npm Scripts documentation
Common front-end scripts
{
"scripts": {
"dev": "vite",
"build": "vite build",
"build:test": "vite build --mode test",
"build:prod": "vite build --mode production",
"preview": "vite preview",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"format": "prettier --write .",
"type-check": "tsc --noEmit",
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage",
"test:e2e": "playwright test"
}
}
The colon here has no special syntax meaning. It's just a naming habit:
Make the relevant commands more hierarchical in appearance.
Lifecycle scripts
For example:
{
"scripts": {
"prebuild": "npm run lint",
"build": "vite build",
"postbuild": "node scripts/report-build.js"
}
}
Implementation:
npm will be executed in the following order:
It is necessary to use preinstall, install, and postinstall with caution, as they will be executed automatically during the installation of dependencies, which may lead to the following issues:
- The installation speed is slowing down.
- Cross-platform compatibility issues;
- CI build failed.
- Potential supply chain security risks.
(4) dependencies: Dependencies for production¶
This is usually the package needed to apply rantime, for example:
- Vue, React;
- Vue Router, React Router;
- Axios;
- Pinia, Redux;
- Date processing library;
- UI component library;
- Form validation library.
Install production dependencies:
Or:
The package manager will automatically update package.json. It is generally not recommended to manually add version numbers.
The criteria for judgment are as follows:
Will the final application lacking this dependency lose necessary functions?
If so, it should normally be placed at dependencies.
(5) devDependencies: Development dependencies¶
{
"devDependencies": {
"eslint": "^9.0.0",
"prettier": "^3.3.0",
"typescript": "^5.6.0",
"vite": "^5.4.0",
"vitest": "^2.0.0"
}
}
The tools used only during development, compilation, and testing phases are placed here. For example:
- Vite, Webpack;
- TypeScript;
- ESLint;
- Prettier;
- Vitest, Jest;
- Playwright;
- Babel;
- Various compilation plugins;
- Type declaration package.
Install development dependencies:
Abbreviation:
The equivalent of pnpm:
The official definition of npm is:
dependencies: Required for application production operations;devDependencies: Required for local development and testing.
For details, see Explanation of npm dependency categories:.
A place that can be easily misunderstood.
vue and axios will eventually be packed into static resources for the pure front end application of the Vite construction and will not be kept in the server node_modules as it is.
But they still belong to the functional dependencies of the application. They are usually placed in:
And Vite, ESLint, and TypeScript are build tools. They are placed as follows:
(6) Dependence on version symbols¶
For example:
Accurate Version
Only this version is accepted.
The advantage is that the results are more controllable. The downside is that it does not automatically obtain updates to the revised version.
^: Compatible sub-versions and revisions
For normal 1.0.0 and later versions, it is roughly equivalent to:
It can be upgraded:
It will not be upgraded to:
~ generally allows patch updates only.
It is roughly equivalent to:
It can be upgraded:
It cannot be upgraded to:
* or latest
Or use it during installation:
It is not recommended to use * as the dependency in formal projects, as different versions may be obtained during each installation.
The version range does not equal the actual installed version.
Assume that package.json contains the following value:
It represents the “allowed version range for installation”, while package-lock.json, pnpm-lock.yaml, or yarn.lock indicate the actual version that is resolved.
Therefore:
package.json: Specify what the project requires.- Lock file: Record what was specifically installed this time.
Team projects should usually submit a lock file.
(7), type: Module type¶
It mainly affects how Node.js interprets the .js file.
ES Module
At this time, the .js file uses ESM by default:
CommonJS
Or, if type is not written, then in a Node.js environment, .js will usually be processed according to CommonJS standards:
Modern Vite front-end projects typically use:
Note that it affects not only the business code, but also:
vite.config.jseslint.config.js- Node.js build script
- Test configuration file
In addition:
.mjsExpressed Es Module;.cjsmakes it clear that CommonJs.
(8) Node and package manager versions¶
engines
Indicates the operational environment version that the project expects to use.
It is also possible to limit the version range:
It should be noted that engines mainly serves as a reminder and constraint in many cases. It does not necessarily prevent the installation by default. Whether the installation will fail strictly depends on the configuration of the package manager.
packageManager
It indicates which package manager and its version should be used for the project.
Common forms of writing:
It can reduce conflicts in lock files caused by team members using npm, pnpm, and Yarn interchangeably.
It is recommended that a project only uses one type of lock file.
Do not submit three lock files at the same time.
8. Asynchronous JavaScript¶
8.1, Promise¶
(1) Promise represents future outcomes.¶
promise is not the final user data, but rather an object that represents future outcomes.
A Promise has three states:
The state transition can only occur once:
It cannot be changed again after completion.
(2) The Promise executor executes synchronously.¶
The function that receives new Promise is usually called executor.
console.log("A");
const promise = new Promise(resolve => {
console.log("B");
resolve("done");
});
console.log("C");
Output:
The executor executes immediately upon creating the Promise.
The asynchronous part is handled by the Promise handler function that is registered later:
8.2, async function¶
(1) The async function always returns a Promise.¶
async function getValue() {
return 42;
}
// The result is a Promise, not a number 42.
const result = getValue();
It is approximately equivalent to:
Read the result:
Or:
8.3, await¶
(1) await is waiting for the result of the Promise.¶
It can be approximated as follows:
However, async/await is usually more readable when it comes to synchronous code.