Solved

javaScript Closure not working

Posted on 2014-10-02
6
90 Views
Last Modified: 2014-10-02
Hi All,

Please see image below.   How come line 13 does not produce a value?
closue
Here's the code:
<html>
  <head>
    <title></title>
    <script>
(function()
{
   var myCnt =3;
console.log("iife val:"+myCnt);
 return function keepCount() {return myCnt;}
}
)();
console.log("hello");
console.log("outside of iife: "+myCnt);

var myAry = new Array();
myAry.push("one");
myAry.push("two");
myAry.push("three");
myAry.push("four");

if (myCnt==myAry.length)
{
  var cnt = myAry.length;
}
else
{
  var cnt = myCnt;
}

for(i=0; i<cnt; i++)
{
  console.log(myAry[i]);
}

getAry(myAry);

function getAry(param)
{
   console.log(param.length);
console.log(param[0]);
}
</script>
  </head>
<body>
</body>
</html>

Open in new window


Thanks for any light you can shed on this.
0
Comment
Question by:Isaac
  • 3
  • 3
6 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40358472
myCnt is only declared inside the function so it doesn't exist outside it.

<script>
var myCnt
(function()
{
   myCnt =3;
console.log("iife val:"+myCnt);
 return function keepCount() {return myCnt;}
}
...
...

Open in new window

0
 
LVL 5

Author Comment

by:Isaac
ID: 40358480
I pasted the wrong code.

I had the function set to a variable:

<html>
  <head>
    <title></title>
    <script>
var cnt =(function()
{
   var myCnt =3;
console.log("iife val:"+myCnt);
 return function keepCount() {return myCnt;}
}
)();
console.log("hello");
console.log("outside of iife: "+cnt);
var my2ndCnt = cnt;

var myAry = new Array();
myAry.push("one");
myAry.push("two");
myAry.push("three");
myAry.push("four");

if (my2ndCnt==myAry.length)
{
  var cnt = myAry.length;
}
else
{
  var cnt = my2ndCnt;
}

for(i=0; i<cnt; i++)
{
  console.log(myAry[i]);
}

getAry(myAry);

function getAry(param)
{
   console.log(param.length);
console.log(param[0]);
}
</script>
  </head>
<body>
</body>
</html>

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 40358495
Closures go inside other functions, they don't stand on themselves, what would be the point

http://javascriptissexy.com/understand-javascript-closures-with-ease/

I'm not sure I understand what you are trying to
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Author Comment

by:Isaac
ID: 40358504
I thought closures keep a variable alive and allow me to use it outside the function.

I am trying to use whatever myCnt is set to outside the function.

http://www.w3schools.com/js/js_function_closures.asp
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40358510
Call your function with parantheses

console.log("outside of iife: "+cnt());

The function name is redundant - you are calling the outer function name, replace
return function keepCount() {return myCnt;}

with
return function () {return myCnt;}
0
 
LVL 5

Author Closing Comment

by:Isaac
ID: 40358514
Thanks!  That makes sense.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now