You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
作用域是使用变量或者函数的上下文。有三种类型的作用域:全局作用域、局部作用域和词法作用域。(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();
完!
The text was updated successfully, but these errors were encountered:
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.)
局部作用域(Local Scope)
变量或者函数只能被你代码本身所在的局部作用域中使用(A variable or function that’s only accessible in a part of your code base has local scope.)
词法作用域(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.)
完!
The text was updated successfully, but these errors were encountered: