Javascript 什么是;这";参见ES6中的箭头功能?

Javascript 什么是;这";参见ES6中的箭头功能?,javascript,this,ecmascript-6,arrow-functions,Javascript,This,Ecmascript 6,Arrow Functions,我在好几个地方读到过,关键的区别在于这个在词汇上绑定在箭头函数中。这很好,但我不知道那是什么意思 我知道这意味着它在定义函数体的大括号范围内是唯一的,但我实际上无法告诉您以下代码的输出,因为我不知道这是指什么,除非它指的是胖箭头函数本身……这似乎没有用 var testFunction = () => { console.log(this) }; testFunction(); 因此,为了直接回答您的问题,您的arrow函数中的这个的值将与分配arrow函数之前的值相同。希望这个代码显

我在好几个地方读到过,关键的区别在于
这个
在词汇上绑定在箭头函数中。这很好,但我不知道那是什么意思

我知道这意味着它在定义函数体的大括号范围内是唯一的,但我实际上无法告诉您以下代码的输出,因为我不知道这是指什么,除非它指的是胖箭头函数本身……这似乎没有用

var testFunction = () => { console.log(this) };
testFunction();


因此,为了直接回答您的问题,
您的arrow函数中的这个
的值将与分配arrow函数之前的值相同。

希望这个代码显示可以让您更清楚地了解。基本上,箭头函数中的“this”是“this”的当前上下文版本。参见代码:

// 'this' in normal function & arrow function
var this1 = {
    number: 123,
    logFunction: function () { console.log(this); },
    logArrow: () => console.log(this)
};
this1.logFunction(); // Object { number: 123}
this1.logArrow(); // Window 

为了提供全局,我将同时解释动态绑定和词汇绑定

动态名称绑定
引用调用该方法的对象。这是一个经常被读的句子。但它仍然只是一个短语,相当抽象。这个句子有对应的代码模式吗

是的,有:

const o = {
  m() { console.log(this) }
}

// the important patterns: applying methods

o.m(); // logs o
o["m"](); // logs o
m
是一种方法,因为它依赖于
this
o.m()
o[“m”]()
表示
m
应用于
o
。这些模式是Javascript对我们著名短语的翻译

还有一个重要的代码模式需要注意:

"use strict";

const o = {
  m() { console.log(this) }
}

// m is passed to f as a callback
function f(m) { m() }

// another important pattern: passing methods

f(o.m); // logs undefined
f(o["m"]); // logs undefined
它与前面的模式非常相似,只是缺少括号。但是后果是相当严重的:当您将
m
传递给函数
f
时,您将拉出其对象/上下文
o
m
。它现在被连根拔起,
不涉及任何内容(假定为严格模式)

词汇(或静态)名称绑定 箭头函数没有自己的
/
超级
/
参数
绑定。它们从其父词法范围继承它们:

const-toString=Object.prototype.toString;
常数o={
foo:()=>console.log(“窗口”,toString.call(this)),
bar(){
constbaz=()=>console.log(“o”,toString.call(this));
baz();
}
}
o、 foo()//日志窗口[对象窗口]

o、 bar()//日志o[object object]
您可以按照下面的方法尝试理解它

// whatever here it is, function or fat arrow or literally object declare
// in short, a pair of curly braces should be appeared here, eg:
function f() {
  // the 'this' here is the 'this' in fat arrow function below, they are
  // bind together right here
  // if 'this' is meaningful here, eg. this === awesomeObject is true
  console.log(this) // [object awesomeObject]
  let a = (...param) => {
    // 'this is meaningful here too.
    console.log(this) // [object awesomeObject]
}
因此,fat arrow函数中的“this”未绑定,这意味着您不能在此处将任何内容绑定到“this”。.apply不会、.call不会、.bind不会当您在文本编辑器中写下代码文本时,fat arrow函数中的“this”将被绑定这个“in-fat-arrow”函数在这里有字面意义。您在文本编辑器中编写的代码就是您的应用程序在repl中运行的代码除非在文本编辑器中更改,否则fat arror中的“this”绑定永远不会更改。
对不起,我的pool English…

箭头函数
指向Es6中周围的父函数,意味着它的作用域与ES5中的匿名函数不同

这是一种非常有用的方法,可以避免将var self赋值给ES5中广泛使用的var self

请看下面的示例,在对象内指定函数:

var checkThis = {
  normalFunction: function () { console.log(this); },
  arrowFunction: () => console.log(this)
};

checkThis.normalFunction(); //Object {}
checkThis.arrowFunction(); //Window {external: Object, chrome: Object, document: document, tmpDebug: "", j: 0…}

在另一个示例中,如果单击下面的“年龄”按钮

<script>
var person = {
    firstName: 'John',
    surname: 'Jones',
    dob: new Date('1990-01-01'),
    isMarried: false,
    age: function() {
        return new Date().getFullYear() - this.dob.getFullYear();
    }
};

var person2 = {
    firstName: 'John',
    surname: 'Jones',
    dob: new Date('1990-01-01'),
    isMarried: false,
    age: () => {
        return new Date().getFullYear() - this.dob.getFullYear();
    }
};

</script>



<input type=button onClick="alert(person2.age());" value="Age">


它将起作用,因为person2中的此作用域已更改。箭头函数从不与此关键字绑定

var env = "globalOutside";
var checkThis = {env: "insideNewObject", arrowFunc: () => {
console.log("environment: ", this.env);
} }

checkThis.arrowFunc()   // expected answer is environment: globalOutside

// Now General function 
var env = "globalOutside";
var checkThis = {env: "insideNewObject", generalFunc: function() {
console.log("environment: ", this.env);
} }
checkThis.generalFunc() // expected answer is enviroment: insideNewObject

// Hence proving that arrow function never binds with 'this'

在arrow函数中使用时,它将始终引用全局对象。使用常规函数声明引用本地对象。此外,您还可以使用对象名作为上下文(object.method,而不是this.method),使其引用本地对象而不是全局对象(窗口)。

它只是从包含范围捕获
this
的值,将其视为任何其他变量。这样您就不必做
var self=this
然后在函数中使用
self
。在您的情况下,没有封闭上下文,或者是全局上下文,或者是模块上下文,因此
这个
在这种情况下是什么,很可能是null或window。换句话说,
这个
的值与在函数赋值之前添加一个
控制台.log(这个)
的值完全相同。@torazaburo迟来的回答——答案取决于原始问题中代码片段的位置。如果它位于顶层,
如果我们在浏览器中,
窗口
对象,如果我们在节点环境中,
模块。导出
。关键是,箭头函数对
这个
的值没有影响。来自@dave的评论,“
这个
在你的箭头函数中的值将与它在分配箭头函数之前的值相同”,这就是它最终让我点击的原因。非常简单和好的例子。使用
函数时
在调用函数时创建
值。因此,当您将其称为
this1.logFunction()
时,您将其称为对象
this1
的方法,并且
引用this1文本对象。另一方面,如果使用arrow函数,
这个
值不是根据调用/调用的方式创建的,而是从词法作用域继承的,在这种情况下,词法作用域是
窗口
对象,定义了这个1 obejct。您能帮我一下吗,为什么没有严格模式,它会记录窗口,但使用严格模式,它没有定义?这是严格模式的哪个属性?
return new Date().getFullYear() - this.dob.getFullYear(); 
return new Date().getFullYear() - person2.dob.getFullYear();
var env = "globalOutside";
var checkThis = {env: "insideNewObject", arrowFunc: () => {
console.log("environment: ", this.env);
} }

checkThis.arrowFunc()   // expected answer is environment: globalOutside

// Now General function 
var env = "globalOutside";
var checkThis = {env: "insideNewObject", generalFunc: function() {
console.log("environment: ", this.env);
} }
checkThis.generalFunc() // expected answer is enviroment: insideNewObject

// Hence proving that arrow function never binds with 'this'