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

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

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

bash 复制代码
 const obj={
    fullName:'zz',
    sayName(){
        console.log(`this.fullName`,this.fullName)//zz
    }
 }

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

 obj.sayName();
bash 复制代码
function sayName(){
    console.log(`this.fullName`,this.fullName)
}

const obj={
    fullName:'freemen'
}
sayName.call(obj);//this.fullName freemen
bash 复制代码
const sayName=(...args)=>{
    console.log(`args`,args)
}
sayName('a','b');//['a', 'b']
bash 复制代码
function Person(){
  this.name='cai';
  const target=new.target;
  console.log(`target`,target)
}
const obj=new Person;
console.log(`obj`,obj);
bash 复制代码
//箭头函数不允许使用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

bash 复制代码
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 ]
相关推荐
wangxiaoxuan21713 小时前
SLAM 的基本结构:前端、后端、回环与地图优化
前端·机器人·科沃斯八界
LGL6030A14 小时前
Java学习历程31——初识Spring Boot
java·spring boot·学习
Agent世界16 小时前
用 CDP 做发布自动化:四道机器闸门与产物验收
前端·javascript
FungLeo16 小时前
成为全栈·Vue 管理后台篇·编辑器图片上传:从文件选择到文章附件关联
前端·vue.js·编辑器·markdown·文件上传·formdata·附件管理
阿达和自己16 小时前
springboot学习-springboot引入MyBatis-Plus
spring boot·学习·mybatis
某哈压力大16 小时前
SSH 突然 Connection reset?不是密钥坏了,是 OpenSSL 把 sshd 干趴了
前端·node.js·命令行
程序员大阳16 小时前
副队长ES6+教程(3)--解构赋值
前端·javascript·es6·赋值·解构
旖旎夜光16 小时前
【C++ 基础入门】C++11 新特性(中):右值引用、移动语义与完美转发
开发语言·c++·笔记·学习
神膘护体小月半16 小时前
css - 元素背景图由 hover 触发,初次加载时闪动的解决办法
前端·javascript·css
程序员大阳17 小时前
副队长ES6+教程(2)--let 与 const
前端·javascript·es6·const·let·var