zoukankan      html  css  js  c++  java
  • es6-数组,函数,对象学习总结

    一、数组拓展

    1.Array.of()
    1 {
    2     // Array.of  将一组数据转换成数组
    3     var arr = Array.of(3,4,2,5);
    4     console.log('arr: ',arr);
    5     var empty = Array.of();   //空数组
    6     console.log(empty)
    7 }
    2.Array.from()
     1 {
     2     //Array.from   将伪数组,集合转换为真正的数组,如document.querySelectorAll,p标签返回的集合
     3     let p =document.querySelectorAll('p');
     4     let pArr = Array.from(p);
     5     console.log(pArr)
     6     pArr.forEach(function(item){
     7         console.log(item.textContent);  //textContent是js原生获取dom节点文本内容
     8     })
     9 
    10     //Array.from   有类似map功能
    11     console.log(Array.from([1,3,5],function(item){return item*2}))   //把每个数组遍历输出
    12 }
    3.fill填充数组
    1 {
    2     //fill 填充数组
    3     console.log('fill-7',[1,'s',undefined].fill(7)) 
    4 
    5     console.log('fill,pos',['a','s',6].fill(7,1,2))  //fill参数依次对应,填充值,填充起始位置,填充截止位置
    6 }
    4.获取数组下标和值
     1 {
     2 
     3     for(let index of ['a','f','s'].keys()){    //keys 下标
     4         console.log('keys:',index)
     5     }
     6     for(let value of ['a','f','s'].values()){   //values 取值
     7         console.log('value',value)
     8     }
     9     for(let [index,value] of ['a','f','s'].entries()){   //entries 能取到下标和值
    10         console.log(index,value)
    11     }
    12 }
    5.其它API
     1 {
     2     // 在当前数组内部,把指定位置的成员复制到其它位置,使用频率不高
     3     console.log([1,2,3,4,5].copyWithin(1,4,5))   //copyWithin参数依次表示 替换位置,替换值开始读取的位置,替换值结束读取的位置   【1,5,3,4,5】
     4 }
     5 {
     6     // 查找一个元素是否在数组内,可以写函数
     7 
     8     console.log([1,2,3,4,5].find(function(item){return item > 3}))  //4  只返回查找到的第一个元素
     9     console.log([1,2,3,4,5].findIndex(function(item){return item > 3}))  //3 只返回查找到的第一个元素下标
    10 }
    11 
    12 {
    13     //includes 包括了对NaN的判断
    14     console.log('number',[1,2,NaN].includes(1))   //true
    15     console.log('number',[1,2,NaN].includes(NaN))  //true
    16     console.log('number',[1,2,NaN].includes('s'))  //true
    17 }

    二、函数拓展

    1.函数参数默认值
    1 {
    2     function test(x,y='world'){   //默认值后面不能再有没有默认值的变量
    3         console.log('say',x,y)
    4     }
    5     test('hello');
    6     test('hello','kitty');
    7 }
    2.作用域
     1 {
     2     //作用域的问题
     3     let x='test';
     4     function test(x,y=x){
     5         console.log('scope:',x,y)
     6     }
     7     test('kill');         //kill  kill
     8     test();             //undefined undefined
     9 
    10     function test2(c,y=x){
    11         console.log('scope:',c,y)
    12     }
    13     test2('kill');        //kill , test
    14 }
    3.rest参数
    1 {
    2     //将一列参数转换成数组
    3     function test3(...arg){   //rest参数,后面不能有其它参数
    4         for(let v of arg){
    5             console.log('rest',v)
    6         }
    7     }
    8     test3(1,2,3,4)
    9 }
    4.扩展运算符
    1 {
    2     // 扩展运算符 将数组转换成离散值,rest的逆运用
    3     console.log(...[1,2,3])
    4     console.log('a',...[1,2,3])
    5 }
    5.箭头函数
    1 {
    2     // 箭头函数,使用箭头函数时,需要注意this指向
    3 
    4 // this绑定(箭头函数在定义时的所在),ES5 this是函数被调用时的所在
    5     let arrow = v => v*2;    //函数名,函数参数,返回值
    6     let arrow2 = () => 5;     //没有参数时,写个圆括号
    7     console.log('arrow',arrow(3))  //6
    8     console.log('arrow2',arrow2())   //5
    9 }
    6.伪调用
     1 {
     2     // 伪调用,在最后一步调用函数,提高性能
     3     function tail(x){
     4         console.log('tail',x)
     5     }
     6     function fx(x){
     7         return tail(x);    //在最后一步调用函数
     8     }
     9     fx(111)         //111
    10 }

    三、对象

    1.简洁表示
     1 {
     2     let o=1;
     3     let k=2;
     4     let es5 ={
     5         o:o,
     6         k:k
     7     };
     8     let es6={
     9         o,
    10         k
    11     };
    12     console.log(es5,es6)
    13 
    14     // 对象中有方法
    15     let es5_method={
    16         hello:function(){
    17             console.log('hello')
    18         }
    19     }
    20     let es6_method={
    21         hello(){
    22             console.log('hello')
    23         }
    24     }
    25     console.log(es5_method.hello(),es6_method.hello())
    26 }
    View Code
    2.属性表达式
     1 {
     2     // 属性表达式,用[]表示,属性是可变的
     3     let a='b';
     4     let es5_obj={
     5         a:'c',
     6         b:'c'
     7     };
     8     let es6_obj={
     9         [a]:'c'    //key值可以是一个变量或者表达式
    10     };
    11     console.log(es5_obj,es6_obj)
    12 }
    3.新增API
     1 {
     2     // is判断两个值是否完全相等,与===一样
     3     console.log('string',Object.is('abc','abc'),'abc'==='abc') //true true
     4     console.log('array',Object.is([],[]),[]===[])  //false  false  引用类型
     5     // 拷贝 assign,浅拷贝
     6     // assign拷贝的是自身对象的属性(如果对象只有继承,他不会拷贝继承的属性),也不会拷贝不可枚举的属性。 
     7     // js中基本包装类型的原型属性是不可枚举的,如Object, Array, Number等 
     8     console.log('copy',Object.assign({a:'a'},{b:'b'}));
     9     // entries
    10     let test={k:123,o:456}
    11     for(let [key,value] of Object.entries(test)){  //与数组类似,数组:test.entries()
    12         console.log([key,value])
    13     }
    14 }
  • 相关阅读:
    React+Redux仿Web追书神器
    关于贝塞尔曲线的故事
    420小时学习代码之后:如何教你免费自学Python
    学问Chat UI(3)
    学问Chat UI(1)
    Binder进程间通信详解
    Handler源码分析
    学问Chat UI(4)
    WebPack错误集
    React问题集序
  • 原文地址:https://www.cnblogs.com/zynkl1314/p/11996464.html
Copyright © 2011-2022 走看看