简体   繁体   English

在Node.js上声明VAR

[英]Declaration of a VAR on Node.js

I am learning Node.js. 我正在学习Node.js。 On the book I am using there is a line that I am not sure I know exactly how it works and was looking for confirmation. 我正在使用的书上有一条线,我不确定我确切地知道它是如何工作的并且正在寻找确认。

The line is: 该行是:

var user = req.user = users[req.params.name];

I did some research on Google and the way I believe this works is: 我在Google上进行了一些研究,并且我相信这种工作方式是:

As a first step will copy the content of users[request.params.name] into req.user then as a second step will copy the content of req.user into user. 第一步将将users [request.params.name]的内容复制到req.user,然后第二步将req.user的内容复制到user。 In the end user == users[req.params.name]; 最终用户== users [req.params.name];

Can you please confirm that I am getting this right or, if not, explain how this line works? 能否请您确认我做对了,否则请解释这条线的工作方式?

thanks 谢谢

That's essentially it. 本质上就是这样。 What you have there is a compound assignment. 您所拥有的是复合作业。 The result of an assignment expression is the value being assigned, and so that value can be the right-hand side of another assignment expression. 赋值表达式的结果是被赋值,因此该值可以是另一个赋值表达式的右侧。

But there's actually something else that happens before any of that: The variable user gets defined in the current context. 但实际上在这之前还有其他事情发生:在当前上下文中定义变量user Here's a detailled order of operation: 这是详细的操作顺序:

  1. Upon entry to the context, the variable user is defined with the value undefined . 输入上下文后,变量user的定义值为undefined This happens before any step-by-step code in the context happens. 这发生在上下文中的任何分步代码发生之前。

  2. When the execution cursor reaches that code in the step-by-step execution of code, then: 当执行游标在逐步执行代码中到达该代码时,则:

    1. The property params is looked up on the variable req . 在变量req上查找属性params

    2. The property name is looked up on the result of req.params . req.params的结果中req.params属性name

    3. The property with the name defined by that value (the value of req.params.name ) is retrieved from the object (presumably) referenced by the users variable. 名称由该值定义的属性( req.params.name的值)是从users变量引用的对象(大概)中检索的。

    4. The resulting value is assigned to the user property on req . 结果值分配给req上的user属性。

    5. The same value is assigned to the user variable. 相同的值分配给user变量。

(I skipped a couple of bookkeeping steps to avoid overdoing the predantry.) (我跳过了一些簿记步骤,以免过度进行前期准备。)

It's important to note that between steps 2.4 and 2.5 above, the value of req.user is not read from the req.user property. 重要的是要注意,在上述步骤2.4和2.5之间, 不会req.user属性中读取req.user的值。 The value is determined in 2.3 and then used in 2.4 and again in 2.5. 该值在2.3中确定,然后在2.4中使用,然后在2.5中再次使用。 That is, a = b = c; 也就是说, a = b = c; is not the same as b = c; a = b; 一样的b = c; a = b; b = c; a = b; . The difference is that in the latter, the value is retrieved from b in the second assignment; 区别在于,后者是从第二个赋值中的b中检索值; in the former, it isn't, it's just reused from the b = c part. 在前者中,不是,只是从b = c部分重用了。 Now, when it's a simple property, that distinction doesn't particularly matter, but it does if you're using property accessor functions (either the non-standard kind from Mozilla's earlier variant of JavaScript, or the newly-standard kind from ECMAScript5). 现在,当它是一个简单的属性时,这种区别并不特别重要,但是,如果您使用的是属性访问器函数(不是Mozilla较早的JavaScript变体中的非标准类型,还是ECMAScript5的新标准类型),则区别不大。 。 Live Example of the difference | 直播实例的区别| Source 资源

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM