?
Solved

javaScript Closure not working

Posted on 2014-10-02
6
Medium Priority
?
96 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
[X]
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
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

752 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