箭头函数与普通function的区别
2024-10-07 16:38:09
1. 箭头函数没有自己的this,它里面的this是继承所属上下文中的this,而且使用call与apply都无法改变
let obj = {
name: 'obj'
}
function fn1() {
console.log(this);
}
fn1.call(obj);
let fn2() => {
console.log(this);
}
fn2.call(obj);
2. 普通函数的参数是arguments,而箭头函数是arg
let arr = [1,2,3]
~function(){
console.log(arguments);
}
(arr); //输出 [1,2,3]
let a = (...arg) => {
console.log(arg);
}
a(arr) //输出[1,2,3]
3. 语法上比普通函数更加简洁
function fn1(x) {
return function(y) {
return x + y;
}
}
let fn1 = x => y => x + y;
4. 箭头函数不能使用new生成构造函数,因为箭头函数没有prototype,而construct在prototype里面。
function Fn1() {
this.x = 100;
}
let f1 = new Fn1;
let Fn2 = () => {
this.x = 200;
}
let f2 = new Fn2; //输出 Fn2 is not a constructor
最新文章
- Spring:Aop before after afterReturn afterThrowing around 的原理
- 超链接a标签
- css性能优化
- Java使用占位符拼接字符串
- .NET 分页
- Centos7下用命令下载jdk7及jboss-eap-6
- tomcat 集群配置,Session复制共享
- requirejs实验001.对我来说,用AMD的方式来组织代码并不轻松.
- 在用VS2010连接oracle数据库时ORA-12504错误
- 《Principles of Mathematical Analysis》-chaper1-实数系与复数系
- Model和Entity Framework
- Windows下Nginx的安装与使用(一):配置端口转发
- hdu1394 分治 or 线段树
- Visual Studio 2010多线程编程
- 页面读取Excel
- holer实现外网访问本地网站
- Python中Celery 的基本用法以及Django 结合 Celery 的使用和实时监控进程
- js 表达式与语句
- MySql常用命令集Mysql常用命令3
- jquery获取input file的文件名,具有兼容性