Global and Local and Private Functions (Javascript)

I am currently reading a book on Javascript from Pragmatic, and I am confused about one thing. They have a section on how to make variables global, local, or private.

  • What is the difference between local and private variables? There is one?

  • How to make a variable global or local, they said something about putting "var =" in front of it, but it was very vague.

+7
source share
5 answers
  • No, people use "private" because they are wrong and should say "local"

  • Local variables are defined as

var foo = "local";

global variables are properties of the object of the global scope (in the browser window )

window.foo = "global";

The fact that you can do foo = "global"; without first assigning the variable foo with var foo is an โ€œerrorโ€. This is fixed in ES5 strict mode.

(function () { "use strict"; foo = 42; })()

gives ReferenceError: foo is not defined

Note that you can make variables global by declaring them in the outer scope

 var foo = "global"; function bar() { var foo = "local"; } 

It should be noted that you should not have any code in the external area. You must wrap your entire scope with anonymous functions to get a โ€œmodule level scopeโ€. This means that you have a file-based top-level area. This is part of the module template.

+8
source

In the browser context, the var keyword binds a variable to the current function.

 var a = 10; var b = function(a) { console.log(a); # 15 } b(15); console.log(a); # 10 

If you do not include the var keyword, it is assigned a window scope and is considered global. If you have no reason to exclude, always include the var keyword.

A variable is considered private if it exists only within the scope of functions. This usually takes the form of an anonymous function. This is not a private variable in the general sense of the word, it is just a local variable.

 (function() { var x = 10; })(); console.log(x); #undefined 
+4
source

What is the difference between local and private variables? Is there one here?

Depends on the context in which they are used. In general, they mean the same thing. From the point of view of OOP, local variables are usually called private.

How to make a variable global or local, They said something about putting "var =" in front of it, but it was very vague.

When you put var in front of a variable, it becomes local variables, but if not, it becomes a global variable. For example:

 var foo = 1; // local foo = 1; // global equivalent to window.foo = 1 becomes part of window object 

A more practical example:

 function myfunc(){ var foo = 1; // presence of var keyword bar = 2; // absence of var keyword } alert(foo); // error eg undefined alert(bar); // 2 because bar is part of window global object 
0
source

Javascript has a scope of functions, any variable defined inside a function with the var keyword is a local function and is not visible from the outside. variables defined in a function without the var keyword are global and are visible everywhere.

 function test(){ var local = 'local'; // this is local global = 'global'; // this is global } test(); // call a function alert(local) // undefined alert(global) // global 
0
source

Private variables make sense only when creating objects. In a typical prototype template, you add any necessary variables and auxiliary functions as properties of the object instance and / or its prototype, but this has the drawback to make them visible to everyone who has access to the object. To avoid this, there is an alternative template in which the local variables of the constructor are variables, and all methods are declared in the constructor area, and only public methods are declared as the actual properties of the object.

0
source

All Articles