What is the difference between a local variable, an instance field, an input parameter, and a class field?

A local variable is defined within the scope of a block. It cannot be used outside of that block. Example: I cannot use local outside of that if block. An instance field, or field, is a variable that’s bound to the object itself. I can use it in the object without the need to use accessors, and any method contained within the object … Read more

Why is my HelloWorld function not declared in this scope?

You need to either declare or define the function before you can use it. Otherwise, it doesn’t know that HelloWorld() exists as a function. Add this before your main function: Alternatively, you can move the definition of HelloWorld() before your main():

When should I use ‘self’ over ‘$this’?

Short Answer Use $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members. Full Answer Here is an example of correct usage of $this and self for non-static and static member variables: Here is an example of incorrect usage of $this and self for non-static and static member variables: Here is an example of polymorphism with $this for member functions: Here … Read more

Don’t understand why UnboundLocalError occurs (closure) [duplicate]

Python doesn’t have variable declarations, so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local.[1] Thus, the line implicitly makes counter local to increment(). Trying to execute this line, though, will try to read the value of … Read more

How to use Global Variables in C#?

In C# you cannot define true global variables (in the sense that they don’t belong to any class). This being said, the simplest approach that I know to mimic this feature consists in using a static class, as follows: You can then retrieve the defined values anywhere in your code (provided it’s part of the same namespace): In order … Read more

Why Is `Export Default Const` invalid?

const is like let, it is a LexicalDeclaration (VariableStatement, Declaration) used to define an identifier in your block. You are trying to mix this with the default keyword, which expects a HoistableDeclaration, ClassDeclaration or AssignmentExpression to follow it. Therefore it is a SyntaxError. If you want to const something you need to provide the identifier and not use default. export by itself accepts a VariableStatement or Declaration to its right. The following is fineexport default Tab; … Read more

Define a global variable in a JavaScript function

As the others have said, you can use var at global scope (outside of all functions and modules) to declare a global variable: (Note that that’s only true at global scope. If that code were in a module — <script type=”module”>…</script> — it wouldn’t be at global scope, so that wouldn’t create a global.) Alternatively: In modern environments, you can assign to … Read more