[Front-end learning]—The difference between arrow functions and ordinary functions (14)
1. The difference between arrow functions and ordinary functions
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
2. Which method in ES6 can achieve array deduplication?
The method to achieve array deduplication in es6 is Array.from combined with new Set
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 ]