gpt4 book ai didi

javascript - 我可以枚举 JavaScript 模块的导出吗?

转载 作者:行者123 更新时间:2023-11-28 17:44:59 26 4
gpt4 key购买 nike

下面的valuesfile-2.js中的普通对象吗?

// file-1.js
export {
FOO,
BAR
} from '~/my-values';

// file-2.js
import * as values from '~/file-1';

// what is `values` here? an object instance?

我问这个问题是因为我想使用 values.hasOwnProperty('FOO') 之类的东西,并且 rollup 引发了以下错误:

'hasOwnProperty' is not exported by 'file-1.js'

附带问题:是否有一种方便的方法使用 import 和 export 关键字来测试代码?据我所知,Chrome 开发工具不支持它们的代码片段。

最佳答案

这些是module namespace exotic objects 。它们是奇异的意味着它们不完全是正常的对象(它们有不同的内部结构)。

它们的原型(prototype)链上也没有 Object.prototype,因此您不能使用 hasOwnProperty,但您应该能够将它与 Function 一起使用.prototype.callFunction.prototype.apply 因为它们确实有 [[ GetOwnProperty ]] 的内部方法

Object.prototype.hasOwnProperty.call(values, 'FOO');

您还可以对其使用Reflect.has

Reflect.has(values, 'FOO');

此外,由于原型(prototype)为 null,您几乎也可以使用 in :

'FOO' in values

关于javascript - 我可以枚举 JavaScript 模块的导出吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46954573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com