javascript 符号_理解JavaScript中“ =”符号的直观指南

javascript 符号

by Kevin Kononenko

凯文·科诺年科(Kevin Kononenko)

理解JavaScript中“ =”符号的直观指南 (A Visual Guide to Understanding the “=” Sign in JavaScript)

实际上,对于第一次学习编码的人来说,赋值运算符(或“ =”符号)实际上会产生误导。 (The assignment operator, or “=” sign, is actually very misleading for anyone that is learning to code for the first time.)

You are taught about the concept of the equals sign for your entire life in math class.

在数学课上,您会学到一生的等号概念。

2 x 3 = 6

2 x 3 = 6

x²-4 = 0

x²-4= 0

The things on the left side of the equation are equal in value to the things on the right side of the equation. They could be flipped at any time, and the statement would still be true.

等式左侧的值与等式右侧的值相等。 它们可以随时翻转,但声明仍然正确。

And then JavaScript comes in like the Kool-Aid man and completely destroys this understanding.

然后,JavaScript像Kool-Aid一样出现,并彻底破坏了这种理解。

Oh, and don’t get me started with the concept of variables. In algebra class, we are taught that variables can only be equal to numbers that satisfy the equation. For example,

哦,别让我开始使用变量的概念。 在代数课上,我们被教导变量只能等于满足方程式的数字。 例如,

x²-4x+3 = 0

x²-4x+ 3 = 0

In the equation above, x can only be 1 or 3. But in JavaScript, the concept of a variable is actually quite different than what you learnt in algebra class.

在上面的等式中, x只能是1或3。但是在JavaScript中,变量的概念实际上与您在代数类中学到的完全不同。

This is a huge issue! It means that every time a newbie looks at an “=” sign when they are learning about variables, they need to repeat in their head over and over:

这是一个巨大的问题! 这意味着,每当新手学习变量时,每次查看“ =”符号时,都需要反复反复进行以下操作:

It’s not what you think it means.

这不是您认为的意思。

It’s not what you think it means.

这不是您认为的意思。

It’s not what you think it means.

这不是您认为的意思。

I wanted to create a more memorable way to explain variables than re-teaching what an “=” sign means. By the end of this tutorial, you will understand why the “=” in variable assignment is more like a ramp that loads up a truck.

我想创建一种比重新教“ =”符号更有意义的方式来解释变量。 在本教程结束时,您将理解为什么变量分配中的“ =”更像是装载卡车的坡道

This should create a clear guide about the purpose of variables and how to use them throughout your script.

这应该为变量的目的以及如何在整个脚本中使用它们创建清晰的指南。

变量的名称和值 (The Name and the Value of a Variable)

Variables are containers for carrying values within your script. In some ways, they are the opposite of variables from algebra.

变量是用于在脚本中携带值的容器。 在某些方面,它们与代数变量相反。

  • You can always give them a new value and restart your script. There is no “permanent” equality to satisfy some condition.

    您总是可以给他们一个新值,然后重新启动脚本。 没有满足某些条件的“永久”平等。
  • The left side of the statement has a completely different purpose than the right side of the statement.

    语句的左侧与该语句的右侧具有完全不同的目的。

Here is an example:

这是一个例子:

let days = 7;

This is called declaring the variable. It creates a new truck called days that can drive around your script and deliver its value OR pick up a new value.

这称为声明变量。 它创建了一个名为days的新卡车,可以围绕您的脚本行驶并提供其价值或获取新的价值

  • The let keyword announces that you are creating a new variable. Or, in the analogy we are about to use, creating a new truck.

    let 关键字宣布您正在创建一个新变量。 或者,以此类推,我们将使用它来创建一辆新卡车。

  • The variable needs a unique name, which is days here. This distinguishes this truck from all the other trucks.

    变量需要一个唯一的名称 ,这里是 。 这使这辆卡车与所有其他卡车区分开来。

  • The assignment operator, or “=” sign, loads the value 7, into the truck.

    分配运算符 (或“ =”符号)将 7装入卡车。

It is very hard to break the habit of looking at this like it is math class all over again, so I am going to explain a little more about the different parts of the variable truck.

很难重新习惯像数学课这样的习惯,所以我将对可变卡车的不同部分进行更多的解释。

This is the left side of the variable statement. It is not an equation! We are creating a truck with a specific name that we can use over and over again. Any time we look at the left side of the statement, we are calling in a truck with a specific name.

这是变量语句的左侧。 这不是方程式! 我们正在创建具有特定名称的卡车,我们可以反复使用它。 每当我们查看语句的左侧时,我们都在呼叫带有特定名称的卡车。

The assignment operator is just like the ramp of a truck. It loads up a new value. You can load up a new value pretty much any time with the let keyword.

分配操作员就像卡车的坡道。 它加载了一个新值。 您几乎可以随时使用let 关键字加载新值。

As a programmer, we are continuously creating new variables, loading up values and watching the changes around the script.

作为程序员,我们正在不断创建新变量,加载值并观察脚本周围的变化。

将值重新分配给变量 (Reassigning Values to Variables)

So far, we can create a truck that can drive around the script and deliver its value. But what about changing the value that the truck is carrying around?

到目前为止,我们可以创建一辆可以围绕脚本行驶并实现其价值的卡车。 但是,如何改变卡车随身携带的价值呢?

The let keyword allows us to create mutable variables whose values can be changed. If we used the const keyword, it would mean that the value is immutable and unchangeable.

let 关键字使我们可以创建可变变量,其值可以更改。 如果我们使用const关键字,则意味着该值是不可变的且不可更改。

In JavaScript, unlike math, you can simply assign a new value to the variable. Our days variable currently stands for the 7 days in a week. But what if we wanted it to stand for the 5 weekdays? Here is the code we could use.

在JavaScript中,与数学不同,您可以简单地为变量分配一个新值。 我们的天数变量当前代表一周中的7天。 但是,如果我们希望它能在5个工作日内站立,该怎么办? 这是我们可以使用的代码。

  1. In line 2, we create the days variable with a value of 7.

    在第2行中,我们创建的days变量的值为7。

  2. On line 4, we re-assign the value of the variable. It is now 5.

    在第4行,我们重新分配变量的值。 现在是5。

  3. On line 6, the days truck arrives with the value of 5.

    在第6行,天卡车到达的值为5。

In the GIF above, line 4 puts a new value in the truck that is later used in line 6.

在上面的GIF中,第4行在卡车中添加了一个新值,该值随后在第6行中使用。

Here is what happens in line 6.

这是第6行发生的情况。

The variable days is not “equal” to anything! It merely carries around the value that you assign to it. This is much more control than you have in math class, where you must discover the value of the variable that satisfies the equation. Now, you are in control!

可变的天数不等于任何东西! 它仅包含您为其分配的值。 这比您在数学类中拥有的控制要多得多,在数学类中,您必须发现满足方程式的变量的值。 现在,您已掌控一切!

为什么需要变量? (Why Do You Need Variables?)

Imagine that you are building an App that tells patients when to take their medication. You need to change the number of days per week based on the medication. Here is a quick snippet.

想象一下,您正在构建一个告诉患者何时服药的应用程序。 您需要根据药物更改每周的天数。 这是一个简短的摘要。

  1. In line 2, days gets loaded up with a value of 7.

    在第2行中,天的值是7。
  2. In line 4, the value of 5 gets loaded up instead.

    在第4行中,取值为5。

In lines 4 and 6, you use the value of the days variable. Could you hard code this by simply putting the number 7 in line 4 and the number 5 in line 6? Of course you could!

在第4和第6行中,使用days变量的 。 您能通过简单地在第4行中输入数字7和在第6行中输入数字5来对此进行硬编码吗? 当然可以!

But, as your App grows, you will find that variables are helpful for 2 reasons:

但是,随着您的应用程序的增长,您会发现变量有帮助的原因有两个:

  1. Instantly changing all the appropriate values at once. Let’s say you had three medications that need to be taken for 7 days a week, and three medications that need to be taken for 5 days a week. You don’t want to constantly change the value of days back and forth! You would instead want to use two separate variables. That gives you two separate trucks to drive values around your script.

    立即一次更改所有适当的值。 假设您有三种药物需要每周7天服用,而三种药物则需要每周5天服用。 你不想不断地改变来回! 相反,您想使用两个单独的变量。 这样就可以给您两个单独的卡车来驱动脚本中的值。

  2. Remembering what a value stands for. If you hard code a value, you may look back and say, why the heck is that 7? But, if you create a variable, you will remember that it stands for 7 days of the week so you can quickly change it if needed.

    记住价值的含义。 如果您硬编码一个值,您可能会回头说,为什么那是7? 但是,如果创建一个变量,您会记住它代表一周的7天,因此您可以根据需要快速进行更改。

赋值运算符右侧的变量名 (Variable Names on Right Side of the Assignment Operator)

So far, we have had a pretty hard rule. The name of the variable is on the left side of the assignment operator, while the value is on the right side.

到目前为止,我们有一个非常严格的规则。 变量的名称在赋值运算符的左侧,而值在右侧。

But what if we have a situation like this?

但是,如果我们遇到这种情况怎么办?

In line 4, the variable name is on both sides of the assignment operator! This is yet another reason why it is NOT an equals sign! In fact, the relationship between the two sides of the statement stays the same.

在第4行中,变量名称在赋值运算符的两侧! 这是为什么它不是等号的另一个原因! 实际上,声明两侧的关系保持不变。

In line 4, we load up a new value onto the days variable. Here is what that looks like.

在第4行中,我们将新值加载到days变量中。 看起来像这样。

Notice how we start at the assignment operator and calculate the right side of the statement first? That is because we are assigning a new value to days here. We cannot touch the left side of the statement. Here is what happens next.

请注意,我们如何从赋值运算符开始并首先计算语句的右侧? 那是因为我们在这里为天分配一个新值。 我们无法触及声明的左侧。 接下来会发生什么。

The days truck pulls up twice in this case. The first time is on the right side of the equation to deliver the old value. And the second time is on the left side of the equation to pick up the new value for days.

在这种情况下,天卡车两次起升。 第一次是在等式的右侧,以传递旧值。 第二次是在等式的左侧,以获取几天的新值。

Our new value for the days variable is 9. In our log statement on line 6, the console would log 9.

我们的days变量的新值为 9。在第6行的log语句中,控制台将记录9。

呼吁采取行动 (Call To Action)

Did you enjoy this? Give it a clap so others can discover it as well. And, if you want to get notified when I release future tutorials that use analogies, sign up here:

你喜欢这个吗? 给它鼓掌,以便其他人也可以发现它。 而且,如果您希望在以后发布使用类比的教程时得到通知,请在此处注册:

翻译自: https://www.freecodecamp.org/news/a-visual-guide-to-understanding-the-sign-in-javascript-3de8495ab3f/

javascript 符号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值