Posted onEdited onInweb Symbols count in article: 176Reading time ≈1 mins.
Web API
REST - Representational State Transfer
SOAP - Simple Object Access Protocol
GraphQL - A query language for your API
difference between REST and SOAP
Feature
REST
SOAP
Protocol
Can use any protocol but typically uses HTTP/HTTPS
Uses HTTP, SMTP, TCP, and more
Standards
No official standard, uses HTTP methods (GET, POST, PUT, DELETE)
Official standard by W3C
Performance
Lightweight, less data overhead
Heavyweight, more data overhead due to XML usage
Message format
Can use multiple formats (JSON, XML, etc.)
Uses XML
Security
Uses web security standards
Uses WS-Security which is more robust
Transaction support
No official support
Official support
State management
Stateless
Stateful
说明:
SOAP by itself is a protocol, but it can use any protocol to transport its messages, REST is not a protocol, it typically uses HTTP/HTTPS.
REST is stateless which means each request from client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server. Session state is therefore kept entirely on the client.
asyncfunctioncopyFilesWithNumberedNames(sourceDir, targetDir) { try { // Create target directory if it does not exist await fs.mkdir(targetDir, { recursive: true });
// Get all files from the source directory const filesToCopy = awaitgetAllFiles(sourceDir);
// Copy each file with a new numbered name let fileNumber = 1; for (const file of filesToCopy) { const newFilename = `${fileNumber}${path.extname(file)}`; const targetPath = path.join(targetDir, newFilename); await fs.copyFile(file, targetPath); console.log(`Copied ${file} to ${targetPath}`); fileNumber++; }
console.log('All files have been copied successfully.'); } catch (err) { console.error('An error occurred:', err); } }
const sourceDirectory = 'xxx'; // Replace with your source directory path const targetDirectory = 'xxx'; // Replace with your target directory path
const str = 'foobarfoo'; console.log(str.replaceAll(/foo/, '')); // TypeError: String.prototype.replaceAll called with a non-global RegExp argument console.log(str.replaceAll(/foo/g, '')); // bar
Posted onEdited onInjavascript Symbols count in article: 925Reading time ≈3 mins.
Hoisting in JavaScript
JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables, classes, or imports to the top of their scope, prior to execution of the code.
Variable Hoisting
1 2
console.log(a); // undefined var a = 1;
The above code is equivalent to:
1 2 3
var a; console.log(a); // undefined a = 1;
注意变量提示只提升到它所在的作用域的顶部,而不是全局作用域的顶部。
1 2 3 4 5 6 7 8 9
functionouter() { console.log(a); // ReferenceError: a is not defined functioninner() { console.log(a); // undefined var a = 1; } inner(); } outer();
The above code is equivalent to:
1 2 3 4 5 6 7 8 9 10 11
functionouter() { console.log(a); // ReferenceError: a is not defined functioninner() { var a; // a is hoisted to the top of its enclosing function `inner`. console.log(a); // undefined a = 1; }
const x = 1; { console.log(x); // ReferenceError: Cannot access 'x' before initialization const x = 2; }
如果我们把const x = 2;注释掉,那么代码就可以正常运行,此时x使用的是外层的x。这说明const x = 2;是被提升的(进而掩盖了外层的x),只是在TDZ中不能访问。
1 2 3 4 5
const x = 1; { console.log(x); // 1 // const x = 2; }
Temporal dead zone (TDZ)
A variable declared with let, const, or class is said to be in a "temporal dead zone" (TDZ) from the start of the block until code execution reaches the place where the variable is declared and initialized. 下面的代码中,
1 2 3 4 5 6 7 8 9 10 11 12
// Temporal dead zone (TDZ), TDZ 从block开始的地方开始,到其定义的地方结束。 // 在TDZ中访问let定义的变量会产生ReferenceError。 // 而var定义的变量则不存在此问题,因为var有hoisting(变量提升) { // TDZ starts at beginning of scope console.log(bar); // undefined console.log(foo); // ReferenceError var bar = 1; let foo = 2; // End of TDZ (for foo) const xxx = 3; }