Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Return function return value

    I need to return a value that I get from a closure from a function. For example, this works:

    Code:
       var get_tag_by_name = function(name) {
          var obj;
          $('.tagit-choice').each(function(i) {
             if($(this).children('.tagit-label').text() == name) {
                obj = this;
             }
          });
          return obj;
       };
    But this doesn't:

    Code:
       var get_tag_by_name = function(name) {
          $('.tagit-choice').each(function(i) {
             if($(this).children('.tagit-label').text() == name) {
                return this;
             }
          });
       };
    Because "this" gets returned to.. nothing. I need to return the value from the closure from my "get_tag_by_name" function.

    Any ideas?

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    You're right, you can't do it that way. The easiest way is probably to just filter the elements, like this:

    PHP Code:
    var get_tag_by_name = function (name) {
        return $(
    '.tagit-choice').filter(function () {
            return $(
    '.tagit-label'this).text() == name;
        }).
    get(0);
    }; 
    By the way, there are no closures in your code.
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • Users who have thanked venegal for this post:

    dwieeb (12-22-2011)

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah, thanks. I learned 4 new things from that snippet you gave. Thanks!

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,335
    Thanks
    13
    Thanked 348 Times in 344 Posts
    the first code does contain a closure, through obj.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by Dormilich View Post
    the first code does contain a closure, through obj.
    It's usually only called a closure, if the inner function outlives the outer function (which isn't the case here). Otherwise, you could call every function a closure, just because it has access to the global scope.
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,335
    Thanks
    13
    Thanked 348 Times in 344 Posts
    good point
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    XD

    We were both wrong, I thought closures = anonymous functions.


  •  

    Posting Permissions

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