JS内置类型

image.png

数组

Array.prototype.map()

创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值。

  1. const array1 = [1, 4, 9, 16];
  2. // pass a function to map
  3. const map1 = array1.map(x => x * 2);
  4. console.log(map1);
  5. // expected output: Array [2, 8, 18, 32]

Array.prototype.reduce()

  1. arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
  1. const arr = [1,2,3,4,5]
  2. const sum = arr.reduce((x,y) => x + y);
  3. console.log(sum)
  4. //15

Arguments 对象

  1. function func1(a, b, c) {
  2. console.log(arguments[0]);
  3. // expected output: 1
  4. console.log(arguments[1]);
  5. // expected output: 2
  6. console.log(arguments[2]);
  7. // expected output: 3
  8. }
  9. func1(1, 2, 3);

应用:
遍历参数求和

  1. function add() {
  2. var sum =0,
  3. len = arguments.length;
  4. for(var i=0; i<len; i++){
  5. sum += arguments[i];
  6. }
  7. return sum;
  8. }
  9. add() // 0
  10. add(1) // 1
  11. add(1,2,3,4); // 10

window.setTimeout

<p>Live Example</p>
<button onclick="delayedAlert();">Show an alert box after two seconds</button>
<p></p>
<button onclick="clearAlert();">Cancel alert before it happens</button>
var timeoutID;

function delayedAlert() {
  timeoutID = window.setTimeout(slowAlert, 2000);
}

function slowAlert() {
  alert('That was really slow!');
}

function clearAlert() {
  window.clearTimeout(timeoutID); //可以用clearTimeout()取消定时器
}

Math.trunc

是内建的去除小数部分的方法

.toString(base)

方法 num.toString(base) 返回在给定 base 进制数字系统中 num 的字符串表示形式。

base=36 是最大进制,数字可以是 0..9 或 A..Z。所有拉丁字母都被用于了表示数字。对于 36 进制来说,一个有趣且有用的例子是,当我们需要将一个较长的数字标识符转换成较短的时候,例如做一个短的 URL。可以简单地使用基数为 36 的数字系统表示

舍入

  • Math.floor 向下
  • Math.ceil 向上
  • Math.trunc 移除小数点后面的内容
  • Math.round 舍入到最近的值

    Object.is

    有一个特殊的内建方法 Object.is,它类似于 === 一样对值进行比较,但它对于两种边缘情况更可靠:
  1. 它适用于 NaN:Object.is(NaN,NaN) === true,这是件好事。
  2. 值 0 和 -0 是不同的:Object.is(0,-0) === false,从技术上讲这是对的,因为在内部,数字的符号位可能会不同,即使其他所有位均为零。

在所有其他情况下,Object.is(a,b) 与 a === b 相同。
这种比较方式经常被用在 JavaScript 规范中。当内部算法需要比较两个值是否完全相同时,它使用 Object.is(内部称为 SameValue)

parseInt 和 parseFloat

它们可以从字符串中“读取”数字,直到无法读取为止。如果发生 error,则返回收集到的数字。函数 parseInt 返回一个整数,而 parseFloat 返回一个浮点数

alert( parseInt('0xff', 16) ); // 255
alert( parseInt('ff', 16) ); // 255,没有 0x 仍然有效

alert( parseInt('2n9c', 36) ); // 123456

string

  • includes,startsWith,endsWith
  • indexOf, lastIndexOf
  • slice(start [, end]) 👈start大于end时会倒序输出,为负值时表示从后往前数的位数

    let str = "stringify";
    alert( str.slice(0, 5) ); // 'strin',从 0 到 5 的子字符串(不包括 5)
    alert( str.slice(0, 1) ); // 's',从 0 到 1,但不包括 1,所以只有在 0 处的字符
    
  • str.trim() —— 删除字符串前后的空格 (“trims”)。

  • str.repeat(n) —— 重复字符串 n 次。

数组

  • push 在末端添加一个元素.
  • pop 从末端取出一个元素.
  • shift 取出数组的第一个元素并返回(比较慢)
  • unshift 在数组的首端添加元素(比较慢)

    通常来说,我们不应该用 for..in 来处理数组,因为速度比较慢 用for更好

length 属性

  • 如果我们手动增加它,则不会发生任何有趣的事儿。但是如果我们减少它,数组就会被截断。该过程是不可逆的
  • 所以,清空数组最简单的方法就是:arr.length = 0

    不要使用 == 运算符比较数组

Array.prototype.splice()

array.splice(start[, deleteCount[, item1[, item2[, …]]]])
item1, item2, … 👉 要添加进数组的元素,从start 位置开始