The beauty and the chicken of JavaScript

Source: Internet
Author: User
Tags bitwise bitwise operators object object hasownproperty

--Summary from: The language of the JavaScript language has its beautiful place and its ribs. Avoid the dross of some language, can reduce the probability of the occurrence of the bug correspondingly. Beautiful Place:A function is a first-class object based on prototype inheritance of dynamic object object literals and array literals dross: 1. Global VariablesThere are three ways to express a global variable: var declaration: var foo = value;    Add an attribute to the global object, which is added to the window: Window.foo = value; Undeclared variable: foo = value; Although variables can be used without declaration, this can cause a lot of later bugs to appear, and these bugs are hard to find. 2. ScopeIn JavaScript, there is no block-level scope, and a variable declared in a code block is visible anywhere the function that contains the block of code is located. So, the best way in JS is to declare all the variables at the beginning of each function. 3. Automatic insertion of semicolonsJavaScript can fix a defective program by automatically inserting a semicolon, but that can cause a lot of serious bugs and is not easy to spot. return{Status:true} The above example actually returns undefined, because the starting part of the value expression must be on the same line as return, not one line, JS will be automatically complete, resulting in an error. The correct wording should be as follows: return {status:true} 4. Reserved wordsReserved words are not used in the language, but are reserved for possible later use. They cannot be used to name variables or parameters. When reserved words are used as key values for object literals, they must be enclosed in quotation marks. 1 Key Words
Break case Catch Continue default Delete does else finally for function if in instanceof new return switch this throw try Ty peof var void while with
2 reserved words
Abstract Boolean byte char class const debugger Double enum export extends Fimal float goto implements import int INTERFAC E Long mative package private protected public short static super synchronized throws transient volatile 5. UnicoeJavaScript is 16-bit. Unicoe treats a pair of characters as a single character, while JavaScript considers a pair of characters to be two different characters. Specific unknown ... 6. typeofThe typeof operator returns a string that identifies the type of operand. But it can't be null. 7. parseintparseint (' 16 ') and parseint (' tons ') as a result. If the first character of the string is 0, then the string will be based on the 8 binary instead of the 10, so the standard notation is parseint (' 08 ', 10); 8. +The + operator can be used for addition operations and string joins, and how to do this depends on the type of its arguments. Addition is only done when two numbers are integers. 9. Floating point numberBinary floating-point numbers do not correctly handle decimal decimals, in JavaScript, 0.1 + 0.2 is not equal to 0.3. But this problem can be avoided. For example, the dollar can be converted to cents by multiplying by 100, then by adding and reducing them, and then dividing their sum by 100来 back into dollars. Ten. NaNNan is a special quantity value defined in the IEEE753. It does not represent a number, but typeof NaN = = = ' No. ' returns True. Nan is produced in the conversion operation of the code, and Nan is not equal to its own. Nan appears not easy to troubleshoot, fortunately isNaN () can recognize numbers and Nan 11. Pseudo-ArraysJavaScript does not have a true array, its array is a pseudo-array, the performance is poor. The type cannot distinguish between arrays and objects and needs to determine whether an array is required to check his constructor property: if (My_value && typeof my_value = = = ' object ' && typeof My_ Value.length = = = ' Number ' &&! (my_value.propertyisenumerable (' length '))) {//my_value is really an array} 12. False ValuesJavaScript has a bunch of fake values. such as 0 NaN ' false null undefined be very careful when distinguishing them. hasOwnProperty.The hasOwnProperty method is used as a filter to avoid a problem with the for-in statement. Unfortunately, hasOwnProperty is a method, not an operator, so in any object, he may be replaced by a different function or even a non-function value. 14. Object JavaScript objects do not have empty objects, because they can get member elements from the prototype chain. And there are a lot of bugs that come along. Chicken Ribs:1. = = Do not use = = and! =, should use = = = and! ==,=== and! = = compares the data types and values of the two values. 2. The WITH statement does not use with, because it will have a bug in the presence of 3. Evaleval makes the code more difficult to read, and neither security nor performance is good. 4. Continue statements
The continue statement jumps to the top of the loop. Using continue can degrade performance  5. The switch runs through the switch condition and causes other problems. Useful, but very dangerous.  6. Missing block statements if/while/do and for statements can accept either a block of code in parentheses or a single-line statement. But it blurs the structure of the program. Strict specification and always using code blocks makes the code easier to understand.  7. ++--These two operators are easy to contribute to an imprudent programming style. Most of the security vulnerabilities caused by buffer overflow errors are caused by code like this.  8. Bitwise operators &     and bitwise AND |      or bitwise OR ^      XOR bitwise XOR         not bitwise NON > >     Right shift >>>   unsigned right shift <<     left in most languages, these bitwise operators are close to hardware processing and very fast, in JavaScript, Bitwise operators are not hardware-handled and are very slow. It reduces the redundancy of the language, making bugs more difficult to detect.  9. Function Statements contrast functional expressions A functions statement is a shorthand form of a VAR statement whose value is a function. A function statement is promoted when it is parsed. means that no matter where the function is placed, it is moved to the top level of the scope at which it was defined. Using a function statement in an IF statement is forbidden. Because most browsers allow the use of function statements in an If statement, they are handled differently at parse time. The problem of portability is created.  10. The wrapper object of type new Boolean (FALSE) returns an object that has a alueof method that returns the wrapped value. Do not use the New boolean/new number/new string to avoid using the new object and the new Array, instead using {} and [].  11. Newjavascript creates a new object that inherits the prototype of its operand, and then calls the operand to bind the newly created object to this. Occurs during new and initializedA lot of problems. It's best not to use new.  12. void in JavaScript, void is an operator. Invalid, useless, obscure, avoid using it.

The beauty and the chicken of JavaScript

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.