从导入的 ES 模块中读取 import.meta
Read import.meta from imported ES modules
import.meta
returns information about the current module. The spec 表示这个对象是可扩展的:
The committee's tentative decision [that] by default the import.meta object will be extensible, and its properties will be writable, configurable, and enumerable.
There is no real benefit to locking down the object, as it is local to the module, and can only be shared explicitly by passing it around. It does not represent "global state" or anything of the sort.
如果我在模块 A 的 import.meta
上定义一个 属性:
// module A
import.meta.test = "hello";
export default () => {};
导入模块 A 时可以读取 属性 吗?如果是,怎么做?
import moduleA from "./module-a.js";
console.log(moduleA.meta); // undefined
// or...
async function () {
const moduleA = import("./module-a.js");
console.log(moduleA.meta); // undefined
}
你自己的话说明了一切:
as it is local to the module, and can only be shared explicitly by passing it around.
import.meta
returns information about the current module. The spec 表示这个对象是可扩展的:
The committee's tentative decision [that] by default the import.meta object will be extensible, and its properties will be writable, configurable, and enumerable.
There is no real benefit to locking down the object, as it is local to the module, and can only be shared explicitly by passing it around. It does not represent "global state" or anything of the sort.
如果我在模块 A 的 import.meta
上定义一个 属性:
// module A
import.meta.test = "hello";
export default () => {};
导入模块 A 时可以读取 属性 吗?如果是,怎么做?
import moduleA from "./module-a.js";
console.log(moduleA.meta); // undefined
// or...
async function () {
const moduleA = import("./module-a.js");
console.log(moduleA.meta); // undefined
}
你自己的话说明了一切:
as it is local to the module, and can only be shared explicitly by passing it around.