JavaScript’s Sharp

While JavaScript’s popularity grows daily, it’s not obvious if enterprise developers spoiled by predictable languages like Java or C# will fill comfortable with it. If they won’t – the cost of the JavaScript-based projects will remain high.

Just take a look at this code snippet on the right – I’ve been using Firebug,the Firefox add-on to run it. The code is on the right, and the result of invoking the function doIt() is on the left.

Yes, it outputs 10 on the console. If you don’t know why it did so, get ready do allocate a substantial time for doing a really thorough testing of your HTML-JavaScript application.

People say that if you cut your finger don’t blame the knife. True. Should we even use sharp knives? Yes, if we are very careful and responsible adults. But we shouldn’t give sharp knives to kids. And majority of today’s enterprise developers are kids playing with legos, a.k.a. frameworks. Unfortunately, you can’t get away with creating an enterprise Web application without programming in a raw language. This is when the fun begins. Let’s embrace it, but be careful.

P.S. If you want to know why the code above printed 10 and not 5, read an excellent blog by Angus Croll where I picked this code sample from.

Advertisement

5 thoughts on “JavaScript’s Sharp

  1. DoIt returns SHowNumber and the second ShowNumber overwrites the first declaration, so 10 is the output. Do I win a prize?

    1. Derrick, I’d give you the prize if the reason of such return wouldn’t be explained in details 🙂

  2. It reminded me my Java lab:

    public class Main {
    public static void main(String[] args) {
    System.out.println( test(2) );
    }

    @SuppressWarnings(“finally”)
    public static int test(int n)
    {
    try {
    return n/n;
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    finally {
    return 0;
    }
    }
    }

    This code always return 0 instead of right results.

    If you don’t have deep knowledges in subject (like Java for me) you always find situations which surprise you. I think, before you playing with sharp knifes you must have a lot practice with bamboo sticks. So, enterprise developers must have a lot of experience to prevent (or rapidly eliminate) such bugs in code.

    1. My apologies, but I fail to see the purpose of the article/blog, whatever it is…the title sounded like there is a new tool for Javascript called “Sharp”, this is the 1st time here so maybe I have missed the fact that you call your blog “sharp”?

  3. it’s functions hoisting, you can avoid this result by defining the function as an Expression not a Declaration

    so instead of this:
    function foo(){}

    write:
    var foo=function(){}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s