Does jQuery and JavaScript have different namespaces?

I have this code in jQuery ..

$(document).ready(function(){ var fieldCounter = 0; ... 

I have a jQuery function that increments this value.

This works, but I can’t access this value on the page from a non-jQuery function? The converse is also true if I use it in JavaScript, for example.

 <script type="text/javascript"> var fieldCounter = 0; 

I can access it from javascript, but jQuery cannot view it?

Am I probably doing something really dumb?

+4
source share
6 answers

This has nothing to do with jQuery, but all with the Javascript scope.

 $(document).ready(function() { var fieldCounter = 0; }); 

fieldCounter declared inside a function . Since Javascript has the scope of the function, the variable is not visible outside the function.

BTW, jQuery is Javascript, they play by the same rules, they are not two different technologies.

Comprehensive answers can be found here: What is the scope of variables in JavaScript?

+8
source

jQuery is not magic. This is a JavaScript library. Your problem is that you are defining a local variable inside the function. Due to the way the lexical realm of JavaScript works, you cannot access it outside this function (except for closures, which is not applicable here).

Most likely you just want:

 $(document).ready(function(){ fieldCounter = 0; 

This will make a global variable.

Edit: Note that using a namespace and / or declaring a global variable is cleaner, but not required.

+2
source

Your problem in the first case is the area. By placing var init inside a function declaration, you checked it for access inside that function.

Something else happens in the second case; it will take more code to see that.

Global scope in Javascript window . This means that when you declare variables directly in the <script> tags, you can return them by requesting window.variableName .

A common way to solve such problems is to create a namespace structure. If you do it right, you can call myNamespace.subNamespace.variable and have full confidence that since it is explicitly bound to a window , you can return it no matter where you are.

Remember that jQuery is built into Javascript. This is nothing special.

+1
source

JavaScript has a scope.

 var count = 8; var myfunction = function() { var newCount = count + 1; }; alert(newCount); // undefined 
0
source

due to javascript volume ... try reading this

0
source

Variables declared inside a jQuery code block will have a local scope. If you need to access a variable in both the local javascript function and the jQuery block, then declare the variable globally. Example code snippet: -

  <script type="text/javascript" language="javascript"> var increment = 0; $(document).ready(function() { $("#button2").click(function() { increment = increment + 1; alert(increment); }); }); function ClickMe() { increment = increment + 1; alert(increment); } </script> 
0
source

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


All Articles