[Learning] Dart - anonymous method of Dart & Closures

First, the anonymous method definition

(Parameter 1, parameter 2, ....) {

    Method body ... 
return Return value }

Second, the anonymous method properties

  • It can be assigned to variables, called by variable
  • Can be called directly transmitted to the other methods or other methods,
     
       

    main void (List <String> args) {
     // No anonymous function parameters
     var FUNC = () {
       Print ( "the Hello");
      };
     FUNC ();

     // anonymous function has parameters
     var = func1 (STR) {
       Print ( "------- $ STR the Hello");
     };
     func1 ( "huangxiaoguo");

     
       

     // by () call, not recommended
      (() {
        Print ( "called by (), are not recommended");
      }) ();

     
       

     @ Anonymous method parameter passing
      var List2 = [ 'H', 'E', 'L', 'L', 'O'];
      Print (listTimes (List2, (STR) {
        return STR *. 3;
      }));
    }

     
       

     List listTimes(List list, String times(str)) {
       for (var i = 0; i < list.length; i++) {
        list[i] = times(list[i]);
       }
       return list;
     }

    Print Results:

    Hello
    The Hello ------- huangxiaoguo
    Called by () is not recommended
    [hhh, eee, lll, lll, ooo]
    Exited

Third, what is closure? Advantages and disadvantages of closing package?

  • 1, variable scope

    To understand the closures, we must first understand the special variable scope of Dart.

    Scoped variables nothing more than two types: global and local variables.

    Dart language so special is that: internal function Global variables can be read directly, but can not read the local variables inside a function outside the function.

  • 2, how to read a local variable inside a function from the outside?

     (1) For a variety of reasons, sometimes we need to get to a local variable inside the function. But already it said, under normal circumstances, it is impossible! It can only be achieved through alternative methods.

    That is, inside the function, and then define a function.

    a() {
      int count = 0;
      printCount() {
        print(count++);
      }
      return printCount;
    }
    
    b() {
      int count = 10;
      return () { print(--count); }; }

     (2) In the above code, the function printCount are included in the function of a interior, then a all local variables inside of printCount are visible. But the reverse is not, PrintCount local variable internal to a is not visible.

    That's Dart language-specific " chain scope " structure (chain scope),

     (3) the child objects will look for an upward all the variables of a parent object to. So, all variables parent object, child objects are visible, not vice versa.

    Since printCount can read a local variable, then as long as the printCount as a return value, we would not be in a read its internal variables outside yet!

    void main(List<String> args) {
      var func = a();
      for (var i = 0; i < 5; i++) {
        func();
      }
    print(
    "-------------------------");
    var func1 = b(); for (var i = 0; i < 5; i++) { func1 (); } } a() { int count = 0; printCount() { print(count++); } return printCount; } b() { int count = 10; return () { print(--count); }; }

     

  • 3, the concept of closures

    The above code in a printCount function and  b anonymous function is closure .

    A variety of professional literature closure definitions are very abstract, I understand that:  the closure is able to read the other functions of the internal variable function .

    Since Dart, only the internal function subroutine to read the local variables, so that the closure can be simply understood as a " function defined inside a function ."

    Therefore, in essence, the closure internal bridge function and connecting external functions .

  •  4, the use of closures

    Closures can be used in many places. It's most useful for two, one is the aforementioned variables can be read inside the function, and the other is to make the values of these variables remain in memory, not in A () , b () call automatically after Clear.

    Why is this so? The reason is that a is printCount the parent function, and printCount is assigned to a global variable, which leads printCount always in memory, and printCount existence depends on a , thus a always in memory, and not after the end of the call , the garbage collection (garbage collection) recovered.

  • 5, the use of closures Precautions

    (1) Due to the closure will make the function of the variables are stored in memory, memory consumption is large, it can not be abused closure, otherwise it will cause performance problems webpage in IE may lead to memory leaks. The solution is, before exiting the function will delete all local variables are not used.

    (2) closure will function outside the parent, the parent function changes the value of the internal variable. So, if you take the parent function as an object (object) to use, the closure of its public methods (Public Method) as, the internal variable as its private property (private value), then you must be careful not to just change the value of the parent function of internal variables.

 

Guess you like

Origin www.cnblogs.com/lxlx1798/p/11038986.html