SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to set a flag using a closure

    Hi guys, I would like to set a flag using a closure so that the internal value of that closure is preserved for me to retrieve it when different events occur. It is one of those times where lack of block scope will stop me from doing what I want to do unless I can set a boolean and preserve its value in a function.

    Can someone suggest the best way of doing this?

    thanks

    Silversurfer
    "Persistence is the path to perfection"

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Code:
    var foo = (function() {
    	var called = 0;
    	return function() {
    		return ++called;
    	};
    })();
    
    alert(foo()); // 1
    alert(foo()); // 2
    alert(foo()); // 3
    The only code I hate more than my own is everyone else's.

  3. #3
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thats great thanks very much, I can now do things like:
    Code:
     if( foo() > 0)
    {
    // do something 
    }
    IS there any way of passing a boolean into the closure in the first place to update that value (true or false) every time you call it?
    "Persistence is the path to perfection"

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Code:
    var foo = (function(val) {
    	return function() {
    		return val = !val;
    	};
    })(false);
    
    alert(foo()); // true
    alert(foo()); // false
    alert(foo()); // true
    The only code I hate more than my own is everyone else's.

  5. #5
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much oddz, I was a C++ developer and I am having some problems adjusting to no block scope and using closure instead of classes and private member variables, this has solved it! Have a great weekend
    "Persistence is the path to perfection"

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Yeah, it is pretty cool when you get the hang of it in comparison to other languages. Closures are probably one of my favorite features of JavaScript.
    The only code I hate more than my own is everyone else's.

  7. #7
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there I don't seem to be able to get this function to return correctly, I know it returns true then false alternately but I can't pass boolean parameters in ,

    if I do:

    foo(true); // true
    foo(true); //false
    foo(true); //true
    foo(true); //false

    Confused?
    "Persistence is the path to perfection"

  8. #8
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    PHP Code:
    var foo = (function(val) {
            
    alert(val);
            return function() {
            return 
    val = !val;
        };
    })(
    false); 
    the outer function is just triggered before you call foo
    so you can't pass the parameter.

  9. #9
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok That seems ok to understand but what do you suggest as an alternative? I want to retain a value from outside a function, shall I just go back to creating it as an attribute of an object?
    "Persistence is the path to perfection"

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Code:
    var foo = (function() {
            var val;
    	return function(newval) {
    		return val = newval;
    	};
    })();
    The only code I hate more than my own is everyone else's.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •