美文网首页
ES6函数特性

ES6函数特性

作者: 番茄向前看 | 来源:发表于2020-03-15 17:59 被阅读0次

箭头函数

简写方式

-- 如果有且只有1个参数,()可以不写
-- 如果有且仅有一个语句并且是return,{}可以不写

    <script>
        let arr = [23, 34, 56, 77]
        //正常写法
        arr.sort(function (n1, n2) {
            return n1 - n2;
        })
        //有且仅有一个语句并且是return,{}可以不写
        arr.sort((n1, n2) => n1 - n2);
        console.log(arr);
    </script>
箭头函数的this
  • 使用箭头函数,可固定当前的环境(执行时候的环境);
 <script>
        let jsons = {
            a: 12,
            fn: function () {
                console.log(this)
            },
            fs: () => {
                console.log(this)
            }
        }
        jsons.fn();//此处输出的事jsons的this

        let oDate = new Date();
        oDate.fn = jsons.fn;
        oDate.fn();//此处输出的事jsons的this就编程了oDate
        oDate.fs = jsons.fs;
        oDate.fs();//当前执行环境为window;

        //在Json里面声明了this,用=>函数会固定这个数值
        class Json {
            constructor() {
                this.a = 12,
                    this.fn = () => {
                        alert(this.a)
                    }
            }
        }
        let json = new Json();
        let oDate = new Date();
        oDate.fn = json.fn;
        oDate.fn();
</script>

数据展开与合并

使用 ... 可收集或展开数据

<script>
        //收集剩余参数
        //...必须是最后一个参数
        function show(a, b, ...c) {
            console.log(a, b, ...c)
        }
        show(2, 3, 4, 6, 7, 8, 9, 10, 23);

        //数组的展开
        let arr1 = [123, 56, 67];
        let arr2 = [34, 56, 89];
        let arr = [...arr1, ...arr2]//与数组连接-将arr1+arr2添加至arr
        function showArr(...arr) {
            console.log(...arr)
        }
        showArr(...arr);

        //json展开
        let json = { a: "番", b: "茄", c: "向", d: "前", e: "看" }
        let json2 = {
            ...json,//与数据合并
            z: 999
        }
        console.log(json2)
</script>

原生对象扩展

  • map映射
<script>
        let arr = [68, 25, 98, 62, 35, 33, 68, 34, 67, 44, 100]
        //map 映射 --一一对应
        let arr2 = arr.map((item, index) => item >= 60 ? '及格' : '不及格')
        console.log(arr);
        console.log(arr2)
</script>
  • reduce 汇总
<script>
        let arr = [68, 25, 98, 62, 35, 33, 68, 34, 67, 44, 100]
        //reduce 1个值例如:对数据求平均数 /tmp为临时数/
        let arr3 = arr.reduce((tmp, item, index) => {
            if (index == arr.length - 1) {
                return (tmp += item) / arr.length;
            } else {
                return tmp += item;
            }
        })
        console.log(arr3)
    </script>
  • filter 过滤器
<script>
        let arr = [68, 25, 98, 62, 35, 33, 68, 34, 67, 44, 100]
      //filter 过滤
        let arr4 = arr.filter((item, index) => item % 2 == 0)
        console.log(arr)
        console.log(arr4)
    </script>
  • forEach 循环
<script>
        let arr = [68, 25, 98, 62, 35, 33, 68, 34, 67, 44, 100]
       //forEach 遍历,没有return,
        let arr5 = arr.forEach(function (item, index) {
            //console.log('第' + index + '个' + item);
            //模板字符串`
            console.log(`第${index}个:${item}`)
        })
</script>
  • 模板字符串“·”
<script>
 console.log(`第${index}个:${item}`)
</script>
  • JSON 对象新方法
<script>
        //stringify 可以将JSON格式解析为字符串成;
        let Json = { a: 123, b: "345", c: "456", d: [1, 2, 4, 5, 6] }
        console.log(JSON.stringify(Json));
        //stringify 可以将字符串成解析为JSON格式;
        let Str = '{ "a": "123", "b":"345",  "c": "456", "d": "[1, 2, 4, 5, 6]"}';
        console.log(JSON.parse(Str));
</script>

相关文章

  • es6、js、css、jquery、vue以及程序设计 知识点总

    es6 列举常用的es6特性。 箭头函数this的指向。 eg:箭头函数的特性 箭头函数内部没有construc...

  • ES6在企业中的应用

    模板字符串 解构赋值解构赋值最常用的在于函数。 数组spread es6浅拷贝 函数新特性之箭头函数 函数新特性之...

  • ES6箭头函数(Arrow Functions)

    箭头函数是什么?(What) 箭头函数 (Arrow Functions)也称“胖箭头函数”,是ES6全新的特性。...

  • (9) 函数

    函数 1. 定义 普通函数 函数表达式 Function关键字 箭头函数(ES6) 2. 函数特性 模拟函数重载f...

  • 解析ES6箭头函数中的this

    ES6中新增了箭头函数这种语法,箭头函数以其简洁性和方便获取this的特性,接下来通过本文给大家分享ES6箭头函数...

  • ES6新特性5:函数的扩展之箭头函数

    箭头函数在ES6中最常用的特性,学习好至关重要,特别在Vue项目和React项目。 箭头函数:ES6中使用(=>)...

  • es6新特性

    es6新特性 1.函数参数添加默认值 es6之前 es6之后: 2.字符串拼接 es6之前: es6之后: 3.解...

  • ES6 常用特性(二)

    箭头函数 箭头函数无疑是 ES6 中一个相当重要的新特性。 特性 1、共享父级 this 对象2、共享父级 arg...

  • ES6的Generator函数(2018-06-21)

    声明Generator函数 Generator函数,又称生成器函数,是ES6的一个重要的新特性。 注意:1、普通函...

  • ES6函数特性

    箭头函数 简写方式 -- 如果有且只有1个参数,()可以不写-- 如果有且仅有一个语句并且是return,{}可以...

网友评论

      本文标题:ES6函数特性

      本文链接:https://www.haomeiwen.com/subject/cyajehtx.html