Use static variable in function ()

I would know if we can declare a static var in a function, how can this be done in JavaScript.

When I call my function, my variable retains its last influence.

Or can I only use a global variable (it's not sexy ...)?

+4
source share
3 answers

You cannot use static in a function.

Global variables in Dart are not code smells because they are only library global.
JavaScript global variables are ugly because they can conflict with global variables from third-party libraries.
This does not happen in Darth.

Dart , (, ), - ,

import 'my_globals.dart' as gl;

print(gl.myGlobalValue);

.

,

class MyGlobals {
  static myVal = 12345;
}

Dart , .

+4

:

library test;

class Test implements Function {
  var status = 0;
  static var static_status = 10;

  call() {
    print('Status: $status');
    print('Static status: $static_status');
    status++;
    static_status++;
  }
}

void main() {
  var fun = new Test();

  fun();
  fun();
  fun();

  var fun2 = new Test();

  fun2();
  fun2();
  fun2();
}

:

Status: 0
Static status: 10
Status: 1
Static status: 11
Status: 2
Static status: 12
Status: 0
Static status: 13
Status: 1
Static status: 14
Status: 2
Static status: 15
+3

.

"mangling".

void main() {
  myFunction();
  myFunction();
  myFunction();
}

int _myFunction$count = 0;

void myFunction() {
  print(_myFunction$count++);
}

, "_myFunction $count" - "count" "myFunction".

, .

void myFunction() {
  static int count = 0;
  print(count++);
}
+1
source

Source: https://habr.com/ru/post/1534142/


All Articles