随笔分类 - js
摘要:Array.prototype.flat() 用于将嵌套的数组“拉平”,变成一维的数组。 该方法返回一个新数组,对原数据没有影响。 var arr1 = [1, 2, [3, 4]]; arr1.flat(); // [1, 2, 3, 4] var arr2 = [1, 2, [3, 4, [5,
阅读全文
摘要:includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。 const array1 = [1, 2, 3]; console.log(array1.includes(2));// expected output: true const
阅读全文
摘要:keys()是对键名的遍历、 values()是对键值的遍历 entries()是对键值对的遍历。 for (let index of ['a', 'b'].keys()) { console.log(index); } // 0 // 1 for (let elem of ['a', 'b'].v
阅读全文
摘要:fill() 方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。 const array1 = [1, 2, 3, 4]; // fill with 0 from position 2 until position 4 console.log(array1.fill(0
阅读全文
摘要:findIndex() 返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。 const array1 = [5, 12, 8, 130, 44]; const isLargeNumber = (element) => element > 13; console.log(array1.fi
阅读全文
摘要:Array.of(7) 创建一个具有单个元素 7 的数组, Array(7) 创建一个长度为7的空数组(注意:这是指一个有7个空位(empty)的数组,而不是由7个undefined组成的数组)。 Array.of(7); // [7] Array.of(1, 2, 3); // [1, 2, 3]
阅读全文
摘要:slice() 方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。 const animals = ['ant', 'bison', 'camel', 'duck', 'elephant']; consol
阅读全文
摘要:let arrayLike = { '0': 'a', '1': 'b', '2': 'c', length: 3 }; // ES5的写法 var arr1 = [].slice.call(arrayLike); console.log(arr1)// ['a', 'b', 'c'] // ES6
阅读全文
摘要:扩展运算符 - push var arr1 = [0, 1, 2]; var arr2 = [3, 4, 5]; let a = Array.prototype.push.apply(arr1, arr2); console.log(a) // 6 console.log(arr1) // [0,
阅读全文
摘要:扩展运算符是三个点(...) console.log(...[1, 2, 3]) // 1 2 3 console.log(1, ...[2, 3, 4], 5) // 1 2 3 4 5 [...document.querySelectorAll('div')] // [<div>, <div>,
阅读全文
摘要:方法返回一个表示当前函数源代码的字符串。 function sum(a, b) { return a + b; } console.log(sum.toString()); // expected output: "function sum(a, b) { // return a + b; // }
阅读全文
摘要:try { // ... } catch (err) { // 处理错误 } try { // ... } catch { // ... }
阅读全文
摘要:function clownsEverywhere( param1, param2 ) { /* ... */ } clownsEverywhere( 'foo', 'bar' ); function clownsEverywhere( param1, param2, ) { /* ... */ }
阅读全文
摘要:某个函数的最后一步是调用另一个函数 function f(x){ return g(x); }函数f的最后一步是调用函数g,这就叫尾调用。 以下三种情况,都不属于尾调用 // 情况一 function f(x){ let y = g(x); return y; } // 情况二 function f
阅读全文
摘要:使用“箭头”(=>)定义函数 var f = v => v; // 等同于 var f = function (v) { return v; }; 需要多个参数,就使用一个圆括号代表参数部分 var f = () => 5; // 等同于 var f = function () { return 5
阅读全文
摘要:ES5 的name属性,会返回空字符串,而 ES6 的name属性会返回实际的函数名。 var f = function () {}; // 匿名函数 // ES5 f.name // "" // ES6 f.name // "f" const bar = function baz() {}; //
阅读全文
摘要:形式为...变量名 一个不定数量的参数表示为一个数组 function sum(...theArgs) { return theArgs.reduce((previous, current) => { return previous + current; }); } console.log(sum(
阅读全文
摘要:函数指定了默认值以后,函数的length属性,将返回没有指定默认值的参数个数。 (function (a) {}).length // 1 (function (a = 5) {}).length // 0 (function (a, b, c = 5) {}).length // 2 定义了 3
阅读全文
摘要:function log(x, y = 'World') { console.log(x, y); } log('Hello') // Hello World log('Hello', 'China') // Hello China log('Hello', '') // Hello 参数变量是默认
阅读全文
摘要:let a = 2 ** 2 let b = 2 ** 3 console.log(a) // 4 console.log(b) // 8 a **= 2; // 等同于 a = a * a; b **= 3; // 等同于 b = b * b * b;
阅读全文
浙公网安备 33010602011771号