Why does global variable stay undefined even after assigning value to it in a local scope in javascript

Snedden27

My understanding of the closure property is that every variable inside in a function scope has access to all the variables in the parent scope(s) the function is in,

So considering this definition I don't understand the behavior of my code below:

var mouseX, mouseY;

window.onload = function() {

    this.addEventListener('mousemove', function() {
        mouseX = event.clientX;
        mouseY = event.clientY
    }); // mouseX and mouseY are defined

    petObj = new Pets();


   }

function Pets(){
document.getElementById('imageList').addEventListener('mouseenter',function()
                                       {
                                       console.log(mouseX)} //undefined mouse X!!!
                                       }

I accept the assignation of mouseX inside the anonymous function for mousemove event listener to reference the global variable declared outside the function. But as you can see it stays undefined outside the scope of the anonymous function

Snedden27

I changed the name of my variable from mouseX to mouseXX and it works,not sure why it worked ,maybe the jquery namespace was messing with the variable name

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why Javascript code prints 'undefined' even when variable is declared global?

From Dev

Variable: local scope, global scope or is it the JavaScript engine?

From Dev

javascript scope : retain global variable value after a function

From Dev

why does a variable in javascript seem to hold the value "undefined" even when it was defined

From Dev

In JavaScript, why does this global variable become undefined in my function?

From Dev

Assigning value to global variable

From Dev

JavaScript global dynamic variable undefined from function to function, seems to be local scope

From Dev

Why is my JavaScript hoisted local variable returning undefined but the hoisted global variable is returning blank?

From Dev

Local function variable change not effecting variable in global scope. Why not?

From Dev

why is global variable not accessible even if local variable is defined later in code

From Dev

global scope and local scope in javascript

From Dev

Why is function not changing global variable even after specifying global

From Dev

is local variable stay in memory in javascript?

From Dev

Why does printing a variable in global scope work, but modifying it does not?

From Dev

Why does printing a variable in global scope work, but modifying it does not?

From Dev

Javascript: While the variable is declared in global scope, it remains undefined inside the function

From Dev

Why does Array.sort discriminate NaN vs undefined even after coercing the value in the comparator?

From Dev

Why does not global variable hold value

From Dev

Why does not global variable hold value

From Dev

define variable of global scope in local scope

From Dev

Why does a lambda return a global variable and not the local variable?

From Dev

Javascript Global vs Local Scope

From Dev

local variable to global javascript

From Dev

Using a local variable outside of its declaring scope; why does this work?

From Dev

Why undefined global variable throws exception but accessing via window does not?

From Dev

Why does eclipse say Local variable is not used even though it is used?

From Dev

Why a function in javascript does not accept undefined variable?

From Dev

Why a function in javascript does not accept undefined variable?

From Dev

JavaScript scope issue with global variable

Related Related

  1. 1

    Why Javascript code prints 'undefined' even when variable is declared global?

  2. 2

    Variable: local scope, global scope or is it the JavaScript engine?

  3. 3

    javascript scope : retain global variable value after a function

  4. 4

    why does a variable in javascript seem to hold the value "undefined" even when it was defined

  5. 5

    In JavaScript, why does this global variable become undefined in my function?

  6. 6

    Assigning value to global variable

  7. 7

    JavaScript global dynamic variable undefined from function to function, seems to be local scope

  8. 8

    Why is my JavaScript hoisted local variable returning undefined but the hoisted global variable is returning blank?

  9. 9

    Local function variable change not effecting variable in global scope. Why not?

  10. 10

    why is global variable not accessible even if local variable is defined later in code

  11. 11

    global scope and local scope in javascript

  12. 12

    Why is function not changing global variable even after specifying global

  13. 13

    is local variable stay in memory in javascript?

  14. 14

    Why does printing a variable in global scope work, but modifying it does not?

  15. 15

    Why does printing a variable in global scope work, but modifying it does not?

  16. 16

    Javascript: While the variable is declared in global scope, it remains undefined inside the function

  17. 17

    Why does Array.sort discriminate NaN vs undefined even after coercing the value in the comparator?

  18. 18

    Why does not global variable hold value

  19. 19

    Why does not global variable hold value

  20. 20

    define variable of global scope in local scope

  21. 21

    Why does a lambda return a global variable and not the local variable?

  22. 22

    Javascript Global vs Local Scope

  23. 23

    local variable to global javascript

  24. 24

    Using a local variable outside of its declaring scope; why does this work?

  25. 25

    Why undefined global variable throws exception but accessing via window does not?

  26. 26

    Why does eclipse say Local variable is not used even though it is used?

  27. 27

    Why a function in javascript does not accept undefined variable?

  28. 28

    Why a function in javascript does not accept undefined variable?

  29. 29

    JavaScript scope issue with global variable

HotTag

Archive