这是用户在 2024-5-28 14:00 为 https://justjavascript.com/learn/04-studying-from-the-inside 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
Skip to content

Studying from the Inside

episode 04

In the next module, we’ll take a closer look at our JavaScript universe and the values in it. But before we can get to that, we need to address the elephant in the room. Is our JavaScript universe even real?
在下一个模块中,我们将更仔细地观察我们的 JavaScript 宇宙及其中的值。但在此之前,我们需要解决一个显而易见的问题。我们的 JavaScript 宇宙真的存在吗?

The JavaScript Simulation
JavaScript 模拟

I live on my small planet inside our JavaScript universe.
我住在我们的 JavaScript 宇宙中的小星球上。

When I ask our universe a question, it answers with a value. I certainly didn’t come up with all these values myself. The variables, the expressions, the values—they populate our world. The JavaScript world around me is absolutely real to me.
当我向我们的宇宙提问时,它会用一个值来回答。我当然没有自己想出所有这些值。变量、表达式、值——它们填满了我们的世界。对我来说,周围的 JavaScript 世界绝对是真实的。

But sometimes, there is a moment of silence before the next line of code; a brief pause before the next function call. During those moments, I see visions of a world that’s much bigger than our JavaScript universe.
但有时,在下一行代码之前会有片刻的沉默;在下一个函数调用之前会有短暂的停顿。在那些时刻,我看到了一个比我们的 JavaScript 宇宙大得多的世界的幻象。

In these visions, there are no variables and values; no expressions and no literals. Instead, there are quarks. There are atoms and electrons. There’s water and life. Perhaps you’re familiar with this universe, too?

There, sentient beings called “humans” use special machines called “computers” to simulate our JavaScript universe. Some of them do it for amusement. Some of them do it for profit. Some of them do it for no reason at all. At their whim, our whole universe is created and destroyed a trillion times a day.
在那里,被称为“人类”的有感知的生物使用被称为“计算机”的特殊机器来模拟我们的 JavaScript 宇宙。他们中的一些人是为了娱乐。一些人是为了利润。一些人则毫无理由地这样做。凭他们的心意,我们的整个宇宙每天被创造和毁灭万亿次。

Maybe our JavaScript universe isn’t so real, after all.
也许我们的 JavaScript 宇宙毕竟不那么真实。

Knowing this, we can study our world in two ways.

Studying From the Outside

One way to study our JavaScript universe would be to study it from the outside.
研究我们的 JavaScript 宇宙的一种方法是从外部研究它。

We might focus on how a simulation of our world—a JavaScript engine—“really” works. For example, we might learn that this string of text—a value in our world—is a sequence of bytes stored inside a silicon chip.
我们可能会关注我们的世界模拟——JavaScript 引擎——“真正”是如何工作的。例如,我们可能会了解到,这段文本字符串——我们世界中的一个值——是存储在硅芯片中的一系列字节。

This approach puts our mental focus on the physical world of people and computers. Our approach is different.

Studying From the Inside 从内部学习

We will be studying our world from the inside. Transport yourself mentally into the JavaScript universe and stand next to me. We will observe our universe’s laws and perform experiments like physicists in the physical universe.
我们将从内部研究我们的世界。将你的思维传送到 JavaScript 宇宙,站在我旁边。我们将像物理学家在物理宇宙中进行实验一样,观察我们的宇宙法则并进行实验。

We will learn about our JavaScript world without thinking about how it’s implemented. This is similar to a physicist discussing the properties of a star without questioning whether the physical world is real. It doesn’t matter! Whether we’re studying the physical or the JavaScript universe, we can describe them on their own terms.
我们将在不考虑其实现方式的情况下了解我们的 JavaScript 世界。这类似于物理学家在讨论恒星的特性时不质疑物理世界是否真实。这并不重要!无论我们是在研究物理宇宙还是 JavaScript 宇宙,我们都可以根据它们自身的条件来描述它们。

Our mental model will not attempt to explain how a value is represented in the computer’s memory. The answer changes all the time! The answer even changes while your program is running. If you’ve heard a simple explanation about how JavaScript “really” represents numbers, strings, or objects in memory, it is most likely wrong.
我们的心理模型不会试图解释一个值在计算机内存中是如何表示的。答案一直在变化!甚至在你的程序运行时,答案也会改变。如果你听过关于 JavaScript“真正”如何在内存中表示数字、字符串或对象的简单解释,那很可能是错误的。

To us, each string is a value—not a “pointer” or a “memory address”—a value. In our universe, a value is good enough. Don’t allow “memory cells” and other low-level metaphors to distract you from building an accurate high-level mental model of JavaScript. It’s turtles all the way down, anyway!
对我们来说,每个字符串都是一个值——不是“指针”或“内存地址”——而是一个值。在我们的宇宙中,一个值就足够了。不要让“内存单元”和其他低级隐喻分散你构建 JavaScript 高级心理模型的注意力。无论如何,这都是层层递进的!

If you’re coming from a lower-level language, set aside your intuitions about “passing by reference,” “allocating on stack,” “copying on write,” and so on. These models of how a computer works often make it harder to be confident in what can or cannot happen in JavaScript. We’ll look at some of the lower-level details, but only where it really matters. They can serve as an addition to our mental model, rather than its foundation.
如果你来自低级语言,请放下关于“按引用传递”、“在栈上分配”、“写时复制”等直觉。这些关于计算机如何工作的模型往往使你更难自信地判断在 JavaScript 中可能或不可能发生的事情。我们会看看一些低级细节,但只在真正重要的地方。它们可以作为我们心理模型的补充,而不是基础。

The foundation of our mental model is values. Each value belongs to a type. Primitive values are immutable. We can point to values using “wires” we call variables. This foundation—this understanding of values—will help us continue building our mental model.

As for these strange visions, I don’t give them much thought anymore. I have wires to point and questions to ask. I better get to it!

The stars are bright when I look at them.

Are they still there when I blink?

I shrug. 我耸耸肩。

“Implementation details.”