Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

javaScript Closure not working

Posted on 2014-10-02
6
92 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)

789 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