Solved

javaScript Closure not working

Posted on 2014-10-02
6
94 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
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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)

717 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