标识符和变量之间的区别(在 JavaScript 中) [英] Difference between identifier and variable (in JavaScript)

查看:37
本文介绍了标识符和变量之间的区别(在 JavaScript 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看这个查询为答案.

I had been looking through this query for the answer.

使用以下 JavaScript 语法:

With the below JavaScript syntax:

var var1 = 1;
var var2 = function(a, b){
         return a + b;
      };
var var3 = var2(3, 5);

我想知道,var1/var2/var3 是变量还是标识符.
与 JavaScript 中的 var 关键字有点混淆.

I would like to know, whether var1/var2/var3 are variables or identifiers.
A bit confused with var keyword in JavaScript.

推荐答案

标识符和变量的区别,同名字和人的区别.

The difference between identifiers and variables is the same as that between names and people.

名字可以识别人.例如,他们还可以识别狗.名字不是人,也不是人名.但是你可以说我是 Amadan(因为说我的名字是 Amadan 听起来很笨拙).

Names identify people. They can also identify dogs, for example. Names are not people, nor are people names. But you can say that I am Amadan (since saying that I am identified by the name Amadan sounds clunky).

同理:

标识符标识变量.例如,它们还可以识别标签.标识符不是变量,也不是变量标识符.但是您可以说该变量是 var2(因为说这是由标识符 var2 标识的变量听起来很笨拙).

Identifiers identify variables. They can also identify labels, for example. Identifiers are not variables, nor are variables identifiers. But you can say that that variable is var2 (since saying that that is the variable identified by the identifier var2 sounds clunky).

我想知道,var1/var2/var3 是变量还是标识符.

I would like to know, whether var1/var2/var3 are variables or identifiers.

Amadan 是一个人还是一个名字?我想,两者都取决于您如何严格地看待它.而对于变量和标识符,答案又是完全平行的.

Is Amadan a person or a name? Both, I suppose, depending on how strictly you view it. And for variables and identifiers, the answer is again completely parallel.

我可以说,NameValue,一般吗?

Can I say, Name and Value, in general?

准确地说,值"是第三个概念,变量的内容"是相关的第四个概念.

To be precise, "value" is a third concept, and "contents" of a variable a related fourth one.

也许一个更好的变量类比是储物柜:它们有标识符(写在盒子上的数字)和内容(你放什么东西).变量不一定是值的内存位置,因为变量可以包含引用,而不是对象本身(有点像将一块房地产的地址放入储物柜,而不是试图填充一个整个房子都放进盒子里).所以,在这个延伸的例子中,房子就是价值;储物柜是变量;写在储物柜上的#284是标识符;而那张写着102 Nowhere Lane, Nowhereville"的纸是对值的引用,也是变量的内容.如果该值足够小且足够简单(用编程术语来说,就是原始值"),您可以将值本身填充到变量中,而不是将其填充到引用中.

Maybe a better analogy for a variable would be locker boxes: they have identifiers (the number written on the box) and contents (whatever you put inside). A variable is not necessarily the memory location of a value, because a variable can contain a reference, and not an object itself (kind of like putting an address of a piece of real-estate into a locker, as opposed to trying to stuff a whole house into the box). So, in this stretched example, the house is the value; the locker is the variable; the #284 written on the locker is the identifier; and the piece of paper with "102 Nowhere Lane, Nowhereville" is a reference to the value, and also the contents of the variable. If the value is small and simple enough (in programming terms, a "primitive"), you can stuff the value itself into the variable, instead of the reference.

例如:

var a = 1;         // assign a value
var b = [2, 3, 4]; // assign a reference
var aa = a;        // copy the contents
var bb = b;        // copy the contents

声明四个变量(abaabb)和四个标识符来命名它们(还有 abaabb);它还提到了许多值(1234、数组 [2,3, 4]).aaa 每个都包含原始值 1 的不同副本.b 包含对 [2, 3, 4] 值的 引用(不是值 [2, 3, 4] 本身!),它又包含值 234.bb 包含另一个副本......参考!因此,如果您更改 b 中包含的值,bb 中的值也会自动更改:

declares four variables (a, b, aa, bb), and four identifiers to name them (also a, b, aa and bb); it also mentions many values (1, 2, 3, 4, the array [2, 3, 4]). a and aa each contain a different copy of the primitive value 1. b contains the reference to the value [2, 3, 4] (not the value [2, 3, 4] itself!), which, in turn, contains the values 2, 3 and 4. bb contains another copy of... the reference! So if you change the value that is contained in b, the value in bb automagically changes too:

b.push(5);
console.log(b);
// => [2, 3, 4, 5]
console.log(bb);
// => [2, 3, 4, 5]

函数也是值.

function hello(name) {
  console.log("Hello, " + name);
}

与(几乎但不是 100%)相同

is (almost but not 100%) identical to

var hello = function(name) {
  console.log("Hello, " + name);
}

定义了一个变量,其标识符为 hello,其内容是对函数的引用.函数本身就是一个值.

which defines a variable whose identifier is hello, and whose contents is a reference to a function. The function itself is a value.

这篇关于标识符和变量之间的区别(在 JavaScript 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆