es6箭头函数的优点:1、简明的语法,例“parameters => {statements;};”,应用起来更加的方便;2、能够隐式返回;3、更直观的作用域和this的绑定(不绑定this)。
本教程操作环境:windows7系统、ECMAScript 6版、Dell G3电脑。
我们都知道,在 JavaScript 里定义函数有多种方式。最常见的是用function关键字:
// 函数声明 function sayHi(someone) { return `Hello, ${someone}!`; } // 函数表达式 const sayHi = function(someone) { return `Hello, ${someone}`; }
上面的函数声明和函数表达式,我们姑且称之为常规函数。
还有就是 ES6 新增的箭头函数语法:
const sayHi = (someone) => { return `Hello, ${someone}!`; }
相对于原先JS中的函数,ES6增长的箭头函数更加简洁,应用起来也更加的方便。
es6箭头函数的优点:
1、简明的语法
一个数组,把它变为原来的二倍之后输出。
删掉一个关键字,加上一个胖箭头; 没有参数加括号,一个参数可选择; 多个参数逗号分隔, const numbers = [5,6,13,0,1,18,23]; //原函数 const double = numbers.map(function (number) { return number * 2; }) console.log(double); //输出结果 //[ 10, 12, 26, 0, 2, 36, 46 ] //箭头函数 去掉function, 添加胖箭头 const double2 = numbers.map((number) => { return number * 2; }) console.log(double2); //输出结果 //[ 10, 12, 26, 0, 2, 36, 46 ] //若只有一个参数,小括号能够不写(选择) const double3 = numbers.map(number => { return number * 2; }) console.log(double3); //如有多个参数,则括号必须写;若没有参数,()须要保留 const double4 = numbers.map((number,index) => { return `${index}:${number * 2}`; //模板字符串 }) console.log(double4);
2、能够隐式返回
显示返回就是svg
const double3 = numbers.map(number => { return number * 2; //return 返回内容; })
箭头函数的隐式返回就是函数
当你想简单返回一些东西的时候,以下:去掉return和大括号,把返回内容移到一行,较为简洁; const double3 = numbers.map(number => number * 2);
补充:箭头函数是匿名函数,若需调用,须赋值给一个变量,如上 double3。匿名函数在递归、解除函数绑定的时候颇有用。
3、更直观的作用域和this的绑定(不绑定this
)
一个对象,咱们原先在函数中是这么写的this
一个对象,咱们原先在函数中是这么写的
const Jelly = { name:'Jelly', hobbies:['Coding','Sleeping','Reading'], printHobbies:function () { this.hobbies.map(function (hobby) { console.log(`${this.name} loves ${hobby}`); }) } } Jelly.printHobbies(); // undefined loves Coding // undefined loves Sleeping // undefined loves Reading
这说明 this.hobbies 的指向是正确的,this.name 的指向是不正确的;
当一个独立函数执行时,this 是指向window的
若是要正确指向,原先咱们的作法会是 设置变量替换spa
//中心代码 printHobbies:function () { var self = this; // 设置变量替换 this.hobbies.map(function (hobby) { console.log(`${self.name} loves ${hobby}`); }) } Jelly.printHobbies(); // Jelly loves Coding // Jelly loves Sleeping // Jelly loves Reading 在ES6箭头函数中,咱们这样写code //中心代码 printHobbies:function () { this.hobbies.map((hobby)=>{ console.log(`${this.name} loves ${hobby}`); }) } // Jelly loves Coding // Jelly loves Sleeping // Jelly loves Reading
这是由于箭头函数中访问的this其实是继承自其父级做用域中的this,箭头函数自己的this是不存在的,这样就至关于箭头函数的this是在声明的时候就肯定了(词法做用域),this的指向并不会随方法的调用而改变。
【相关推荐:javascript视频教程、web前端】
以上是es6箭头函数有什么优点的详细内容。更多信息请关注PHP中文网其他相关文章!

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

html5aimstoenhancewebcapabilities,Makeitmoredynamic,互动,可及可访问。1)ITSupportsMultimediaElementsLikeAnd,消除innewingtheneedtheneedtheneedforplugins.2)SemanticeLelelemeneLementelementsimproveaCceccessibility inmproveAccessibility andcoderabilitile andcoderability.3)emply.3)lighteppoperable popperappoperable -poseive weepivewebappll

html5aimstoenhancewebdevelopmentanduserexperiencethroughsemantstructure,多媒体综合和performanceimprovements.1)SemanticeLementLike like,和ImproVereAdiability and ImproVereAdabilityAncccossibility.2)和TagsallowsemplowsemplowseamemelesseamlessallowsemlessemlessemelessmultimedimeDiaiiaemediaiaembedwitWithItWitTplulurugIns.3)

html5isnotinerysecure,butitsfeaturescanleadtosecurityrisksifmissusedorimproperlyimplempled.1)usethesand andboxattributeIniframestoconoconoconoContoContoContoContoContoconToconToconToconToconToconTedContDedContentContentPrevulnerabilityLikeClickLickLickLickLickLickjAckJackJacking.2)

HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

使用ID选择器在CSS中并非固有地不好,但应谨慎使用。1)ID选择器适用于唯一元素或JavaScript钩子。2)对于一般样式,应使用类选择器,因为它们更灵活和可维护。通过平衡ID和类的使用,可以实现更robust和efficient的CSS架构。

html5'sgoalsin2024focusonrefinement和optimization,notnewfeatures.1)增强performandemandeffifice throughOptimizedRendering.2)risteccessibilitywithrefinedibilitywithRefineDatientAttributesAndEllements.3)expliencernsandelements.3)explastsecurityConcerns,尤其是withercervion.4)

html5aimedtotoimprovewebdevelopmentInfourKeyAreas:1)多中心供应,2)语义结构,3)formcapabilities.1)offlineandstorageoptions.1)html5intoryements html5introctosements introdements and toctosements and toctosements,简化了inifyingmediaembedingmediabbeddingingandenhangingusexperience.2)newsements.2)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver CS6
视觉化网页开发工具