简体   繁体   English

JavaScript中的'let'和'this'有什么区别?

[英]What is the difference between 'let' and 'this' in JavaScript?

I'm learning JavaScript, coming from Ruby, have done some stuff in C as well.我正在学习 JavaScript,来自 Ruby,也在 C 中做了一些东西。 One example in my reading is:我阅读的一个例子是:

function letCounter() {
  let counter = 0;

  return () => ++counter;
}

and is compared to并与

function thisCounter() {
  this._count = 0;
}

thisCounter.prototype.fire = function() {
  this._count ++;
  return this._count;
}

In the first example, the count is NOT accessible on an instance of letCounter:在第一个示例中,计数在 letCounter 的实例上不可访问:

let let_counter_ins = letCounter();
let_counter_ins.counter // <- undefined

while in the second, count, ~I think~, is an attribute of all instances of the function itself?而在第二个中,计数,~我认为~,是 function 本身的所有实例的属性?

let this_counter_ins = new thisCounter();
this_counter_ins.count  // <- 0

Seems like a function in JavaScript can have 'state' (calls to console.log(let_counter_ins()) will continuously increment a counter, as opposed to it starting over at 0).似乎 JavaScript 中的 function 可以具有“状态”(调用console.log(let_counter_ins())将不断增加计数器,而不是从 0 开始)。 But also this 'state', set with let is somehow different than a 'state' set with this ?但是这个用let设置的“状态”也与用this设置的“状态”有些不同? Seems like instances of both letCounter and thisCounter are keeping track of some counter, but how it is accessible is different?似乎letCounterthisCounter的实例都在跟踪某个计数器,但它的访问方式是不同的? Trying to understand what the difference is between setting function attributes with this vs let and why one of them is available externally.试图了解使用this vs let设置 function 属性之间的区别是什么,以及为什么其中一个在外部可用。

In the first code, a local variable counter is initialized, and the returned function closes over the variable (in a "closure").在第一个代码中,局部变量counter被初始化,返回的 function关闭变量(在“闭包”中)。 The value only exists in the local scope of the function - in a Lexical Environment that, once the function finishes, is only referenceable via the () => ++counter .该值仅存在于 function 的本地 scope 中 - 在词法环境中,一旦 function 完成,只能通过() => ++counter引用。 There's no way to directly observe the contents of a closure, other than by the methods that the closure may (or may not) have returned or use that deliberately expose its contents.没有办法直接观察闭包的内容,除非闭包可能(或可能不会)返回或使用故意公开其内容的方法。

Every call of letCounter will result in a new binding for counter being created, a new Lexical Environment for it, and a new closure, so separate calls of letCounter will result in separate counts being kept track of.每次调用letCounter都会导致创建一个新的counter绑定、一个新的词法环境和一个新的闭包,因此单独调用letCounter将导致跟踪单独的计数。

In the second code, with this , counter is not a local variable, but a normal property of the instance object. Object properties are completely public (with the exception of the extremely new # private member syntax I won't get into), so anything, including thisCounter.prototype.fire and external code can access this.counter (where this references the instance) or this_counter_ins.count (where this_counter_ins references the instance) and get the current value on the object.在第二个代码thiscounter不是一个局部变量,而是实例 object 的一个普通属性#属性是完全公开的(除了非常新的 #private 成员语法我不会进入),所以任何东西,包括thisCounter.prototype.fire和外部代码都可以访问this.counter (其中this引用实例)或this_counter_ins.count (其中this_counter_ins引用实例)并获取 object 上的当前值。

let is used to declare a variable which is scoped only in its parent bracket. let 用于声明一个仅在其父括号内作用域的变量。 (Not visible outside of brackets) (括号外不可见)

this is, completely different.这是,完全不同的。 It represents the object owning the actual method.它代表拥有实际方法的 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript 中的“for(let item of iter)”和“for(item of iter)”有什么区别 - What's the difference between "for(let item of iter)" and ''for(item of iter)" in JavaScript "let" 和 "var" 和有什么不一样? - What is the difference between "let" and "var"? JavaScript 中的“let”和“var”之间是否存在性能差异 - Is there a performance difference between 'let' and 'var' in JavaScript let foo = ({bar() {}}) 和 let foo = {bar() {}} 有什么区别? - What is the difference between let foo = ({bar() {}}) and let foo = {bar() {}}? JavaScript中a +1和a-1 +2有什么区别 - What is the difference between a + 1 and a - 1 +2 in javascript JavaScript中“ &gt;&gt;&gt;”和“ &gt;&gt;”有什么区别? - What is the difference between “>>>” and “>>” in JavaScript? JavaScript 中的 &#39; 和 &quot; 有什么区别? - What is the difference between ' and " in JavaScript? JavaScript中“和”有什么区别? - What is the difference between ' and " in JavaScript? 这2个javascript有什么区别? - What is the difference between these 2 javascript? 删除未定义的属性和const声明的属性有什么区别,让我们在Javascript中使用delete运算符? - what is the difference between delete an undefined property and a property which declared by const, let using delete operatorin Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM