【发布时间】:2021-11-10 12:15:43
【问题描述】:
当我在 JavaScript 和 Node.js 上找到 URL 时,我正在搜索 URL 模块。
我有几个问题:
- JavaScript
URL是否与 Node.js URL 不同? - Node.js 中的
URL模块与 JavaScript 的特点? - 在文档中,
URL被称为全局对象。所以 您不再需要url模块?
浏览器兼容的 URL 类,按照 WHATWG URL 实现 标准。已解析 URL 的示例可以在标准本身中找到。 URL 类也可用于全局对象。
在全局对象平台里面,我也看到了http、path、os等模块。
我想在不需要的情况下使用 os 和 path 模块(因为它们在全局对象中),但我的文件执行失败:
> node test.js
console.log(path); // ReferenceError: path is not defined
我也写了下面的代码,但是输出中打印了未定义的值:
console.log(global.path); // undefined
- 是什么原因?
但是当我在 REPL 中使用这些时,它会返回以下输出:
<ref *1> {
resolve: [Function: resolve],
normalize: [Function: normalize],
isAbsolute: [Function: isAbsolute],
join: [Function: join],
relative: [Function: relative],
toNamespacedPath: [Function: toNamespacedPath],
dirname: [Function: dirname],
basename: [Function: basename],
extname: [Function: extname],
format: [Function: bound _format],
parse: [Function: parse],
sep: '\\',
...
...
- REPL和脚本文件有区别的原因是什么?
感谢您的关注。期待您的回复。
【问题讨论】:
标签: javascript node.js url global-object