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

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

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

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 ]
相关推荐
小雨下雨的雨3 小时前
井字棋AI机器人实现详解 - Minimax算法实战-鸿蒙PC Electron框架完成
前端·人工智能·算法·华为·electron·鸿蒙
数智工坊7 小时前
机器人运动控制:采样、优化与学习三大流派深度对比与实战
android·学习·机器人
ZC跨境爬虫7 小时前
跟着 MDN 学JavaScript day_7:数学运算与逻辑判断实战测试
开发语言·前端·javascript·学习·ecmascript
fangdengfu1237 小时前
ES分析系统各个服务日志占用量
java·前端·elasticsearch
JustHappy9 小时前
古法编程秘籍(六):程序到底是怎么跑起来的?从 IO 到中断,一次讲明白
前端·后端·全栈
HYCS9 小时前
用pixi.js实现fabric.js(六):从线性代数的角度理解编辑器交互
前端·javascript·canvas
卷帘依旧9 小时前
useImperativeHandle的作用
前端
MartinYeung510 小时前
[论文学习]隐私保护联邦特徵选择与差分隐私的的工程实践框架
学习
卷帘依旧10 小时前
Hooks在Fiber上的存储原理
前端