在ES6语法之前,JS中的函数由function关键字、params参数和被花括号包裹的函数体组成。为了与后面说到的箭头函数相区别,我们先把这样的函数叫做常规函数,常规函数既可以用声明式写法也可以用赋值式写法。例子: function test(name) { //声明式写法 console.log(name) } test('Jerry') let test2 = function(name)...
function Person( name){ this.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...
在这段代码中,如果我把Dog.prototype.method = function(){}换成箭头函数,其他的不变,this.name就变成了undefined。当时使用nodejs和jsbin调试均是如此, 查了下此时arrow function中的this是global context,虽然知道这规则就是es6这样规定的,但是好奇心仍然得不到满足,今日偶然在知乎看见一个回答提到了更多信息。 ...
理解es6 中 arrow function的this 箭头函数相当于定义时候,普通函数.bind(this) 箭头函数根本没有自己的this,导致内部的this就是定义时候的外层代码块中的this。 外层代码块中的this,则取决于执行时候环境上下文context中的this 并不是所有的{}都可以代表是上下文环境或者代码块,例如 {x:1,y:2} ,就是简简单单的...
In the global execution context (outside of any function), this refers to the global object whether in strict mode or not. 代码没有在任何函数中执行,而是在全局作用域中执行时,this的值就是global对象,对于浏览器来说,this就是window。 这一条规则还是比较容易接受的。
An Arrow Function does not define local bindings for arguments, super, this, or new.target. Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment. Typically this will be the Function Environment of an immed...
An arrow function does not create its own this, the this value of the enclosing execution context is used. Thus, in the following code, the this within the function that is passed to setInterval has the same value as this in the enclosing function:...
In fatarrow functions, thisnever gets bound to a new value, no matter how the function is called. this will always be the same this value as its surrounding code. (By the way, lexical means relating to, which I guess, is how the lexical this got its name). ...
In that function call, this will refer to x. That function call returns a function, which is used in the second function call: x.y()() That function call has no explicit this value, so this is window. That function call returns another function, the innermost arrow function,...
Code flow analysis allows TypeScript to figure out what this is for an arrow function by looking at the context it is defined in, not because it can actually determine what it is bound to. It is not bound, therefore it doesn't have a this type. It doesn't "inherit" this, it uses...