你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

[ECMAScript] es6对es5有哪些方面的优化呢?

2021/12/6 0:30:43

[ECMAScript] es6对es5有哪些方面的优化呢?

引入了let和const解决了var变量提升带来的问题,同时引入箭头函数解决this访问问题。

function Person(firstname, lastname) {
    this.firstname = firstname;
    this.lastname = lastname;

}
//输出undefined
Person.prototype.getNameInCallback = function () {
    setTimeout(functino(){
        console.log(this.firstname, this.lastname);
    }, 1000);
}
//输出heyanbo
Person.prototype.getNameInCallback = function () {
    setTimeout(functino(){
        console.log(this.firstname, this.lastname);
    }.bind(this), 1000);
}
//输出heyanbo
Person.prototype.getNameInCallback = function () {
let context = this;
    setTimeout(functino(){
        console.log(context.firstname, context.lastname);
    }.bind(this), 1000);
}
//输出heyanbo
Person.prototype.getNameInCallback = function () {
    setTimeout(() => {
      //箭头函数里面的this可以直接当成一个变量,直接从父级作用域链中等变量对象中获取
        console.log(this.firstname, this.lastname);
    }, 1000);
}
var p = new Person('he', 'yanbo');
p.getNameInCallback();

个人简介

我是歌谣,欢迎和大家一起交流前后端知识。放弃很容易,
但坚持一定很酷。欢迎大家一起讨论



主目录

与歌谣一起通关前端面试题