【前端学习】—箭头函数和普通函数的区别(十四)
  Cl85xNwk6bHe 2023年11月02日 23 0


【前端学习】—箭头函数和普通函数的区别(十四)

一、箭头函数和普通函数的区别

【前端学习】—箭头函数和普通函数的区别(十四)_学习

const obj={
    fullName:'zz',
    sayName(){
        console.log(`this.fullName`,this.fullName)//zz
    }
 }

 obj.sayName();
const obj={
    fullName:'zz',
    sayName:()=>{
        console.log(`this.fullName`,this.fullName)//undefined
    }
 }

 obj.sayName();
function sayName(){
    console.log(`this.fullName`,this.fullName)
}

const obj={
    fullName:'freemen'
}
sayName.call(obj);//this.fullName freemen
const sayName=(...args)=>{
    console.log(`args`,args)
}
sayName('a','b');//['a', 'b']
function Person(){
  this.name='cai';
  const target=new.target;
  console.log(`target`,target)
}
const obj=new Person;
console.log(`obj`,obj);
//箭头函数不允许使用new.target

const Person=()=>{
  this.name='cai';
  const target=new.target;
  console.log(`target`,target)
}
const obj=new Person;
console.log(`obj`,obj);//Uncaught SyntaxError: new.target expression is not allowed here

二、ES6中哪个方法可以实现数组去重

es6中实现数组去重的方法是Array.from配合new Set

【前端学习】—箭头函数和普通函数的区别(十四)_ES6_02

const array=[1,2,3,4,2,1,2];
//const result=new Set(array);//输出的结果是对象我们使用Array.from方法转化为数组

const result=Array.from(new Set(array));
console.log(result);//[ 1, 2, 3, 4 ]


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论