数组

重排
1.reverse():反转数组项的顺序
注:使用该方法会同时改变原来数组的顺序,而不是返回一个副本
例:
let arr = [1,2,3,4,5];
console.log(arr.reverse());//[ 5, 4, 3, 2, 1 ]
console.log(arr);//[ 5, 4, 3, 2, 1 ]
2.sort():按照升序顺序排列数组每一项
例:
let arr = [0,12,3,7,-12,23];
console.log(arr.sort());
//[ -12, 0, 12, 23, 3, 7 ]
可以看到,我们调用sort()方法后排序并没有正确的按照升序来排序。
原因是sort()方法排序会先调用每一个元素的toString()转型方法,然后比较得到的字符串,即使每一项都是数值,sort()方法比较的也是字符串
解决办法:sort()可以接受一个比较函数作为参数
例:
let arr = [0,12,3,7,-12,23];
console.log(arr.sort(function(a,b){
if(a < b){
return -1;
}else if(a > b){
return 1;
}else{
return 0;
}
}));
如果要进行降序排列只需要将返回值修改即可。
当然,还有一种更简单的方法,
例:
let arr = [0,12,3,7,-12,23];
console.log(arr.sort(function(a,b){
return a - b;
//降序就返回 b - a
}));
注:reverse()和sort()返回值是经过排列后的数组
位置
1.indexOf():查找项目索引,他是从数组开头开始找,如果没找到就返回-1
例:
let arr = ["H","e","l","l","o"];
console.log(arr.indexOf("l"));//2
console.log(arr.indexOf("z"));//-1
2.lastIndexOf():查找起点位置索引,从数组末尾开始找,没找到就返回-1
例:
let arr = ["H","e","l","l","o"];
console.log(arr.lastIndexOf("l"));//3
console.log(arr.indexOf("z"));//-1
3.includes():查看数组是否包含某个元素,是就返回true,否则返回false
例:
let arr = ["1","2","3"];
console.log(arr.includes(2));//flase
console.log(arr.includes("2"));//true
console.log(arr.includes(7));//false
集合
Set集合是一种无重复(重点!!!)元素的列表,这是这种数据结构最大的特点。
创建集合
let s1 = new Set();
let s2 = new Set([1,2,3]);
console.log(s1);//Set {}
console.log(s2);//Set { 1, 2, 3 }
注:JS中严格区分大小写
添加
我们可以用add()给集合添加值
例:
let s1 = new Set();
s1.add(1);
console.log(s1);//Set { 1 }
s1.add(2).add(3).add(4);
console.log(s1);
//Set { 1, 2, 3, 4 }
例2:
let s1 = new Set();
s1.add([1,2,3]);
console.log(s1);
//Set { [ 1, 2, 3 ] }
相关属性和方法
1.size:
它可以获取元素的个数,和数组的length相似
例:
let s1 = new Set([1,2,3]);
console.log(s1.size);//3
2.has():
它可以查看一个集合中是否包含某一个值
例:
let s1 = new Set([1,2,3]);
console.log(s1.has(1));//true
console.log(s1.has("1"));//false
3.delete:
它可以删除Set对象里某一个元素
例:
let s1 = new Set([1,2,3]);
s1.delete(2);
console.log(s1);//Set { 1, 3 }
s1.delete("2");
console.log(s1);//Set { 1, 3 }
注:delete无法删除集合中的数组
4.clear():
它可以删除所有的元素
例:
let s1 = new Set([1,2,3]);
s1.clear()
console.log(s1);//Set {}
注:它将一次清空集合的所有内容,包括删除数组
遍历
1.for-of:
let s = new Set([1,2,3,4,5]);
for(let i of s){
console.log(i);
}// 1
// 2
// 3
// 4
// 5
2.forEach:
//使用forEach进行遍历
let s = new Set([1,2,3,4,5]);
s.forEach(ele => console.log(ele));
// 1
// 2
// 3
// 4
// 5
3.keys():
它可以遍历集合的键:
let s = new Set(["Bill","Lucy","David"]);
for(let i of s.keys()){
console.log(i);
}
// Bill
// Lucy
// David
4.values():
它可以遍历集合的值:
let s = new Set(["Bill","Lucy","David"]);
for(let i of s.values()){
console.log(i);
}
// Bill
// Lucy
// David
5.entries():
它可以遍历集合的键和值:
let s = new Set(["Bill","Lucy","David"]);
for(let i of s.entries()){
console.log(i);
}
//因为未设定值,所以返回的是它本身
// [ 'Bill', 'Bill' ]
// [ 'Lucy', 'Lucy' ]
// [ 'David', 'David' ]
集合转数组
let s1 = new Set([1,2,3]);
console.log(s1);//Set { 1, 2, 3 }
let arr = [...s1];
console.log(arr);//[ 1, 2, 3 ]
或者使用Array对象提供的from()方法转换:
let s1 = new Set([1,2,3]);
console.log(s1);//Set { 1, 2, 3 }
let arr = Array.from(s1);
console.log(arr);//[ 1, 2, 3 ]
注:因为集合不能存放相同的元素,所以可以用这种方法给数组去重
弱集合
let arr = [1,2,3];
let s = new Set(arr);
arr = null;//删除arr数组的指向
console.log(s);//Set { 1, 2, 3 }数组依然存在在集合中
console.log(arr);//null
注:这就是删除数组,但无法删除
1.内存泄漏
也就是站着空间却没有用,造成内存浪费,浏览器使用变慢
例:
let arr = [1,2,3];
arr = null;
解决方法:在JS中采用的是动态内存管理技术,例如垃圾回收机制,会自动从内存中删除不再被程序需要的东西。
注:弱集合无法在创建弱集合时传入一个数组进行初始化,不过弱集合也有has(),add(),delete()等方法
映射
JS对象,本质上是键值对的集合,为了解决这个问题,ES6提供了Map数据结构。
它类似对象,也是键值对的集合,它提供了“值-值”的对应,是一种更完善的Hash结构的实现
创建映射
let m = new Map();
m.set("name","xiejie");
m.set("age",18);
console.log(m);
//Map { 'name' => 'xiejie', 'age' => 18 }
console.log(m.get("name"));
//xiejie
注:JS中严格区分大小写
在对象中无法用对象最为对象属性的键名。而在Map映射中却可以这样做,可以说在Map映射里面可以使用任意数据类型作为键:
let m = new Map();
m.set({},"xiejie");
m.set([1,2,3],18);
m.set(3581,18);
console.log(m);
//Map { {} => 'xiejie', [ 1, 2, 3 ] => 18, 3581 => 18 }
相关属性和方法
在映射中同样支持:
has(key):检查制定的键名在Map映射中是否存在
delete(key):从Map映射中移除指定键名及对应的值
clear():移除Map映射中所有的键值
Map映射同样支持size属性,其代表当前集合中包含的键值对数量:
let arr = [["name","xiejie"],["age",18]];
let m = new Map(arr);
console.log(m);//Map { 'name' => 'xiejie', 'age' => 18 }
console.log(m.size);//2
console.log(m.has("name"));//true
console.log(m.get("name"));//xiejie
m.delete("name");
console.log(m);//Map { 'age' => 18 }
m.clear();
console.log(m);//Map {}
遍历
1.for-of:
let m = new Map([["name","xiejie"],["age",18]]);
for(let i of m){
console.log(i);
}
// [ 'name', 'xiejie' ]
// [ 'age', 18 ]
2.keys():
遍历映射的键
let m = new Map([["name","xiejie"],["age",18]]);
for(let i of m.keys()){
console.log(i);
}
// name
// age
3.values():
遍历映射的值:
let m = new Map([["name","xiejie"],["age",18]]);
for(let i of m.values()){
console.log(i);
}
// xiejie
// 18
4.entries():
遍历映射的键和值:
let m = new Map([["name","xiejie"],["age",18]]);
for(let i of m.entries()){
console.log(i);
}
// [ 'name', 'xiejie' ]
// [ 'age', 18 ]
映射转数组
let arr = [["name","xiejie"],["age",18]];
let m = new Map(arr);
console.log([...m.keys()]);//[ 'name', 'age' ]
console.log([...m.values()]);//[ 'xiejie', 18 ]
console.log([...m.entries()]);//[ [ 'name', 'xiejie' ], [ 'age', 18 ] ]
console.log([...m]);//[ [ 'name', 'xiejie' ], [ 'age', 18 ] ]
或者使用Array对象的from()方法:
let arr = [["name","xiejie"],["age",18]];
let m = new Map(arr);
console.log(Array.from(m));
//[ [ 'name', 'xiejie' ], [ 'age', 18 ] ]
弱映射
主要是解决存在映射内部的垃圾数据问题
let weakMap = new WeakMap();
注:弱映射和普通映射一样,具有has(),get(),set(),delete()等方法
函数
函数可以通过名称来引用,像是自包含了一个微型程序的代码块。
利用函数,我们可以实现对代码的重复使用。
声明函数的方法
1.字面量声明函数:
function 函数名(形式参数){
//函数体
}
函数名:我们调用函数需要书写的标识符
形式参数:简称形参,调用函数需要接收的函数
实际参数:简称实参,调用函数时实际传递过去的参数
注:函数和数据一样,命名不可用关键字和保留字
例:
function test(name){
console.log("Hello,"+name);
}
test("xiejie");//Hello,xiejie
2.函数表达式声明函数
let ݒ变量 = function(){
//函数体
}
例:
let test = function(name){
console.log("Hello,"+name);
}
test("xiejie");//Hello,xiejie
例2:
let test = function saySth(name){
console.log("Hello,"+name);
}test("xiejie");//Hello,xiejie
3.构造器声明函数
let 变量 = new Function("参数","参数","函数体");
例:
let test = new Function("name","console.log('Hello,'+name)");
test("xiejie");//Hello,xiejie
注:虽然这种方法也能创建函数并且调用,但并不托件,因为这样会导致JS解析器解析两次代码
调用
let test = function(){
console.log("Hello")
}let i = test;//没有调用函数,而是将test函数赋值给了i
i();//Hello
返回值
函数的返回值关键字为return
例:
let test = function(){
return "Hello";
}
let i = test();
console.log(i);//Hello
即使不写return,函数本身也有返回值undefined
例:
let test = function(){
console.log("Hello")
}
let i = test();//Hello
console.log(i);//undefined
注:return关键字只返回一个值
函数参数
参数分两种,一个是实际参数,一个是形式参数
参数声明不需要添加关键字,添加反而会报错
例:
function test(let i){
console.log(i);
}
test(5);
//SyntaxError: Unexpected identifier
形参:
1.参数可以重名,重名取最后一个参数:
function test(x,x){
console.log(x);
}
test(3,5);//5
2.即使函数声明了参数,调用时也可以不传递参数值:
function test(x){
console.log(x);
}
test();//undefined
3.调用函数可以传递若干个参数值给函数,而不是管函数声明时有几个参数:
function test(x){
console.log(x);//1
}
test(1,2,3);
当一个函数要执行时,系统会在执行函数体代码前做一些初始化工作,例为函数创建一个arguments的伪数组对象,它将包含调用函数时传递所有的实际参数
例:
function test(x){
for(let i=0;i<arguments.length;i++){
console.log(arguments[i]);
}
}
test(1,2,3);
// 1
// 2
// 3
不定参数
它是ES6新增的功能,在最后一个形参前面添加3个点,会将所有的实参放到一个数组内:
function test(a,...b){
console.log(a);//1
console.log(b);//[2,3]
}
test(1,2,3);
注:不定参数是放在形参最后,若否,则报错
默认参数
从ES6开始,书写函数给函数形参一个默认值,这样调用函数没有传入相应的实参,就会使用默认值,如果传入了实参,就会使用实参:
function test(name = "world"){
console.log("Hello,"+name);
}test("xiejie");//Hello,xiejie
test();//Hello,world
属性和方法
1.name属性:
表示函数的名字:
function test(){
console.log("Hello")
}
console.log(test.name);//test
2.length属性
表示形参的个数:
let test = function(a,b,c){
console.log("Hello")
}
console.log(test.length);//3
3.caller属性:
它不是arguments对象的,而是函数对象本身的属性,他先是函数的调用者,如果函数是在全局执行环境中(浏览器中)被调用,它的值为null,如果在另一个函数被调用,它的值就是那个函数
全局执行环境中被调用:
浏览器中:
<body>
<script>
let test = function(){
console.log(test.caller) }
test();//null
</script>
</body>
node中:
let test = function(){
console.log(test.caller)
}
test();//[Function]
被另一个函数调用:
let test = function(){
let test2 = function(){
console.log(test2.caller);
//[Function: test]
//因为这个函数的调用者就是test函数
}
test2();
}
test();
4.callee属性:
它是arguents对象的一个属性,它是一个指针,指向拥有arguments对象的函数:
let test = function(){
let test2 = function(){
let test3 = function(){
console.log(arguments.callee);
//[Function: test3]
}
test3();
}
test2();
}
test();
它能够找到arguments对象所属的函数,不让函数的执行和函数名仅仅的关联在一起:
//计算阶乘的递归函数
let test = function(i){
if(i == 1){
return 1;
}else{
return i * test(i-1);//这里就和函数名紧紧地关联了起来
}
}console.log(test(3));

posted on 2019-07-02 10:53  蜀风古韵  阅读(85)  评论(0编辑  收藏  举报

导航