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

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

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

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 ]
相关推荐
AI成长日志3 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
xiaotao1313 小时前
第九章:Vite API 参考手册
前端·vite·前端打包
午安~婉4 小时前
Electron桌面应用聊天(续)
前端·javascript·electron
彧翎Pro4 小时前
基于 RO1 noetic 配置 robosense Helios 32(速腾) & xsense mti 300
前端·jvm
_李小白4 小时前
【OSG学习笔记】Day 38: TextureVisitor(纹理访问器)
android·笔记·学习
小码哥_常4 小时前
解锁系统设置新姿势:Activity嵌入全解析
前端
之歆5 小时前
前端存储方案对比:Cookie-Session-LocalStorage-IndexedDB
前端
哟哟耶耶5 小时前
vue3-单文件组件css功能(:deep,:slotted,:global,useCssModule,v-bind)
前端·javascript·css
是罐装可乐5 小时前
深入理解“句柄(Handle)“:从浏览器安全到文件系统访问
前端·javascript·安全
杨云龙UP5 小时前
从0到1快速学会Linux操作系统(基础),这一篇就够了!
linux·运维·服务器·学习·ubuntu·centos·ssh