Javascript is a broad language because you can do almost all the things you want on a web page, just by creating functions and finding ways to do.
I can say that Javascript is not a safe language, because you can easily access most of the variables and functions, read and know how it works, just accessing the *.js file, included in the page.
My Thought: Some access modifiers have been created to use in Javascript due to needs, because Javascript does not "transport" to other places (pages), unless you use a session variable.
And about that, some known modifiers:
private
protected
public
I can tell you that I know that some javascript modifiers have some resemblance to them, which are:
Local:
var Variavel = 0;
Automatically, it is converted to an Integer(Integer) variable, because it is receiving an Integer(Integer) value, and also, this is a local variable because of the modifier var
that declares this variable in a way that you cannot access its value unless you are within the same function that this variable was declared.
Example:
If you declare these functions this way, with standard modifiers:
function conflito(){
i = 2;
mudarI();
alert(i);
}
function mudarI(){
i = 0;
}
In this case, the i
is the same variable for both functions.
So if you run conflito()
, you will receive an alert that results 0
.
BUT, IF YOU DECLARE i
using the modifier var
:
function conflito(){
var i = 2;
mudarI();
alert(i);
}
function mudarI(){
var i = 0;
}
In this case, you have two variables i
, because they are restricted to use only within their function, so if you run conflito();
now, you will receive an alert with value of 2
.
Variable belonging to the Method:
this.Variable = "a";
This variable is automatically a String, because it is receiving a String value, you probably already know what this modifier does, but, I will try to explain with my point of view, that is, this variable is coming from Superclass ie, which can be called a class , or in other words, the class "parent".
An example:
function TClass()
{
this.getVar = function()
{
try
{
return "test";
}
catch(err)
{
return false;
}
}
this.alertVar = function()
{
try
{
alert(this.getVar());
}
catch(err)
{
alert('erro');
}
}
}
var $Class = new TClass();
As you can see above, I created a class TClass
and some variables that contain functions within (javascript closure) and added the modifier to them. To make them linked to TClass
and, as you see in the function alertVar()
, run out alert(this.getVar())
, it performs the function that is TClass
in this context.
And this part:
var $Class = new TClass();
I’m creating the class as you probably already know, to have access to your methods, making it possible to run, to test:
$Class.alertVar();
Resulting an Alert containing "test", as you can see :
Note that you cannot access the methods TClass
otherwise, you can only access it if you create the class instance and access it from it.
I hope you have understood the usability of this modifier.
Global Variables:
window.Variable = true;
Javascript automatically declares this variable as a Boolean, because it is receiving a boolean value. The modifier window
, as it says itself, you can access it at the moment you are in the window(window), because Javascript variables when declared, they go to the DOM of the window, see what is DOM:
DOM(Document Object Model): DOM is a cross-platform that represents the way the HTML, XHTML, and XML markups (Markup’s) are organized by the browser you are using. In other words, if you access the DOM you can see each Property, all variables , or anything that is currently declared in the browser.
Unlike other variables , the window variables may have assigned another value and access the current value, no matter where you are accessing, within a function or not, within a *.js file or not.
Example of Global(window):
Perform in the event onLoad
from a page a code declaring a window variable(window), or declaring using the same browser console:
window.Variable = true;
Then add a JS file that contains this function, or just declare by executing the code in the browser console:
function testGlobalVar(){
if (Variable)
alert("funciona!");
}
When you perform testGlobalVar()
you will receive the alert, but it is only because you declared it as 'window()' otherwise it will not run.
Standard modifier for Javascript:
Variavel = 0.5;
This variable is automatically declared as Float because it is receiving a Float value. I don’t know if you already know, but javascript variables declared as usual (no modifiers), have a pattern modifier that makes the variable look similar to window variables , but you can’t access it from anywhere you are, but in most cases, you can access it, particularly, I don’t know all the cases you can’t access it, but I know you can’t when you uploaded a *.js file and was declared inside it. Only if you perform a function that declares it, so it goes to the DOM and after that try to access.
However, I see you want to know about the var
in question, but I gave you a general explanation of all the modifiers I know (I believe they are all), for your knowledge get wider and you have a better understanding of the modifier in question.
I hope you understand what I’m saying .
Ah, and if you got confused when you saw a function within a variable, study Javascript Closures, you will understand after a while :).
Not a silly question, on the contrary. In fact, considering how often we see "polluted" Javascript code with global statements of variables, it’s a very relevant question!
– rsenna
@Maniero what would be his criterion used to determine that this question did not get enough attention?
– Costamilam
@Guilhermecostamilam My strictly personal. Whoever is giving the reward can do it anywhere. It’s not a requirement to have a reason to spend your reputation to highlight something you think you should.
– Maniero
@Maniero I just wondered, had already seen you offer rewards to reward an existing answer. I didn’t understand the reason of this, I thought I suddenly wanted some updated answer with
const
andlet
, but I wasn’t sure– Costamilam
@Guilhermecostamilam this is about the
var
very user not yet use.– Maniero
Next for being 'var' when the code is compiled, it has read preference.. Var hi = 0; it will be interpreted first than, oi = 1;
– Willian