当前位置:网站首页>箭头函数的使用
箭头函数的使用
2022-08-04 05:26:00 【strongest强】
1.省略了function的书写,让代码更加简洁
let say_hello=function(){
console.log('hello world!');
}
let delete_function=()=>{
console.log('I had deleted function!');
}
say_hello();
delete_function();
输出:
hello world!
I had deleted function!
2.不能使用伪数组
let input = function() {
for (var k of arguments) {
console.log(k);
}
}
input(1, 2, 3);
console.log('.........................');
var hello=()=>{
for(var k of arguments){
console.log(k);
}
}
hello(5,6,7);
输出:
1,2,3
.........................
arguments is not defined
3.不能作为构造实例化对象
var person = (name, age) => {
this.name = name,
this.age = age;
}
let xinyi = new person('xie', 'wang');
console.log(xinyi);
输出:
person is not a constructor //备注(person不是一个构造器)
4.当传入实参只有一个时候,可以省略小括号
let fruits = asia => {
console.log(asia);
}
fruits('apples');
输出:
apples
5.当花括号内的代码只有一条时并且返回结果时,可以省略大括号
let add = (num1, num2) => num1 + num2;
console.log(add(2, 5));
console.log('.........................');
let pow = (num1) => num1 * num1;
console.log(pow(8));
输出:
7
.........................
64
6.静态指向,不能使用call,bind,apply强行改变this指向
function fun() {
console.log(this.name);
}
var hel = () => {
console.log(this.name);
}
var name = 'strongest强'
let newname = {
name: '强哥'
}
fun();
console.log('.........................');
hel();
console.log('.........................');
fun.call(newname);
console.log('.........................');
hel.call(newname);
console.log('.........................');
hel.apply(newname);
console.log('.........................');
hel.bind(newname)(null);
输出:
strongest强
.........................
strongest强
.........................
强哥
.........................
strongest强
.........................
strongest强
.........................
strongest强
边栏推荐
猜你喜欢
随机推荐
7.18 Day23 - the markup language
梳理CamStyle、PTGAN、SPGAN、StarGAN
npm init [email protected] 构建项目报错SyntaxError: Unexpected token ‘.‘解决办法
利用Jenkins实现Unity自动化构建
Summary of MySQL database interview questions (2022 latest version)
力扣:343. 整数拆分
Unity开发类似Profile那样的数据分析工具
处理List<Map<String, String>>类型
心余力绌:企业面临的软件供应链安全困境
Programming hodgepodge (4)
OpenSSF 安全计划:SBOM 将驱动软件供应链安全
《看见新力量》第四期免费下载!走进十五位科技创业者的精彩故事
Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
[原创]STL容器map和unordered_map性能,创建,插入,随机访问速度对比!
PHP解决字符乱码问题(多种编码转换)
力扣:62.不同路径
EventBus源码分析
Wwise入门和实战
7.16 Day22---MYSQL (Dao mode encapsulates JDBC)
lambda函数用法总结