思考一个问题:为什么箭头函数的this指向要设计成和普通函数不一样? --为了取代var self = this; 或.bind(this),以一种更便捷和巧妙的方式来从外部函数继承this In other words, arrow functions treat this like any other lexical variable. 翻译:换句话说,箭头函数对待
function Person( name){ = name; this.x=function(){ return this; } } let p = new Person("mcgrady"); console.log(p===p.x()) //true 1. 2. 3. 4. 5. 6. 7. 8. 9. //严格模式下,函数中的this是undefined "use strict" function f(){ console.log(this); //undefined } f();...
「The this keyword evaluates to the value of the ThisBinding of the current execution context.」「this 这个关键字代表的值为当前执行上下文的ThisBinding。」然后再来看看MDN 对this 的定义:「In most cases, the value of this is determined by how a function is called.」「在大多数的情况下,this ...
In JavaScript, there are four different ways to invoke a function, and each one of them provides different context objects for thethiskeyword to point at when executing a function. The four rules for bindingthisare determined by how we invoke functions in JavaScript. Understanding these four ways...
ES6 箭头函数 Arrow Function 前言 1. ES6 前定义函数 2. ES6 箭头函数语法 3. ES6 箭头函数返回值 4. 箭头函数中的 this 到底是谁 ? 前言 ES6 新增了一种新的函数: 箭头函数 Arrow Function 箭头函数相当于匿名函数,简化了函数定义,将原函数的 function 关键字和函数名都删掉,并使用 => ...
1. Arrow Function 的基本用法 1.1 本质就是匿名函数 普通定义一个函数,要用到function关键字,不管是存在提升现象的function foo() { }还是定义给变量var foo = function() { },这样的话调用时才能找到这个函数,就像每个人都有名字一样。
Javascript 采用的就是词法作用域。 因为采用词法作用域,函数的作用域在函数定义的时候就决定了。 采用静态作用域时,会从内向外寻找变量。 所以箭头函数寻找this会从内向外寻找。 var x=1; var obj={ x:2, print: () => { // 箭头函数使用词法作用域寻找 this。 从内向外寻找 this, 这里一直找到全局变量...
Gentle explanation of ‘this’ keyword in JavaScript 1. 迷之 this 对于刚开始进行 JavaScript 编程的开发者来说,this 具有强大的魔力,它像谜团一样需要工程师们花大量的精力去真正理解它。 在后端的一些编程语言中,例如Java、PHP,this仅仅是类方法中当前对象的一个实例,它不能在方法外部被调用,这样一个简单的...
In JavaScript,thiskeyword refers to theobjectwhere it is called. 1. this Inside Global Scope Whenthisis used alone,thisrefers to the global object (windowobject in browsers). For example, leta =this;console.log(a);// Window {}this.name ='Sarah';console.log(window.name);// Sarah ...
A function's this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode. JavaScript是一门比较奇特的语言,它的this与其他语言不一样,并且它的取值还取决于代码是否为严格模式("use strict")。