Some bad features of js

Source: Internet
Author: User

Some bad features of js
First of all, it is undeniable that JavaScript is a weak language with many excellent features. However, this language was put into engineering practice at the beginning of its design and has not undergone rigorous laboratory tests, it is regarded as a toy language that is not visible to developers for a long period of time, and some features are indeed poorly designed, today I will talk about these bad features in detail. 1. The first worst feature in js should count its dependence on global variables. What is a global variable? That is to say, it is open to all domains and can be modified at any time and in any part, which greatly reduces the program reliability. Secondly, for large projects, as the business expands and the amount of Code increases, the program becomes larger and larger. variable naming in subprograms is likely to conflict with global variables, this may cause the program to fail to run normally. The ideal design is that the entire program has only one global variable, which can be implemented using closures. 2. Scope. Js has no block-level scope and only function scope. It does not conform to common conventions in variable declaration. Of course, bibao can also be used to simulate block-level scope. 3. Reserved Words. Js contains many words that are not used but will be retained, such as class. They cannot be named as variables or parameters. Attribute names used as object declarations must be enclosed by quotation marks, the name of an object attribute using reserved words cannot be accessed through dot notation, but can only be accessed through brackets notation. Var a; // correct var class; // error. The variable var person = {name: "aaa"} // correct var person = {class: "ren";} // error. The reserved words must be enclosed in quotation marks (var person = {"class": "ren"}). // the correct person. class // error. You cannot use the dot notation to access the person ['class'] object with reserved words as attribute names. // The object is correct. 4. typeof operator. The typeof operator is used to return the basic type of an object. For example, typeof 1 returns the number, but unfortunately in typeof null, the object is returned, and we expect it to return null, because the returned object is meaningless. 5. Floating Point Number. Binary floating point numbers cannot correctly process decimal places. For example, the result of 0.1 + 0.2 cannot be 0.3. This is caused by following the binary floating point Arithmetic standard. This is a bit different from the mathematical knowledge we have been learning. If we do not know the reason, it may lead to some misleading, but the processing of integers is accurate. 6. naN. this indicates a special number value, but it is not a number, although typeof NaN = "number" (it is too pitfall here ). Note that NaN is not equal to itself, that is, NaN! = NaN (this is true. Remember !!!). NaN is generated mainly because you want to convert a non-numeric string into a number. 7. pseudo array. Strictly speaking, there is no real array in JavaScript. The array in it is actually an object with an attribute named integer and a length attribute, which is less efficient than a real array, however, we do not have to worry about cross-border errors. Arrays and objects cannot be determined through typeof. A program that looks more reliable and judges that a value is an array may be like this: if (value & typeof value = "object" & value. constructor = Array) {// value is an Array}, but the Array created in different frames or Windows will get false, the following program is the most reliable array detection program I know. The Code is as follows: if (value & typeof value = "object" & typeof value. length = "number" & typeof value. splice = "function "&&! (Value. propertyIsEnumerable ("length") {// value is an array}; of course, the bad JavaScript features are not limited to the above. For example, if a semicolon is inserted automatically, the returned value is undefined after the return line breaks, unicode character, parseInt function, connector "+", false value, and so on. The above is a major bad feature. Of course, you can share your opinions with others.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.