小白学JavaScript(2)

281 阅读4分钟

接上文,继续复习巩固JS知识

1.数组--Array

用法

let arr = [1,2,3]

let arr = new Array(1,2,3)

转化

let arr = "1,2,3".split(",")

let arr = "123".split("")

Array.from("123")

伪数组

  1. 拥有 length 属性,其它属性(索引)为非负整数(对象中的索引会被当做字符串来处理,这里你可以当做是个非负整数串来理解)
  2. 不具有数组所具有的方法

伪数组与数组的区别

  • 伪数组的原型链中并没有数组的原型。
  • 伪数组:有下标和length数组的类数组对象。
  • 伪数组没有数组原型中的共有属性。
  • 伪数组是一个 Object,而真实的数组是一个 Array。
  • 伪数组存在的意义,是可以让普通的对象也能正常使用数组的很多方法

常见的伪数组

  • 函数内部的 arguments
  • DOM 对象列表(比如通过 document.getElementsByTags 得到的列表)
  • jQuery 对象(比如 $("div")

伪数组转数组

  • [...Array] 通过扩展运算符
  • 通过Array.form()方法

数组的增删改查

增加 数组尾部增加push 返回值为数组长度,改变原数组

const animals = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows');
console.log(count);
// expected output: 4

数组头部增加unshift,返回值为数组长度,改变原数组

const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));
expected output: 5

splice方法,主要看第2个参数,当第2个参数为0时表示插入,不为0时表示删除。

数组中间添加splice

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

删除 数组尾部删除pop,返回被删除的元素,改变原数组

const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
console.log(plants.pop());
// expected output: "tomato"
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]

数组头部删除shift,返回被删除的元素,改变原数组

const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1);
// expected output: Array [2, 3]
console.log(firstElement);
// expected output: 1

数组中间删除splice,返回被删除的元素,改变原数组

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 2, 'Feb');
console.log(months);
// expected output: Array ["Jan", "Feb", "June"]

修改 数组下标修改 splice修改

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 1, 'Feb');
console.log(months)
// expected output: Array ["Jan", "Feb", "April", "June"]

查看 数组下标查看

数组的一些常用api

翻转数组reverse,返回新数组,改变原数组

const reversed = array1.reverse();
console.log('reversed:', reversed);
// expected output: "reversed:" Array ["three", "two", "one"]

数组的排序 按字母排序

const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]

按数字排序

const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// expected output: Array [1, 100000, 21, 30, 4]

sort默认升序,如果想降序的话sort里面加个函数

const array1 = [1, 30, 4, 21, 100000];
array1.sort((a,b)=> b - a);
console.log(array1);
// expected output: Array [1, 100000, 21, 30, 4]

合并数组 concat连接数组,返回新数组,不改变原数组

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);

截取数组 slice()方法返回一个新的数组对象,对数组某一个区域的进行截取。

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]

2.函数

函数的四种定义方式 1.具名函数

function 函数名(形参1,形参2){

语句

return 返回值

}

2.匿名函数 上面的具名函数,去掉函数名就是匿名函数,也叫函数表达式

let a = function(x,y){return x + y}

3.箭头函数

let f1 = x => x * x;

let f2 = (x,y)=>{return x + y}圆括号不能省

let f3 = (x,y) => {z = x + y; return z}花括号不能省

let f4  = (x,y) => ({name:x,age:y})

直接返回对象会报错,需要加个圆括号

4.构造函数

let f = new Function("x","y","return x + y");

基本没人使用。

所有函数都是Function构造出来的。

包括Object,Array,Function也是。

函数的元素 每个函数都有这些东西

  • 调用时机
  • 作用域
  • 闭包
  • 形式参数
  • 返回值
  • 调用栈
  • 函数提升
  • arguments(除了箭头函数)
  • this(除了箭头函数)

这里先简单的提一下,具体关于函数的闭包,原型链,this指向,立即执行函数等会在后面的文章中逐渐涉及。

argument

  • 当我们不确定有多少个参数传递的时候,可以用arguments来获取。在JS中,arguments实际上它是当前函数的一个内置对象。所有函数都内置了一个arguments对象。arguments对象中存储了传递的所有实参。
  • arguments展示形式是一个伪数组。因此可以进行遍历。
  • 伪数组的特点:1.具有length属性。2.按索引方式存储数据。3.不具有数组的push,pop等方法。
  • 只有函数才有argunments,每个函数都内置了arguments.