ES6模块范围

编程入门 行业动态 更新时间:2024-10-26 12:27:47
本文介绍了ES6模块范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有代码:

// lib.js var a = "a"; export var b = "b"; // main.js console.log(a); // "a" variable is not available in a global scope import {b} from "lib"; console.log(a); // is "a" variable available in a global scope or only in a module scope?

模块导入后,我可以在全局范围内使用"a"变量吗?还是仅在模块范围内可用? ES6模块是否具有类似此技巧的类似工作原理:

Can I use "a" variable in a global scope after module importing or is it available only in a module scope? Will ES6 modules have a similar working principle like this trick:

// module exports.module1 = (function(){ var a = "a"; })(); // "a" variable is not available in a global scope

推荐答案

模块导入后,我可以在全局范围内使用"a"变量吗?还是仅在模块范围内可用?

Can I use "a" variable in a global scope after module importing or is it available only in a module scope?

仅在声明它的模块内部可用.

It's only available inside the module it was declared in.

ES6模块是否具有类似此技巧的工作原理:[...]

Will ES6 modules have a similar working principle like this trick: [...]

基本上是.

ES6具有以下范围,从顶部"到底部"的顺序:

ES6 has these kinds of scopes, order from "top" to "bottom":

  • 全球范围
  • 模块作用域
  • 功能范围
  • 块范围

更多推荐

ES6模块范围

本文发布于:2023-07-24 13:23:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1203700.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!