js代码记录本

1
2
3
术语:
Immutable 不可改变的,返回新对象
Mutable 修改原对象

字符串

取字符串右边4位

  • [Immutable]

'123456'.slice(-4) // => 3456

数组

合并操作

  • [Immutable]

返回newArray
组合两个或两个以上的数组
array1.concat([item1[, item2[, . . . [, itemN]]]])

  • [Mutable]

push技巧
将新数组追加到一个数组
Array.prototype.push.apply(arrayObj, [item1])

移除操作

  • [Immutable]

返回newArray
返回一个数组中的一部分。
arrayObj.slice(start, [end])

  • [Mutable]

从一个数组中移除元素
arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])

从数组中移除最后一个元素并返回该元素。
arrayObj.pop( )

从数组中移除第一个元素并将返回该元素
arrayObj.shift( )

插入操作

  • [Mutable]

将新元素追加到一个数组
arrayObj.push([item1 [item2 [. . . [itemN ]]]])
用法:
my_array.push (5, 6, 7);

在数组的开头插入新元素。
arrayObj.unshift([item1[, item2 [, . . . [, itemN]]]])

找出数组中最大元素

Math.max.apply(this, targetArray)