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
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts

    "Pop quiz, hot shot"

    (a la Dennis Hopper in "Speed")
    Code:
    var X = {}, result = "PASS"
    X.Exn = function() { result = "FAIL"; }
    (function() {})();
    Without running this code in your browser or other JS engine, what do you think this code will do?

    Hint: I wrote this code, and I got it wrong.
    Last edited by Alex Vincent; 06-07-2012 at 07:08 PM. Reason: code tags
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Um...will it "shoot the hostage?"
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,664
    Thanks
    6
    Thanked 1,006 Times in 979 Posts
    It will do nothing?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by VIPStephan View Post
    It will do nothing?
    Nothing that anyone will see anyway - without more code to actually access the values set.

    It will end up with X defined as an object with an Exn method attached.
    It will end up with a variable called result that contains the value 'PASS' and where if the Exn method were ever to be run that value would be changed to 'FAIL'.

    It is the last line that does absolutely nothing at all since it is a self executing anonymous function with nothing in it to actually run.

    Of course the Exn method isn't run by the code so the value of result doesn't get changed and since there's no code to actually display the values the only way you'd tell if the value were to be changed would be to examine it in the built in debugger that all browsers except Firefox now have (you have to install either the Venkmann or Firebug extension to add a debugger to Firefox equivalent to the ones built into Internet Explorer, Chrome, Safari, and Opera).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts

    Lightbulb

    That's the same logic I made. It's also wrong, because X.Exn does run. Perhaps if I make one little change:

    Code:
    var X = {}, result = "PASS"
    X.Exn = function() { result = "FAIL"; } (function() {})();
    As an expert explained to me, it's saying "execute that first function with the second function as the first argument of that first function. Then execute whatever that returns."

    This has to do with automatic semicolon insertion: there was no semicolon at the end of the second line. So, it tried to execute that function. But ultimately it threw the exception.

    Semicolons are transparently inserted in a lot of cases. This is one where it's not, because we're dealing with the end of a function body and the beginning of an argument list passed into the function.

    My rule of thumb has been "if you have a closing brace, you don't need a semicolon." I may need to change that rule...
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Alex Vincent View Post
    That's the same logic I made. It's also wrong, because X.Exn does run.
    Ah, I overlooked the missing semi-colon on the end of the second line.

    You always need a semi-colon after a } that ends an anonymous JavaScript function - its only when you define named functions that you don't need it (and of course named functions can only be used in very limited circumstances in JavaScript so its easier to always just assign anonymous functions to variables instead since that always works - as long as you don't leave off the semi-colon).

    The only spot where adding a semi-colon after a } would break the code is where the } is followed by else or while so a better rule would be to always insert the semi-colon except if it is immediately followed by one of those two words.
    Last edited by felgall; 06-09-2012 at 01:18 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    New Coder
    Join Date
    Jun 2009
    Posts
    81
    Thanks
    0
    Thanked 8 Times in 8 Posts
    Good observations.
    Unintended consequences can drive you crazy.
    Code:
    <div id="info"></div>
    <script>
    
    var Sheldon = function(n){
    	document.getElementById("info").innerHTML+=n;
    }
    
    ((function(){}).a="Bazinga")()
    
    </script>
    If you wrap an assignment in parenthesis after a function - it becomes plain the statement is actually an argument for the preceding function.

    Code:
    var f = function(n){
    	document.getElementById("info").innerHTML+=n
    }
    
    (a = "hello world" , b=["a second argument",""]);
    Last edited by rdspoons; 06-10-2012 at 02:18 AM.


  •  

    Posting Permissions

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