赞
踩
let x = 10;
if (x === 10) {
let x = 20; // 这里的 x 和外面的 x 不是同一个变量
console.log(x); // 20
}
console.log(x); // 10
const y = 5;
// y = 10; // 会抛出错误,因为y是一个常量
let name = "world";
console.log(`Hello, ${name}!`); // "Hello, world!"
let add = (a, b) => a + b;
console.log(add(2, 3)); // 5
// 数组解构
let [a, b] = [1, 2];
console.log(a); // 1
console.log(b); // 2
// 对象解构
let {firstName, lastName} = {firstName: "John", lastName: "Doe"};
console.log(firstName); // John
console.log(lastName); // Doe
let promise = new Promise(function(resolve, reject) {
setTimeout(() => resolve("done"), 1000);
});
promise.then(
result => console.log(result), // "done"
error => console.log(error)
);
let arr = [1, 2, 3];
console.log(arr.includes(2)); // true
console.log(arr.includes(4)); // false
console.log(2 ** 3); // 8
async function asyncCall() {
console.log('calling');
let result = await new Promise((resolve) => {
setTimeout(() => resolve("resolved"), 1000)
});
console.log(result);
}
asyncCall();
const object = { a: 1, b: '2', c: true };
console.log(Object.values(object)); // [1, '2', true]
const object = { a: 1, b: '2', c: true };
console.log(Object.entries(object)); // [['a', 1], ['b', '2'], ['c', true]]
let str = "5";
console.log(str.padStart(2, "0")); // "05", 填充至长度2,用"0"填充
let str = "5";
console.log(str.padEnd(2, "0")); // "50", 填充至长度2,用"0"填充
const obj = {
property1: 42
};
const descriptors = Object.getOwnPropertyDescriptors(obj);
console.log(descriptors.property1.writable); // true
console.log(descriptors.property1.value); // 42
function myFunc(
param1,
param2, // 尾部逗号
) {
// 函数体
}
myFunc(
1,
2, // 尾部逗号
);
async function* asyncGenerator() {
let i = 0;
while (i < 3) {
// 模拟异步操作
await new Promise(resolve => setTimeout(resolve, 100));
yield i++;
}
}
(async () => {
for await (let num of asyncGenerator()) {
console.log(num);
}
})();
new Promise((resolve, reject) => {
// 异步操作
setTimeout(() => resolve("Success"), 1000);
})
.then(result => console.log(result))
.catch(error => console.error(error))
.finally(() => console.log("Promise is settled."));
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
console.log(x); // 1
console.log(y); // 2
console.log(z); // { a: 3, b: 4 }
let n = { x, y, ...z };
console.log(n); // { x: 1, y: 2, a: 3, b: 4 }
let arr = [1, [2, [3, [4]]]];
console.log(arr.flat()); // 默认深度为1, 结果: [1, 2, [3, [4]]]
console.log(arr.flat(2)); // 指定深度为2, 结果: [1, 2, 3, [4]]
console.log(arr.flat(Infinity)); // 指定深度为Infinity, 结果: [1, 2, 3, 4]
let greeting = ' Hello world! ';
console.log(greeting.trimStart()); // "Hello world! "
console.log(greeting.trimEnd()); // " Hello world!"
let entries = [['name', 'John'], ['age', 30]];
let obj = Object.fromEntries(entries);
console.log(obj); // { name: "John", age: 30 }
let bigInt = 1234567890123456789012345678901234567890n;
console.log(bigInt + 1n); // 1234567890123456789012345678901234567891n
(async () => {
if (condition) {
const module = await import('./module.js');
module.doSomething();
}
})();
let promises = [
Promise.resolve(33),
new Promise(resolve => setTimeout(() => resolve(66), 0)),
Promise.reject(new Error('an error'))
];
Promise.allSettled(promises)
.then(results => results.forEach((result) => console.log(result.status)));
let quote = 'To be or not to be';
console.log(quote.replaceAll('be', 'test')); // "To test or not to test"
let x = 0;
let y = 1;
x &&= 2; // x = x && 2
console.log(x); // 0
y ||= 2; // y = y || 2
console.log(y); // 1
let z = null;
z ??= 2; // z = z ?? 2
console.log(z); // 2
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。