Docs Home / mongosh / Write Scripts

Code Scoping代码范围界定

When JavaScript is loaded into mongosh, top-level functions and variables defined with const, var, and let are added to the global scope.当JavaScript加载到mongosh中时,用constvarlet定义的顶级函数和变量被添加到全局作用域中。

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_VERSIONloadedFlagunloaded与函数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. This guards against accidental scope collisions with similarly named elements in the global scope.为了避免与其他代码中定义的函数和变量发生冲突,请务必在编写脚本时考虑作用域。作为最佳实践,MongoDB建议包装代码以限制范围。这可以防止在全局范围内与同名元素发生意外的范围冲突。

One way to keep functions and variables out of global scope is to wrap your code like this:将函数和变量排除在全局范围之外的一种方法是这样包装代码:

'use strict';
(() => {
...
})()

Tip

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()exposedYearlySales添加到全局作用域。

When you, call exposedYearlySales() it calls the yearlySales() function. The yearlySales() function is not directly accessible.当你调用exposedYearlySales()时,它会调用yearlySales()函数。yearlySales()函数不能直接访问。