Code Scoping代码范围界定
On this page本页内容
When JavaScript is loaded into 当JavaScript加载到mongosh
, top-level functions and variables defined with const
, var
, and let
are added to the global scope.mongosh
中时,用const
、var
和let
定义的顶级函数和变量会添加到全局范围中。
Consider the following code:请考虑以下代码:
const SNIPPET_VERSION = "4.3.2";
var loadedFlag = true;
let unloaded = false;
function isSnippetLoaded(loadedFlag) {
return ( loadedFlag ? "Snippet is loaded" : "Snippet is not loaded" )
}
The variables, 变量SNIPPET_VERSION
, loadedFlag
, and unloaded
are added to the global scope along with the function, isSnippetLoaded()
.SNIPET_VERSION
、loadedFlag
和unloaded
与函数isSnippetLoaded()
一起添加到全局作用域中。
To avoid collisions with functions and variables defined in other code, be sure to consider scope as you write scripts. 为了避免与其他代码中定义的函数和变量发生冲突,在编写脚本时一定要考虑作用域。As a best practice, MongoDB recommends wrapping your code to limit scope. 作为一种最佳实践,MongoDB建议包装您的代码以限制范围。This guards against accidental scope collisions with similarly named elements in the global scope.这样可以防止与全局作用域中名称相似的元素发生意外作用域冲突。
One way to keep functions and variables out of global scope is to wrap your code like this:将函数和变量排除在全局范围之外的一种方法是这样包装代码:
;
(() => {
...
})()
use strict;
is for use in scripts. 仅用于脚本中。If you enter 如果你在use strict;
in the mongosh
console directly, mongosh
will switch to a database called strict
.mongosh
控制台中输入use strict;
,mongosh
将直接切换到一个名为strict
的数据库。
Example: Restricting Scope示例:限制范围
Compare the following code samples. 比较以下代码示例。They are very similar, but the second one is written in a way that restricts variable scope.它们非常相似,但第二个是以限制变量范围的方式编写的。
Sample 1: Unrestricted scope.示例1:不受限制的范围。
let averageGrossSales = [ 10000, 15000, 9000, 22000 ];
const Q1_DISCOUNT = .10;
const Q2_DISCOUNT = .15;
const Q3_DISCOUNT = .06;
const Q4_DISCOUNT = .23;
function quarterlySales(grossAmount, discount ) {
return grossAmount * discount ;
}
function yearlySales() {
let annualTotal = 0;
annualTotal += quarterlySales(averageGrossSales[0], Q1_DISCOUNT );
annualTotal += quarterlySales(averageGrossSales[1], Q2_DISCOUNT );
annualTotal += quarterlySales(averageGrossSales[2], Q3_DISCOUNT );
annualTotal += quarterlySales(averageGrossSales[3], Q4_DISCOUNT );
return annualTotal ;
}
Sample 2: Restricted scope.示例2:限制范围。
(() => {
let averageGrossSales = [ 10000, 15000, 9000, 22000 ];
const Q1_DISCOUNT = .10;
const Q2_DISCOUNT = .15;
const Q3_DISCOUNT = .06;
const Q4_DISCOUNT = .23;
function quarterlySales(grossAmount, discount ) {
return grossAmount * discount ;
}
globalThis.exposedYearlySales = function yearlySales() {
let annualTotal = 0;
annualTotal += quarterlySales(averageGrossSales[0], Q1_DISCOUNT );
annualTotal += quarterlySales(averageGrossSales[1], Q2_DISCOUNT );
annualTotal += quarterlySales(averageGrossSales[2], Q3_DISCOUNT );
annualTotal += quarterlySales(averageGrossSales[3], Q4_DISCOUNT );
return annualTotal ;
}
} )()
In Sample 2, the following elements are all scoped within an anonymous function and they are all excluded from the global scope:在示例2中,以下元素都在匿名函数中起作用,并且都被排除在全局作用域之外:
The main function,主函数,yearlySales()
The helper function,辅助函数,quarterlySales()
The variables变量
The globalThis.exposedYearlySales = function yearlySales()
assignment statement adds exposedYearlySales
to the global scope.globalThis.exposedYearlySales = function yearlySales()
赋值语句将exposedyearlySSales
添加到全局范围中。
When you, call 当您调用exposedYearlySales()
it calls the yearlySales()
function. exposedYearlySales()
时,它会调用yearlySales()
函数。The yearlySales()
function is not directly accessible.yearlySales()
函数无法直接访问。