当前位置:网站首页>ES6新增特性--箭头函数
ES6新增特性--箭头函数
2022-06-11 09:14:00 【夺笋123】
定义格式
var 函数名=(函数参数)=>函数返回值;
箭头函数使用实例
//无函数参数
var fun=()=>5;
//相当于
var fun=function(){
return 5
}
//单个函数参数
var pow1 = num => Math.pow(num,2);
var pow1=function(num){
return Math.pow(num,2);
}
//多个函数参数
var sum = (num1, num2) => num1 + num2; //相当于var sum = (num1, num2) => {return num1+num2};
var sum=function(num1,num2){
return num1+num2;
}
作用
- 简化回调函数
//sort函数中的回调函数
var result = array.sort((a, b) => a - b);
//相当于
var result = array.sort(function (a, b) {
return a - b;
});
回调函数:被作为实参传入另外一个参数并在该函数中被调用,用来完成某些任务的函数
- 简化表达
//定义一个计算数字平方值的工具函数
const pow_1=num=>num**2
pow_1(n)
特点
(1)箭头函数没有自己的this对象。
(2)不可以当作构造函数,也就是说,不可以对箭头函数使用new命令
(3)不可以使用arguments对象,该对象在函数体内不存在。
(4)不可以使用yield命令
注意
当函数返回值为一个对象时,需要加上括号
var myobj= name => ({
name: "Temp" ,age: 19});
箭头函数使用例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>这是一个html5 demo</title>
</head>
<body>
<script> var num1=1,num2=2 var sum = (num1, num2) => {
return num1+num2}; // var sum = (num1, num2) => num1+num2 var pow_1=num=>num**2 var fun=()=>5; console.log(fun()) console.log(pow_1(num2)) console.log(sum(num1,num2)) </script>
</body>
</html>
Chrome浏览器运行结果
边栏推荐
- 山东大学项目实训(四)—— 微信小程序扫描web端二维码实现web端登录
- ERP体系的这些优势,你知道吗?
- Some learning records I=
- Machine learning notes - the story of master kaggle Janio Martinez Bachmann
- Award winning survey streamnational sponsored 2022 Apache pulsar user questionnaire
- Importance of implementation of clothing ERP in the project
- Analysis of high frequency interview questions in massive data processing
- 【智能开发】血压计方案设计与硬件开发
- Openstack explanation (XXIII) -- other configurations, database initialization and service startup of neutron
- How win10 Home Edition connects to remote desktop
猜你喜欢

openstack详解(二十一)——Neutron组件安装与配置

Opencv CEO teaches you to use oak (V): anti deception face recognition system based on oak-d and depthai

关于原型及原型链

Modularnotfounderror: no module named 'find_ version’

Openstack explanation (22) -- neutron plug-in configuration

报错RuntimeError: BlobReader error: The version of imported blob doesn‘t match graph_transformer

Augmented reality experiment IV of Shandong University
![[image processing] spatial domain image enhancement](/img/d9/6afee1006570651ccf93ed8da95dd3.png)
[image processing] spatial domain image enhancement

Redis source code analysis hash object (z\u hash)

Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
随机推荐
报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s
剑指 Offer II 041. 滑动窗口的平均值
Openstack explanation (21) -- installation and configuration of neutron components
Day41 process pool and thread pool
移动端页面使用rem来做适配
1493. the longest subarray with all 1 after deleting an element
Comment l'entreprise planifie - t - elle la mise en oeuvre?
Talk about reading the source code
Award winning survey streamnational sponsored 2022 Apache pulsar user questionnaire
[intelligent development] scheme design and hardware development of sphygmomanometer
考研數學 【數列極限證明題】題型方法總結
Runtimeerror: blobreader error:the version of imported blob doesn't match graph_ transformer
Install jupyter in the specified environment
CUMT learning diary - theoretical analysis of uCOSII - Textbook of Renzhe Edition
Set MySQL as externally connectable
山东大学增强现实实验四
Type-C Bluetooth speaker single port rechargeable OTG solution
机器学习笔记 - Kaggle大师Janio Martinez Bachmann的故事
Detailed explanation of the difference between construction method and method
Sed explanation of shell script (SED command, sed -e, sed s/ new / old /...)