简体   繁体   English

dart 中的词法 Scope 是什么?

[英]What is the Lexical Scope in dart?

Basically I am going through the definition of closure functions which says -基本上我正在经历闭包函数的定义,它说 -

A function that can be referenced with access to the variables in its lexical scope is called a closure可以通过访问其词法 scope 中的变量来引用的 function 称为闭包

So I want to know this term lexical scope .所以我想知道这个词的词法 scope

Lexical scope词法范围

lexical scoped variable/closure etc can only be accessed within the block of code in which it is defined.词法范围变量/闭包等只能在定义它的代码块内访问。

Dart is a lexically scoped language. Dart 是一种词法作用域语言。 With lexical scoping, descendant scopes will access the most recently declared variable of the same name.使用词法作用域,后代作用域将访问最近声明的同名变量。 The innermost scope is searched first, followed by a search outward through other enclosing scopes.首先搜索最内部的范围,然后通过其他封闭范围向外搜索。

You can “follow the curly braces outwards” to see if a variable is in scope.您可以“按照花括号向外”查看变量是否在范围内。

See the following example.请参阅以下示例。

main() { //a new scope
  String language = "Dart";

  void outer()  {
    //curly bracket opens a child scope with inherited variables

    String level = 'one';
    String example = "scope";

    void inner() { //another child scope with inherited variables
      //the next 'level' variable has priority over previous
      //named variable in the outer scope with the same named identifier
      Map level = {'count': "Two"};
      //prints example: scope, level:two
      print('example: $example, level: $level');
      //inherited from the outermost scope: main
      print('What Language: $language');
    } //end inner scope

    inner();

    //prints example: scope, level:one
    print('example: $example, level: $level');
  } //end outer scope
  outer();
} //end main scope

Lexical closures词法闭包

A closure is a function object that has access to variables in its lexical scope, even when the function is used outside of its original scope.闭包是一个函数对象,它可以访问其词法范围内的变量,即使该函数在其原始范围之外使用。

 /// Returns a function that adds [addBy] to the
/// function's argument.
Function makeAdder(num addBy) {
  return (num i) => addBy + i;
}

void main() {
  // Create a function that adds 2.
  var add2 = makeAdder(2);
    
  // Create a function that adds 4.
  var add4 = makeAdder(4);
    
  assert(add2(3) == 5);
  assert(add4(3) == 7);
}

You can read more from here .您可以从这里阅读更多内容。

Dart is a lexically scoped language, which means that the scope of variables is determined statically, simply by the layout of the code. Dart 是一种词法作用域语言,这意味着变量的作用域是静态确定的,仅由代码的布局决定。 You can “follow the curly braces outwards” to see if a variable is in scope.您可以“沿着花括号向外”查看变量是否在范围内。

Here is an example of nested functions with variables at each scope level:以下是在每个范围级别具有变量的嵌套函数的示例:

String topLevel = 'Hello';

void firstFunction() {
  String secondLevel = 'Hi';
  print(topLevel);
  nestedFunction() {
    String thirdLevel = 'Howdy';
    print(topLevel);
    print(secondLevel);
    innerNestedFunction() {
      print(topLevel);
      print(secondLevel);
      print(thirdLevel);
    }
  }
  print(thirdLeve);
}

void main() => firstFunction();

This is a valid function, until the last print statement.这是一个有效的函数,直到最后一个 print 语句。 The third-level variable is defined outside the scope of the nested function, because scope is limited to its own block or the blocks above it.第三级变量是在嵌套函数的范围之外定义的,因为范围仅限于它自己的块或它上面的块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM