当前位置: 首页 > 图灵资讯 > 技术篇> ES6 函数优化

ES6 函数优化

来源:图灵教育
时间:2023-08-02 09:47:11

箭头函数的优点;简写对象的函数属性、箭头函数和解构表达式的应用

箭头函数:不需要编写基本函数的解构(如:function),直接使用 =>

        var print = function (obj) {            console.log(obj);        };        print("print");        //箭头函数        var print2 = obj => console.log(obj);        print2("print2");        var sum = function (a, b) {            return a+b;        };        console.log(sum(1,2));        //箭头函数        var sum2 = (a,b) => a+b;        console.log(sum2(1)2));        var sum3 = (a,b) => {            console.log(a+b);            return a+b;        };        console.log(sum3(1)2));        let person = {            "name": "dada",            learn1: function (course="java") {                console.log(this.name + " 在学习 " + course);            },            //箭头函数;不能使用this            learn2: (course) => console.log(person.name + " 在学习 " + course),            //简写            learn3(course){                console.log(this.name + " 在学习 " + course);            }        };        person.learn1("java");        person.learn2("js");        person.learn3("css");        const person = {            name:"dada",            age:13,            language:["java","js","css"]        };         function hello(person) {             console.log("hello " + person.name);         }         hello(person);         ///箭头函数,解构表达式        var hello2 = ({name}) => console.log("hello2 " + name);        hello2(person);