[翻译]Scope in JavaScript

Daily Tips原文地址(翻译练手文章篇幅不大,1到2分钟。)

2017年12月13日

JavaScript的作用域(Scope in JavaScript)

作用域是使用变量或者函数的上下文。有三种类型的作用域:全局作用域、局部作用域和词法作用域。(Scope is the context in which a function or variable is accessible. There are three types of scope: global, local, and lexical.)

高层次来说,函数能使用定义在他们本身之外的变量和其他函数。(At a high level, functions have access to variables and other functions set outside themselves, but not variables set inside other functions.)

全局变量(Global Scope)

在其他的函数中都可以使用定义在全局作用域中的变量和函数(A variable or function in the global scope is accessible inside other functions.)

// this is in the global scope(这是在全局作用域中)
var sandwich = 'tuna';

var logSandwich = function () {
    // Will log `tuna` in the console(将会打印tuna)
    // It can access sandwich because it`s in the global scope(可以使用sandwich,因为它在全局作用域中)
    console.log(sandwich);
};
logSandwich();

// Will also log `tuna` in the console(同样会打印tuna)
console.log(sandwich);
复制代码

局部作用域(Local Scope)

变量或者函数只能被你代码本身所在的局部作用域中使用(A variable or function that’s only accessible in a part of your code base has local scope.)

var logSandwich = function () {
    // this has variable local scope(这是局部作用域)
    var sandwich = 'tuna';

    // Will log `tuna` in the console(将会打印tuna)
    // It can access sandwich because it`s scope is local to the function(可以使用sandwich,因为它处于函数本身的局部作用域)
    console.log(sandwich);
};
logSandwich();

// returns "Uncaught ReferenceError: sandwich is not defined"(返回‘Uncaught ReferenceError: sandwich is not defined’)
// `sandwich` is local to the logSandwich() function, and not accessible here(sandwich是logSandwich函数的局部变量,在这里不能使用)
console.log(sandwich);
复制代码

词法作用域(Lexical Scope)

如果你把你的函数,变量和其他一些函数藏在父类函数里面定义就会产生词法作用域,并且能被定义在其中的嵌套函数使用。父类函数不能使用定义在其中的嵌套函数里的变量及其方法。(If you nest your functions, variables and other functions defined in the parent function have lexical scope and can be accessed by the inner funtions. The parent function cannot access variables or functions defined within the inner functions.)

var sandwiches = function () {

    // this is in the lexical scope(这是词法作用域)
    var sandwich = 'tuna';

    var logSandwich = function () {

        // Will log `tuna` in the console(将会打印tuna)
        // It can access sandwich because it's in the lexical scope(能使用sandwich,因为他在词法作用域中)
        console.log(sandwich);

        // Will log `chips` because it's in the local scope(将会打印chips因为在局部作用域中)
        var snack = 'chips';
        console.log(snack);

    };
    logSandwich();

    // Will also log `tuna` in the console(将会打印tuna)
    console.log(sandwich);

    // returns "Uncaught ReferenceError: snack is not defined"(返回‘Uncaught ReferenceError: snack is not defined’)
    // `snack` is local to the logSandwich() function, and out of the lexical scope(snack在logSandwich()函数中处于局部作用域,在这儿处于词法作用域)
    console.log(snack);

};
sandwiches();
复制代码

完!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值