首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

Top 十 Things that JavaScript Got Wrong!

2012-11-22 
Top 10 Things that JavaScript Got Wrong!!!reference from http://net.tutsplus.com/tutorials/javascri

Top 10 Things that JavaScript Got Wrong!!!
reference from http://net.tutsplus.com/tutorials/javascript-ajax/top-10-things-that-javascript-got-wrong/

    JavaScript, if only by default, is one of the most popular programming languages available. Over the years, it's been labeled as a nightmare to work with, and, to some extent, this is true! However, more often than not, what people mean to say is that the DOM API is a nightmare. Nevertheless, there are a handful of flat-out errors in the language.

1. The Name. JavaScript is NOT Java

    We'll start with a fun jab at the name choice. While it was originally called Mocha, and then LiveScript, it was later changed to JavaScript. According to history, its similarities to the name Java was the result of a collaboration between Netscape and Sun, in exchange for Netscape bundling the Java runtime within their popular browser. It's also been noted that the name came, almost as a joke, due to the rivalry between LiveScript and Java for client-side scripting.

    Nevertheless, it resulted in thousands of "JavaScript has nothing to do with Java" comments in forums across the web!

2. Null is an Object?

    Consider this..


3. NaN !== NaN

    NaN, as we'd expect refers to a value that is not a legal number. The problem is that NaN isn't equal to anything...including itself.

   

6. Scope Inconsistencies

    Consider the following code:


    The reason why foo(bar.method) does not render the same result is because the method function will be called as a method of the window object, rather than bar. To fix this, we must call bar.method() from within the passed anonymous function.

    Thanks so much to Jeremy McPeak for notifying me of this error.

7. The Use of Bitwise Operators

JavaScript shares many similarities with Java - one of them being the set of bitwise operators.

  

8. Too Many Falsy/Bottom Values

    Maybe this isn't specifically an error in JavaScript, but it certainly makes the learning process, especially for beginners, a tough one. Values like null, false, and undefined almost mean the same thing, but there are differences that can be confusing to understand.

    To test, open up the console in Firefox, and find the boolean of the following items.



    So why does JavaScript do this? It's because of something called "semicolon insertion." Essentially, JavaScript will attempt to correct our bad coding. If, for instance, it thinks that you've left off a closing semicolon, it'll go ahead and add it on for you. Though this was originally intended to be a convenience, especially for newer JavaScripters, it's actually a very bad thing when you don't have control over your own code, as demonstrated above.

    In our example, there's no way to determine why foo.a returns "undefined. " Now that we're aware of semicolon insertion, the reason it's undefined is because JavaScript will add a semicolon to the end of the return statement.

  return; // JS incorrectly adds this semicolon.    {       a : 'b'; // It'll add a semicolon here as well, because it doesn't realize that this is an object.    };  

   
    Aritcle so far ended ,but dispute about js confuse is not over ,if you like this article ,please paste your idea or thoughts below this blog ,in chineses or english ,help by yourself.

    No doubt here, no progress there!!!

热点排行