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 9 of 9
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts

    Immediately-invoked function expression in the middle of code

    I'm working on a mini jQuery plugin for a site, and realizing I need to make a private function for it, I found on the jQuery site that I should wrap it in an immediately-invoked function expression to create a namespace. I did so, and jQuery threw a fit. Not sure what was wrong, I moved the code to a separate file and jQuery didn't care anymore. Then I removed it back to the original file but at the bottom, then the top, and in neither case did it care. So then I moved it one block up, and one block down, and it didn't care. So I'm at a loss.

    The file is long, but basically, it goes like this:

    Code:
    function func1() {
    }
    
    function func2() {
    }
    
    jQuery.fn.plugin1 = function () {
    }
    
    (function ($) {
    $.fn.plugin2 = function () {
    })(jQuery);
    
    function func3() {
    }
    There are a lot more functions, and plugin2 is what I've added.

    I moved the IIFE above plugin1 and it worked. It seems to be only in its current location does it fail, and I'm not sure why.

    Code:
    function func1() {
    }
    
    function func2() {
    }
    
    (function ($) {
    $.fn.plugin2 = function () {
    })(jQuery);
    
    jQuery.fn.plugin1 = function () {
    }
    
    function func3() {
    }
    This works.

    Does anyone have any advice?

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    What is the actual problem? Also, please try to adapt your example to become a SSCCE

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Thanks for the link Airblader. I've often wondered about means of converting code into something manageable for forums.

    That being said, its less of a problem and more of "why is it happening?". In my first example, the code doesn't work. The Firebug console says there's an error in the jquery.min.js file (the library file), and in the second example, there is no problem. I can move the IIFE anywhere else with no problem either. I was curious if there was something about IIFE's or structure or something that would cause code to break in the first scenario but not the second.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Is plugin1 dependent on plugin2?

  • #5
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by Keleth View Post
    I've often wondered about means of converting code into something manageable for forums.
    Because you are describing a problem without saying what the error is and by showing code that does not show the unexpected behavior, even after I explicitly asked you for both of them.

    Imagine being a car mechanic and someone coming into your shop without their car, saying "My car doesn't work". This is about the same you are presenting us here with. There are countless ways to break perfectly fine code, so you're going about this the wrong way. Reproduce the error and then figure out the cause, not take working code and figure out how to break it.

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Nope, both are completely independent.

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Airblader View Post
    Because you are describing a problem without saying what the error is and by showing code that does not show the unexpected behavior, even after I explicitly asked you for both of them.

    Imagine being a car mechanic and someone coming into your shop without their car, saying "My car doesn't work". This is about the same you are presenting us here with. There are countless ways to break perfectly fine code, so you're going about this the wrong way. Reproduce the error and then figure out the cause, not take working code and figure out how to break it.
    I'm sorry if it wasn't clear, but the error happens when I take a IIFE and place it immediately after a jQuery plugin. The error stops when I move the IIFE anywhere else. Its why I put a basic outline of what was happening above.

    I understand its a broad statement, but its also a broad problem; the code itself is irrelevant; the structure seems to be the issue. I've commented out most of my own code in both scenarios presented above, and it still fails.

  • #8
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    What I'm asking you is to tell us what error you are getting. It would at least be a start. It would still be a complete guessing game, but at least a little more informed.

    Edit: And the thing about bugs is that often they aren't what they appear. So even though you might think it's a structural issue, it could very likely not be one. The only way to really know would be to see code that shows the behavior and analyze it. But like I said, even knowing the error is at least a start for a better informed guess.
    Last edited by Airblader; 10-03-2013 at 08:03 PM.

  • #9
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    I'm off now, but since it's a guessing game I just want to give my guess: The error is a "abc has no method xyz" kind of thing and the reason is no true independence between both plugins.


  •  

    Posting Permissions

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