给一个对象“上下文化”是什么意思?
【What does it mean to "contextify" an object?】
在 Node.js 中执行的所有 JavaScript 都运行在一个“上下文”的范围内。 根据 V8 嵌入指南:
【All JavaScript executed within Node.js runs within the scope of a "context". According to the V8 Embedder's Guide:】
在 V8 中,context 是一种执行环境,它允许在单个 V8 实例中运行独立、无关的 JavaScript 应用。你必须明确指定希望运行任何 JavaScript 代码的上下文。
当调用方法 vm.createContext() 时,contextObject 参数(如果 contextObject 为 undefined,则使用新创建的对象)会在内部与一个新的 V8 上下文实例相关联。这个 V8 上下文为使用 node:vm 模块方法运行的 code 提供了一个隔离的全局环境,使其可以在其中操作。创建 V8 上下文并将其与 contextObject 关联的过程,就是本文档所称的“将对象上下文化”。
【When the method vm.createContext() is called, the contextObject argument
(or a newly-created object if contextObject is undefined) is associated
internally with a new instance of a V8 Context. This V8 Context provides the
code run using the node:vm module's methods with an isolated global
environment within which it can operate. The process of creating the V8 Context
and associating it with the contextObject is what this document refers to as
"contextifying" the object.】