const arr = [1, 3, 5, 2, ‘8‘, NaN, -0]
arr.includes(1) // true
arr.includes(1, 2) // false 该方法的第二个参数表示搜索的起始位置,默认为0
arr.includes(‘1‘) // false
arr.includes(NaN) // true
arr.includes(+0) // true
if (arr.indexOf(el) !== -1) {
// ...
}
[NaN].indexOf(NaN)// -1
[1, 4, -5, 10].find((n) => n < 0) // -5
[1, 5, 10, 15].findIndex(function(value) {
return value > 9;
}) // 2
[NaN].findIndex(y => Object.is(NaN, y)) // 0
console.log(2**10);// 输出1024zhicc
console.log(Math.pow(2, 10)) // 输出1024
fetch(‘https://blog.csdn.net/‘)
.then(response => {
console.log(response)
return fetch(‘https://juejin.im/‘)
})
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error)
})
async function foo () {
try {
let response1 = await fetch(‘https://blog.csdn.net/‘)
console.log(response1)
let response2 = await fetch(‘https://juejin.im/‘)
console.log(response2)
} catch (err) {
console.error(err)
}
}
foo()
async function foo () {
return ‘浪里行舟‘
}
foo().then(val => {
console.log(val) // 浪里行舟
})
async function foo () {
return Promise.resolve(‘浪里行舟‘)
}
foo().then(val => {
console.log(val) // 浪里行舟
})
const obj = { foo: ‘bar‘, baz: 42 };
Object.values(obj) // ["bar", 42]
const obj = { 100: ‘a‘, 2: ‘b‘, 7: ‘c‘ };
Object.values(obj) // ["b", "c", "a"]
const obj = { foo: ‘bar‘, baz: 42 };
Object.entries(obj) // [ ["foo", "bar"], ["baz", 42] ]
const obj = { 10: ‘xxx‘, 1: ‘yyy‘, 3: ‘zzz‘ };
Object.entries(obj); // [[‘1‘, ‘yyy‘], [‘3‘, ‘zzz‘], [‘10‘: ‘xxx‘]]
String.padStart(targetLength,[padString])
‘x‘.padStart(4, ‘ab‘) // ‘abax‘
‘x‘.padEnd(5, ‘ab‘) // ‘xabab‘
‘12‘.padStart(10, ‘YYYY-MM-DD‘) // "YYYY-MM-12"
‘09-12‘.padStart(10, ‘YYYY-MM-DD‘) // "YYYY-09-12"
const obj = {
name: ‘浪里行舟‘,
get bar () {
return ‘abc‘
}
}
console.log(Object.getOwnPropertyDescriptors(obj))
const source = {
set foo (value) {
console.log(value)
},
get bar () {
return ‘浪里行舟‘
}
}
const target1 = {}
Object.assign(target1, source)
console.log(Object.getOwnPropertyDescriptor(target1, ‘foo‘))
const source = {
set foo (value) {
console.log(value)
},
get bar () {
return ‘浪里行舟‘
}
}
const target2 = {}
Object.defineProperties(target2, Object.getOwnPropertyDescriptors(source))
console.log(Object.getOwnPropertyDescriptor(target2, ‘foo‘))
// for of遍历
function Gen (time) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(time)
}, time)
})
}
async function test () {
let arr = [Gen(2000), Gen(100), Gen(3000)]
for (let item of arr) {
console.log(Date.now(), item.then(console.log))
}
}
test()
function Gen (time) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(time)
}, time)
})
}
async function test () {
let arr = [Gen(2000), Gen(100), Gen(3000)]
for await (let item of arr) {
console.log(Date.now(), item)
}
}
test()
// 1575536194608 2000
// 1575536194608 100
// 1575536195608 3000
const arr1 = [10, 20, 30];
const copy = [...arr1]; // 复制
console.log(copy); // [10, 20, 30]
const arr2 = [40, 50];
const merge = [...arr1, ...arr2]; // 合并
console.log(merge); // [10, 20, 30, 40, 50]
console.log(Math.max(...arr)); // 30 拆解
const input = {
a: 1,
b: 2,
c: 1
}
const output = {
...input,
c: 3
}
console.log(output) // {a: 1, b: 2, c: 3}
const input = {
a: 1,
b: 2
}
const output = {
...input,
c: 3
}
input.a=‘浪里行舟‘
console.log(input,output) // {a: "浪里行舟", b: 2} {a: 1, b: 2, c: 3}
const obj = {x: {y: 10}};
const copy1 = {...obj};
const copy2 = {...obj};
obj.x.y=‘浪里行舟‘
console.log(copy1,copy2) // x: {y: "浪里行舟"} x: {y: "浪里行舟"}
console.log(copy1.x === copy2.x); // → true
const input = {
a: 1,
b: 2,
c: 3
}
let { a, ...rest } = input
console.log(a, rest) // 1 {b: 2, c: 3}
fetch(‘https://www.google.com‘)
.then((response) => {
console.log(response.status);
})
.catch((error) => {
console.log(error);
})
.finally(() => {
document.querySelector(‘#spinner‘).style.display = ‘none‘;
});
console.log(/foo.bar/.test(‘foo\nbar‘)) // false
console.log(/foo.bar/s.test(‘foo\nbar‘)) // true
const re = /foo.bar/s // Or, `const re = new RegExp(‘foo.bar‘, ‘s‘);`.
console.log(re.test(‘foo\nbar‘)) // true
console.log(re.dotAll) // true
console.log(re.flags) // ‘s‘
const re = /(\d{4})-(\d{2})-(\d{2})/;
const match= re.exec(‘2019-01-01‘);
console.log(match[0]); // → 2019-01-01
console.log(match[1]); // → 2019
console.log(match[2]); // → 01
console.log(match[3]); // → 01
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = re.exec(‘2019-01-01‘);
console.log(match.groups); // → {year: "2019", month: "01", day: "01"}
console.log(match.groups.year); // → 2019
console.log(match.groups.month); // → 01
console.log(match.groups.day); // → 01
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
const usDate = ‘2018-04-30‘.replace(re, ‘$<month>-$<day>-$<year>‘)
console.log(usDate) // 04-30-2018
let test = ‘hello world‘
console.log(test.match(/hello(?=\sworld)/))
// ["hello", index: 0, input: "hello world", groups: undefined]
let test = ‘world hello‘
console.log(test.match(/(?<=world\s)hello/))
// ["hello", index: 6, input: "world hello", groups: undefined]
const str = ‘?‘;
console.log(/\d/u.test(str)); // → false
console.log(/\p{Number}/u.test(str)); // → true
const str = ‘?‘;
console.log(/\p{Alphabetic}/u.test(str)); // → true
// the \w shorthand cannot match ?
console.log(/\w/u.test(str)); // → false
console.log(/\P{Number}/u.test(‘?‘)); // → false
console.log(/\P{Number}/u.test(‘?‘)); // → true
console.log(/\P{Alphabetic}/u.test(‘?‘)); // → true
console.log(/\P{Alphabetic}/u.test(‘?‘)); // → false
newArray = arr.flat(depth)
// depth是指定要提取嵌套数组的结构深度,默认值为 1
const numbers1 = [1, 2, [3, 4, [5, 6]]]
console.log(numbers1.flat())// [1, 2, 3, 4, [5, 6]]
const numbers2 = [1, 2, [3, 4, [5, 6]]]
console.log(numbers2.flat(2))// [1, 2, 3, 4, 5, 6]
let arr = [1, 2, 3]
console.log(arr.map(item => [item * 2]).flat()) // [2, 4, 6]
console.log(arr.flatMap(item => [item * 2])) // [2, 4, 6]
const object = { x: 23, y:24 };
const entries = Object.entries(object); // [[‘x‘, 23], [‘y‘, 24]]
const result = Object.fromEntries(entries); // { x: 23, y: 24 }
// ES10之前
const obj = {
a: 21,
b: 22,
c: 23
}
console.log(Object.entries(obj)) // [[‘a‘,21],["b", 22],["c", 23]]
let arr = Object.entries(obj).filter(([a, b]) => b > 21) // [["b", 22],["c", 23]]
let obj1 = {}
for (let [name, age] of arr) {
obj1[name] = age
}
console.log(obj1) // {b: 22, c: 23}
// 用Object.fromEntries()来实现
const obj = {
a: 21,
b: 22,
c: 23
}
let res = Object.fromEntries(Object.entries(obj).filter(([a, b]) => b > 21))
console.log(111, res) // {b: 22, c: 23}
let str = ‘ 前端工匠 ‘
console.log(str.length) // 6
str = str.trimStart()
console.log(str.length) // 5
let str1 = str.trim() // 清除前后的空格
console.log(str1.length) // 4
str.replace(/^\s+/g, ‘‘) // 也可以用正则实现开头删除空格
let str = ‘ 浪里行舟 ‘
console.log(str.length) // 6
str = str.trimEnd()
console.log(str.length) // 5
let str1 = str.trim() //清除前后的空格
console.log(str1.length) // 4
str.replace(/\s+$/g, ‘‘) // 也可以用正则实现右端移除空白字符
function collectGroup1 (regExp, str) {
const matches = []
while (true) {
const match = regExp.exec(str)
if (match === null) break
matches.push(match[1])
}
return matches
}
console.log(collectGroup1(/"([^"]*)"/g, `"foo" and "bar" and "baz"`))
// [ ‘foo‘, ‘bar‘, ‘baz‘ ]
function collectGroup1 (regExp, str) {
let results = []
for (const match of str.matchAll(regExp)) {
results.push(match[1])
}
return results
}
console.log(collectGroup1(/"([^"]*)"/g, `"foo" and "bar" and "baz"`))
// ["foo", "bar", "baz"]
// ES10之前
try {
// tryCode
} catch (err) {
// catchCode
}
// ES10
try {
console.log(‘Foobar‘)
} catch {
console.error(‘Bar‘)
}
// 超过 53 个二进制位的数值,无法保持精度
Math.pow(2, 53) === Math.pow(2, 53) + 1 // true
// 超过 2 的 1024 次方的数值,无法表示
Math.pow(2, 1024) // Infinity
const aNumber = 111;
const aBigInt = BigInt(aNumber);
aBigInt === 111n // true
typeof aBigInt === ‘bigint‘ // true
typeof 111 // "number"
typeof 111n // "bigint"
Symbol(‘desc‘).description; // "desc"
Symbol(‘‘).description; // ""
Symbol().description; // undefined
function sum(a, b) {
return a + b;
}
console.log(sum.toString());
// function sum(a, b) {
// return a + b;
// }
原文:https://www.cnblogs.com/maxiag/p/13381949.html