当前位置:网站首页>箭头函数的使用
箭头函数的使用
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强
边栏推荐
猜你喜欢
随机推荐
Several ways to heavy
处理List<Map<String, String>>类型
多个gcc/glibc版本的共存及指定gcc版本的编译
7.15 Day21---MySQL----Index
Sublime Text 3 2021.8.3 个人配置
MySQL数据库(基础)
读者让我总结一波 redis 面试题,现在肝出来了
企业需要知道的5个 IAM 最佳实践
MySql data recovery method personal summary
如何低成本修bug?测试左移给你答案
webrtc中的视频编码(一) 视频编码模块轮廓
(Kettle) pdi-ce-8.2 连接MySQL8.x数据库时驱动问题之终极探讨及解决方法分析
MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log
实现登录密码混合动态因子,且动态因子隐式
8、自定义映射resultMap
7.18 Day23----标记语言
8. Custom mapping resultMap
JNI基本使用
static在不同位置定义变量居然还有不同的含义?
Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法









