Solved

javaScript Closure not working

Posted on 2014-10-02
6
88 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)

707 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

15 Experts available now in Live!

Get 1:1 Help Now