❤️ javascript30是一系列的视频教程,旨在30天编写30个前端小项目。 这些项目不须要使用其余lib,不须要编译,不须要模板,回归最纯真的JavaScript;javascript
🐶 🐶 🐶前端
🐚 这是这个系列的第4篇java
- 1. Drum Kit
- 2.JS + CSS Clock
- 3.CSS Variables
- 4.Array Cardio 1
项目代码同步更新在男同交友网git
熟悉并掌握JavaScript中Array的使用,以及一些开发的小技巧;github
本期先讲一部分,剩余的在第7期中继续完成;数组
JS浏览器
length微信
用处:app
🌰:frontend
const length_test = [1, 3, 55, 66]
console.log(length_test.length)
length_test.length = 2
console.log(length_test)复制代码
这里附平时经常使用的清空数组的操做:
// 清空数组
let empty_test = [1,2,3,4];
// 1.length = 0
empty_test.length = 0
// 2. empty_test = []
empty_test = []
// 3. splice
empty_test.splice(0)复制代码
效率最好的是第二种,可是推荐使用第一种; 由于第二种新建了内存
prototype
......大部分的方法都是原型链上的方法。 具体的每一个方法会在后续一一实践;
console.log(Array.prototype)复制代码
静态方法是指那些不须要对类进行实例化,使用类名就能够直接访问的方法,须要注意的是静态方法不能被实例化的对象调用。静态方法常常用来做为工具函数。 --MDN
// Array.isArray()
const type_test = [];
const type_test1 = {};
console.log('type_test type is array? : ', Array.isArray(type_test))
console.log('type_test1 type is array? : ', Array.isArray(type_test1))复制代码
那么若是遇到不支持ES6语法的浏览器呢?
这里实现一种更加健壮的Array检测方法
function isArray(value){
if (typeof Array.isArray === "function") {
return Array.isArray(value);
}else{
return Object.prototype.toString.call(value) === "[object Array]";
}
}复制代码
类数组对象(拥有一个 length 属性和若干索引属性的任意对象)
可遍历对象(你能够从它身上迭代出若干个元素的对象,好比有 Map 和 Set 等)
语法:
Array.from(arrayLike[, mapFn[, thisArg]])复制代码
// 将可迭代对象(Set 对象)转换成数组
Array.from(new Set(["foo", window]));
// 使用 map 函数转换数组元素
Array.from([1, 2, 3], x => x + x); // [2, 4, 6]
// 生成一个数字序列
Array.from({length:5}, (v, k) => k); // [0, 1, 2, 3, 4]复制代码
tips: 发现了一个小玩意
// 实现字符串反转
let str = 'abcd'
Array.from(str).reverse().join('')复制代码
Array.of(element0[, element1[, ...[, elementN]]])复制代码
🌰:
Array.of(1); // [1]
Array.of(1, 2, 3); // [1, 2, 3]
Array.of(undefined); // [undefined]复制代码
对于不支持的浏览器,能够这样作:
if (!Array.of) {
Array.of = function() {
return Array.prototype.slice.call(arguments);
};
}复制代码
并返回数组新的长度
(length 属性值)let sports = ["soccer", "baseball"];
let total = sports.push("football", "swimming");
console.log(sports); // ["soccer", "baseball", "football", "swimming"]
console.log(total); // 4复制代码
tips: 用Array.pus().apply(arr1,arr2)来替代建立一个新数组。这种方法不是用来建立一个新的数组,其只是将第一个第二个数组合并在一块儿,同时减小内存的使用:
let array1 = [1,2,3];
let array2 = [4,5,6];
console.log(array1.push.apply(array1, array2));复制代码
而且返回这个元素
let colors = ['red', 'yellow', 'white'];
console.log(colors);
let target = myFish.pop();
console.log(colors);
console.log(target);复制代码
.pop方法和.push成对使用,它返回数组的末尾元素并将元素从数组移除。若是数组为空,返回void 0(undefined)。使用.push和.pop咱们能轻易模拟出LIFO(后进先出或先进后出)栈 -- 有趣的JavaScript原生数组函数
简易栈(Stack)的实现:
function Stack () {
this._stack = []
}
Stack.prototype.next = function () {
return this._stack.pop()
}
Stack.prototype.add = function () {
return this._stack.push.apply(this._stack, arguments)
}
stack = new Stack()
stack.add(1,2,3)
stack.next()复制代码
并返回这个元素。该方法会改变数组的长度
shift 方法移除索引为 0 的元素(即第一个元素),并返回被移除的元素,其余元素的索引值随之减 1。若是 length 属性的值为 0 (长度为 0),则返回 void 0(undefined)
// shift
let colors2 = ['red', 'yellow', 'white'];
console.log(colors2);
let target = colors2.shift();
console.log(colors2);
console.log(target2);复制代码
并返回数组新的 length 值
let arr = [1, 2];
arr.unshift(0); //result of call is 3, the new array length
//arr is [0, 1, 2]复制代码
用.unshift 和 .shift模拟FIFO(先进先出)队列(Queue)
function Queue () {
this._queue = []
}
Queue.prototype.next = function () {
return this._queue.shift()
}
Queue.prototype.add = function () {
return this._queue.unshift.apply(this._queue, arguments)
}
queue = new Queue()
queue.add(1,2,3)
queue.next()复制代码
本文对你有帮助?欢迎扫码加入前端学习小组微信群: