3

Trying to write some JavaScript (still learning it)
Why does the fucking

(function() {

})();

not work for starting a function below it?
Or above... I tried to fiddle with it too much, I'm just gonna leave it for now

Comments
  • 3
    Thats an anonymous self executing function. This might help: http://esbueno.noahstokes.com/post/...
  • 0
    @practiseSafeHex I skipped over it a bit, seems like that's gonna be really helpful
    Thanks a lot ;D

    Let's see if I can get my code to start working again
  • 0
    Wait, the whole fucking code should be inside of that function?
    *goes back to reading*
  • 2
    @aile11 ha yes. For this reason, this is not my preferred way of using Javascript.
  • 0
    @practiseSafeHex ooh, so what would be your recommended way of doing it?
  • 3
    @aile11 it depends what it is you are actually trying to do. Could use a window.onload event for example.

    But I don't know the context. All I can say is, when ever I see code online doing that, the first thing I have to do is take it out of the anon function so that I can integrate it with my code.

    It's a horrible way to do things as it's very anti-collaboration and frustrating to deal with
  • 0
    @practiseSafeHex the only thing it should do is execute my code, I guess I'll use window.onload() then
    Thank you for your tips o/
  • 0
    Maybe read Addy Osmani's ebook about JS patterns. You can read it for free online.
  • 0
    Guys and girls, I'm proud, so far,no one sugested to use jQuery (pretty sure on SO there would be a bunch of use this jQuery on load or on ready function stuff)
  • 0
    @pascalwacker sorry, but I'm not using jquery atm
    Maybe I'll get into it in the future, but not for now
  • 0
    Well.. Js sucks a bit
  • 0
    @jakobev everybody has their own preferences
    Why do you think it sucks?
  • 0
    @aile11 wasn't sugesting to use it ;) it should have been a sarcastic comment about every one on SO telling every one with little expirience, to use it (even if it is completly unnecessary)
  • 0
    Why should you use self-executing anonymous functions anyway?
  • 0
    You can write your functions below the self executed one and just call the functions inside it
  • 0
    @pascalwacker if it's unnecessary, why use it? ;)

    @osmarks well, I saw it somewhere, thought it was a good way to do it

    @svidlak that's what I tried to do, this is a rant about it not working :P
  • 0
    @aile11 But what is the point of wrapping code in them? Is it a stylistic thing?
  • 0
    @osmarks I'm still learning, so better ask others ;D
    But I guess it's mainly for side functions which may not get interfered by another function
Add Comment