美文网首页
箭头函数和function的区别

箭头函数和function的区别

作者: xiaojingy | 来源:发表于2020-09-08 14:11 被阅读0次

1、写法不同

let foo = () => {
  console.log('foo');
}
function foo(){
  console.log('foo');
}

2、this的指向不一样
使用function定义的函数,this的指向随着调用环境的变化而变化的,而箭头函数中的this指向是固定不变的,一直指向的是定义函数的环境。
3、构造函数

 //使用function方法定义构造函数
function Person(name, age){
    this.name = name;
    this.age = age;
}
var lenhart =  new Person(lenhart, 25);
console.log(lenhart); //{name: 'lenhart', age: 25}
//尝试使用箭头函数
var Person = (name, age) =>{
    this.name = name;
    this.age = age;
};
var lenhart = new Person('lenhart', 25); //Uncaught TypeError: Person is not a constructor

4、变量提升
由于js的内存机制,函数声明的时候存在变量提升,

foo()  // foo
function foo(){
  console.log('foo') 
}
fn() //Uncaught TypeError: fn is not a function
let fn = () => {
  console.log('fn');
}

相关文章

  • 箭头函数和function的区别

    1、写法不同 2、this的指向不一样使用function定义的函数,this的指向随着调用环境的变化而变化的,而...

  • 箭头函数和普通函数的区别

    什么是箭头函数? 箭头函数就是没有function关键字,而是一个类似箭头的函数: 相当于 它们之间的区别: 箭头...

  • 箭头函数和普通函数的区别

    定义:箭头函数没有function关键字,而是一个类似箭头的函数。 等价于 区别: 箭头函数作为匿名函数,是不能作...

  • 箭头函数(常用)

    ES6 允许使用箭头(=>)定义函数 箭头函数对于使用function关键字创建的函数有以下区别1.箭头函数没有a...

  • 箭头函数和普通函数的主要区别是什么?

    箭头函数和普通函数的主要区别: this的指向问题,箭头函数是不存在this的(也是箭头函数和普通函数最主要的区别...

  • JS:箭头函数(ES6标准)

    Arrow Function(箭头函数)。 ES6标准新增了一种新的函数:Arrow Function(箭头函数)...

  • 箭头函数()=>{}与function的区别

    1.箭头函数与function定义函数的写法: 2.this的指向:使用function定义的函数,this的指向...

  • 箭头函数与function的区别

    1.写法不同 2.使用function定义的函数,this的指向随着调用环境的变化而变化的,而箭头函数中的this...

  • 2019-01-11

    ES6 箭头函数 箭头函数表示法:()=>console.log('Hello') 箭头函数和普通函数的区别 和普...

  • 箭头函数和立即执行函数

    箭头函数 箭头函数和普通函数有什么区别?如果把箭头函数转换为不用箭头函数的形式,如何转换主要是this的差别,箭头...

网友评论

      本文标题:箭头函数和function的区别

      本文链接:https://www.haomeiwen.com/subject/cqelektx.html