SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ! before function

    Hi,
    peeping into
    http://twitter.github.com/bootstrap/javascript.html
    code I've noticed this
    PHP Code:
    !function ($) {
    }(
    window.jQuery); 
    I don't understand what's the use of ! before function
    Can you explain me, please ?

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,332
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    That preceeding "bang" will cast the result of the anonymous function as a boolean.

    Then you can test for TRUE || FALSE on the execution of the anonymous function.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,
    but in which case is it useful ?
    Can you give me a practical example, please ?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by whisher View Post
    Thanks,
    but in which case is it useful ?
    Can you give me a practical example, please ?
    Sure thing.

    If something is being validated, and you need to display an error if it's not validated, you could use the NOT operator like this:

    Code javascript:
    var form = document.getElementById('orderInfo');
    var isValid = validate(form);
     
    if (!isValid) {
        // show an error message here
    }

    Sometimes it makes better sense to check things that way. If the NOT operator is not used, you can end up with a useless "do nothing" piece before you get to what actually needs to be done.

    Code javascript:
    ...
    if (isValid) {
        // do nothing
    } else {
        // show error
    }

    And sometimes, even if you do something when things are valid, it can make better sense to do the non-matching process first before going on to potentially more complex things for when it does match:

    Code javascript:
    ...
    if (!isValid) {
        // show error
    } else {
        // do lots of other things
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Paul I just knew the NOT operator
    but I don't see the point using it like
    PHP Code:
    !function ($) { 
    }(
    window.jQuery); 
    in bootstrap.js file
    https://github.com/twitter/bootstrap...s/bootstrap.js
    there a list of !function and I don't see why ?

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by whisher View Post
    Thanks Paul I just knew the NOT operator
    but I don't see the point using it like
    PHP Code:
    !function ($) { 
    }(
    window.jQuery); 
    in bootstrap.js file
    Normally it is parenthesis that are used there instead, to create a function expression.

    Code javascript:
    (function ($) {
    }(window.jQuery));

    I am not in favour of using ! instead of () though for shaving off just one character though, when it results in code that raises more questions.
    Last edited by paul_wilkins; Jun 16, 2012 at 08:46.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    I am not in favour of using ! instead of () though for shaving off just one character though, when it results in code that raises more questions.
    I agree - the ! is just converting the presumably undefined value returned from the function to false and then applying the not to it to make it true and then discarding it. To me that sounds like more actual processing to be performed than simply wrapping the function inside (). After all the only requirement is that the word function have something in front of it in order to identify that you are defining an anonymous function so that JavaScript doesn't give an error due to there not being a name for the function in the middle of the function(). Performing extra processing in order to make the source code one character shorter makes no sense.




    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •