2024最全前端面试系列(ES6)(ES6你不得不知道的新特性)

function fn() {

return “Hello World”;

}

foo ${fn()} bar

// foo Hello World bar

Symbol


Symbol是ES6新增的基本类型。

Symbol 值通过Symbol函数生成。这就是说,对象的属性名现在可以有两种类型,一种是原来就有的字符串,另一种就是新增的 Symbol 类型。凡是属性名属于 Symbol 类型,就都是独一无二的,可以保证不会与其他属性名产生冲突。

// 没有参数的情况

let s1 = Symbol();

typeof s1 // “symbol”

let s2 = Symbol();

s1 === s2 // false

// 有参数的情况

let s1 = Symbol(‘foo’);

let s2 = Symbol(‘foo’);

s1 === s2 // false

箭头函数


(1)函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。即继承上下文的this对象

(2)不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误。

// 箭头函数

function id () {

() => { console.log(this.id) }

}

// ES5普通函数

function id () {

var _this = this;

function () {

console.log(_this.id)

}

}

补充:(this对象)

this表示当前对象,this的指向是根据调用的上下文决定。

全局环境:this对象始终指向window对象

局部对象:

1.全局作用域。this指向window

2.对象里面调用函数。this指向对象

let button = document.getElemetById(‘button’)

button.onclick = function () {

console.log(this) //this指向button对象

}

3.使用new实例化对象,在构造函数中的this指向实例化对象

let fn = function(){

this.id = ‘xiaoMing’

}

let fn1 = new fn() //this指向fn1对象

遍历


1.利用for…of遍历数组

2.利用for…in遍历对象中的属性

变量解构


ES6 逐点突破系列 – 变量的解构赋值

数组解构

let [x, , y] = [1, 2, 3];

x // 1

y // 3

对象解构

let data = {

id: 1,

name: ‘xiaoMing’,

age: 18

};

let { id, age } = data;

console.log(id, age); //1, 18

字符串解构

const [a, b, c, d, e] = ‘hello’;

a // “h”

b // “e”

c // “l”

d // “l”

e // “o”

Promise


扩展运算符


应用场景

  1. 复制对象

let a = [11, 12, 13]

let b = a

let c = […a]

a.push(14)

console.log(b) //[11, 12, 13, 14]

console.log© //[11, 12, 13]

P.S. 只能针对一维数组

const obj = {a:{b:1}}

const {…x) = obj

obj.a.b = 2

console.log(b) //{a:{b:2}}

  1. 合并对象

let a = {…obj1, …obj2}

P.S. 后面的属性会覆盖前面同名属性

let obj1 = {a:1, b:2}

let obj2 = {…obj1, …{a:2, b:4}}

console.log(obj2) //{a:2, b:4}

  1. 变量解构

const [first, …last] = [1, 2, 3]

console.log(last) //[2, 3]

  1. 扩展函数参数

function show(…a){

console.log(a) //[11, 12, 13]

}

show(11, 12, 13)

async-await


object.assign()


set和map数据结构


数组去重

function distinct (arr) {

return Aarry.from(new Set(arr))

}

哈希(两数之和)

function twoSum (arr, target) {

const map = new Map()

for(let i = 0; i < arr.length; i++){

const error = target - arr[i]

if(map.has(error)){

return [map.get(error), i]

}else{

map.set(arr[i],i)

}

  • 14
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值