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

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

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

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 ]
相关推荐
然我8 分钟前
不用 Redux 也能全局状态管理?看我用 useReducer+Context 搞个 Todo 应用
前端·javascript·react.js
前端小巷子13 分钟前
Web 实时通信:从短轮询到 WebSocket
前端·javascript·面试
神仙别闹17 分钟前
基于C#+SQL Server实现(Web)学生选课管理系统
前端·数据库·c#
web前端神器23 分钟前
指定阿里镜像原理
前端
枷锁—sha28 分钟前
【DVWA系列】——CSRF——Medium详细教程
android·服务器·前端·web安全·网络安全·csrf
枷锁—sha29 分钟前
跨站请求伪造漏洞(CSRF)详解
运维·服务器·前端·web安全·网络安全·csrf
群联云防护小杜1 小时前
深度隐匿源IP:高防+群联AI云防护防绕过实战
运维·服务器·前端·网络·人工智能·网络协议·tcp/ip
汉得数字平台1 小时前
【鲲苍提效】全面洞察用户体验,助力打造高性能前端应用
前端·前端监控
花海如潮淹1 小时前
前端性能追踪工具:用户体验的毫秒战争
前端·笔记·ux
_丿丨丨_6 小时前
XSS(跨站脚本攻击)
前端·网络·xss