www.webdeveloper.com
Results 1 to 10 of 10

Thread: "What does the exclamation mark do before the function?"

  1. #1
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,402

    Question "What does the exclamation mark do before the function?"

    i'm in a web development class. it's for my major, but i've been doing this for years so very little is earth shattering for me. as the teacher was explaining embedding from a very beginner standpoint, i saw something that confuses the hell out of me. it's the way twitter uses javascript to allow a user to embed their widgets on webpages. this is that script:

    Code:
    <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
    everything between the curly braces {} i get. no problem got it.

    it's everything else that confuses me. my first search led me to stackoverflow, which is why i titled my thread like i did:

    http://stackoverflow.com/questions/3...e-the-function

    okay, got it. the exclamation point makes the statement an expression. cool great.

    but i guess that makes me confused about this:

    Code:
    (function(){})();
    or i guess that's the same as:

    Code:
    !function {}()
    so my ultimate question, especially if you can answer it in the context of the twitter code i provided, is what does the second set of parenthesis () do?? how does that modify the function or help the page or whatever??

    1. If you reply to my post, and your reply would then appear directly beneath my post, DON'T QUOTE MY ENTIRE POST!!! IT'S REDUNTANT!!! IT'S ASININE!!!! IT'S REDUNDANTLY ASININE!!!!! DON'T DO IT!!!!
    2. jQuery extends the functionality of JavaScript. If you don't know JavaScript, give up on that jQuery script and learn JavaScript. You'll save yourself a lot of frustration, I promise.
    3. Use the [code][/code] tags. Otherwise, you may be left wondering why no one responded to your eyesore of a thread.


  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,366
    ! = not, so if your function !function(blah){return true;}() will return a true value from the function and ! will make it false.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,402
    i see by your response you didn't bother to read my entire post.

    so to keep this short, read the stackoverflow link i posted. if you had read my post, you would have seen that i'm not assigning the return of a function to any value. i also asked what the second set of parenthesis means. you didn't even bother to answer that.

    please do something other than read the title of the thread before you respond in the future!

    1. If you reply to my post, and your reply would then appear directly beneath my post, DON'T QUOTE MY ENTIRE POST!!! IT'S REDUNTANT!!! IT'S ASININE!!!! IT'S REDUNDANTLY ASININE!!!!! DON'T DO IT!!!!
    2. jQuery extends the functionality of JavaScript. If you don't know JavaScript, give up on that jQuery script and learn JavaScript. You'll save yourself a lot of frustration, I promise.
    3. Use the [code][/code] tags. Otherwise, you may be left wondering why no one responded to your eyesore of a thread.


  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    Code:
    (function(){
       //... instructions
    })();
    This code define an anonymous function and call it immediately. It's useful to work in a namespace without interferences with the global space.
    For example, the following function define sndRqt function to use for Ajax call
    Code:
    (function(){
    	var xmlObj=false;
    	var xmlFct=[function(){return new XMLHttpRequest()}
    		,function(){return new ActiveXObject("Msxml2.XMLHTTP")}
    		,function(){return new ActiveXObject("Msxml3.XMLHTTP")}
    		,function (){return new ActiveXObject("Microsoft.XMLHTTP")}];
    	for (var i=0;i<xmlFct.length;i++) {try{xmlObj = xmlFct[i]();}catch(e){continue;}break;}
    	
    	return sndRqt=function(url,cllbck,pstDta){
    		var req=xmlObj;
    		if (!req) return;
    		var mth=(pstDta)? "POST":"GET";
    		req.open(mth,url,true);
    		req.setRequestHeader('User-Agent','XMLHTTP/1.0');
    		if (pstDta) req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    		req.onreadystatechange=function(){
    			if (req.readyState!=4) return;
    			if (req.status!=200 && req.status!=304) return;
    			cllbck(req);}
    		if (req.readyState==4) return;
    		req.send(pstDta);
    	};
    }());
    Then an expression like sndRqt('myfile.php?val='+encodeURIComponent(myvalue),function(r){alert(r.responseText)]) makes an Ajax GET request.
    Last edited by 007Julien; 02-25-2014 at 09:43 AM.

  5. #5
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,402
    julien, that definitely agrees with my initial assessment:

    Quote Originally Posted by Angry Black Man View Post
    okay, got it. the exclamation point makes the statement an expression. cool great.
    but you still didn't answer my question:

    Quote Originally Posted by Angry Black Man View Post
    so my ultimate question, especially if you can answer it in the context of the twitter code i provided, is what does the second set of parenthesis () do?? how does that modify the function or help the page or whatever??

    1. If you reply to my post, and your reply would then appear directly beneath my post, DON'T QUOTE MY ENTIRE POST!!! IT'S REDUNTANT!!! IT'S ASININE!!!! IT'S REDUNDANTLY ASININE!!!!! DON'T DO IT!!!!
    2. jQuery extends the functionality of JavaScript. If you don't know JavaScript, give up on that jQuery script and learn JavaScript. You'll save yourself a lot of frustration, I promise.
    3. Use the [code][/code] tags. Otherwise, you may be left wondering why no one responded to your eyesore of a thread.


  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,391
    Just to butt in with a question. What happens when you leave the second set of parenthesis out? Does it still work or does it die?

  7. #7
    Join Date
    Oct 2013
    Posts
    517
    I'm not an expert on JS by any means and I'm learning this along with you ABM

    While looking through the original code you provide a lightbulb went off in my head. I've worked with this before. Not exactly, but kind of. Let's say you want to pass the IDs of 3 elements to a named function and turn them into variables for use within that function. Here's what you do:

    HTML Code:
    onclick="blah(id1,id2,id3)"
    The event can be anything: onblur, onfocus, onload, etc.

    The JS function will look something similar to:
    Code:
    function blah(a,b,c){
    // do your stuff here. var a now equals document.getElementById("id1"), etc.
    }
    This looks to be a mechanism to do the same within an unnamed function. If you take out all the stuff in the braces in your twitter example it would look like this:
    Code:
    !function(d,s,id){}(document,"script","twitter-wjs");
    So var d now equals document, s equals "script" and id equals "twitter-wjs".
    If there's no IDs or variables to work with, I guess you leave both sets of parentheses blank as in ().

    Take my uneducated WAG for what it's worth....

  8. #8
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,402
    kevin, bro, that's fecking money! nice work! wish there were "thanks" buttons!!

    case closed!

    1. If you reply to my post, and your reply would then appear directly beneath my post, DON'T QUOTE MY ENTIRE POST!!! IT'S REDUNTANT!!! IT'S ASININE!!!! IT'S REDUNDANTLY ASININE!!!!! DON'T DO IT!!!!
    2. jQuery extends the functionality of JavaScript. If you don't know JavaScript, give up on that jQuery script and learn JavaScript. You'll save yourself a lot of frustration, I promise.
    3. Use the [code][/code] tags. Otherwise, you may be left wondering why no one responded to your eyesore of a thread.


  9. #9
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,402
    Quote Originally Posted by JMRKER View Post
    Just to butt in with a question. What happens when you leave the second set of parenthesis out? Does it still work or does it die?
    i imagine it wouldn't work, assuming that kevin is right (and i have a lot of confidence he is)

    edit--

    Code:
    !function(str){alert(str)}("hello world")
    works as expected

    Code:
    !function(str){alert(str)}()
    // undefined

    Code:
    !function(str){try{alert(str)}catch(e){alert(e)}}
    // returns false, no error relating to alert strangely
    Last edited by Angry Black Man; 02-25-2014 at 11:12 AM.

    1. If you reply to my post, and your reply would then appear directly beneath my post, DON'T QUOTE MY ENTIRE POST!!! IT'S REDUNTANT!!! IT'S ASININE!!!! IT'S REDUNDANTLY ASININE!!!!! DON'T DO IT!!!!
    2. jQuery extends the functionality of JavaScript. If you don't know JavaScript, give up on that jQuery script and learn JavaScript. You'll save yourself a lot of frustration, I promise.
    3. Use the [code][/code] tags. Otherwise, you may be left wondering why no one responded to your eyesore of a thread.


  10. #10
    Join Date
    Oct 2013
    Posts
    517
    Quote Originally Posted by Angry Black Man View Post
    wish there were "thanks" buttons!!
    Close enough! You're welcome! Glad my guess wasn't as "WA" as I originally thought.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles