JavaScript Learning notes--3. Basic JavaScript Concepts

Source: Internet
Author: User
Tags bitwise hasownproperty

1. Everything in ECMAScript (variables, function names, and operators) is case-sensitive.
2. Identifier: Refers to the name of a variable, function, property, or parameter of a function.
    • The first character must be a letter, an underscore (_), or a dollar sign ($);
    • Other characters can make letters, underscores, dollar signs, or numbers.
    • By convention, the ECMAScript identifier is in hump-case format.
3. Strict mode: *use strict*, you can enable strict mode in the script, written at the top of the code. 4. The statement in ECMAScript takes a semicolon as the end. Although not required, it can be used to avoid errors (incomplete input etc), and in some cases can increase the performance of the code (the parser does not need to speculate where a semicolon is required). 5. Variables: Variables in ECMAScript are loosely typed, that is, they can be used to hold any type of data--each variable is just a placeholder for holding the value.
    • var variable name;
    • Omitting the var operator allows you to define global variables, but is not recommended. Because global variables defined in local scopes are difficult to maintain, and if the Var operator is deliberately ignored, it can cause unnecessary confusion due to the fact that the variable is not immediately defined (definition). Assigning a value to an undeclared variable causes a Referenceerror error to be thrown in strict mode.
6. Data type: ECMAScript there are 5 simple data types (base data types): Undefined, Null, Boolean, number, string, and a complex data type object.
    • The typeof operator is used to detect the data type of a given variable
    • typeof Null returns an object because a special value of NULL is considered to be an empty objects reference
7. The undefined type has only one value, which is the special undefined. This value is introduced by the third version of ECMA-262, which distinguishes between null object pointers and uninitialized variables. However, a variable that contains a undefined value is not the same as a variable that has not been defined.
    • var message, "Defines a variable named message, but uninitialized", using alert (message) to display the value of the message variable as "undefined";
    • Without a "var message", it means that the so-called message variable is not defined, that is, the message variable does not exist in nature, and an error is generated by using alert (message).
    • However, executing the typeof operator on an uninitialized variable returns the undefined value, and the typeof operator for a variable that is not declared (undefined) also returns the undefined value.
    • So, even if the uninitialized variable is automatically assigned the undefined value, it is still worth recommending that the variable be explicitly initialized, because when the typeof operator operates on the variable, we can immediately know that the variable being detected has not been declared, Instead of being initialized (example: var message = "Example" should be used).
    • Notice the difference between "initialize" and "declared".
8. The null type also has only one value, that is, special null. From a logical point of view, a null value represents an empty object pointer, so typeof NULL returns "Object".
    • If the defined variable is intended to be used to hold the object in the future, it is better to initialize the variable to null instead of the other value, so that it is convenient to check the null value to see if the variable holds a reference to the object (if (car! = null))
9. All types of values in ECMAScript have values that are equivalent to the Boolean value, var message = "Hello";    var Messageasboolean = Boolean (message); The message string is converted to a Boolean value that is stored in the Messageasboolean. 10. The highest precision of a floating-point value is 17 decimal places, but it is not as accurate as an integer when doing arithmetic calculations, so do not do a similar operation if (a + b = = 0.3). Never test a specific floating-point value. A. NaN (not a number) is used to denote a case where an operand that would have returned a numeric value did not return a numeric value. In ECMAScript, any number divided by 0 will return Nan, so it will not affect the execution of other code.
    • Any operation that involves Nan will return Nan
    • Nan and any values are not equal, including the Nan itself.
    • The IsNaN () function, which can accept a parameter, can make any type, and the function can be used to help determine whether the parameter is "not a numeric value".
12. There are 3 functions that can convert non-numeric values to numeric values: Number (), parseint (), and parsefloat (). The first is for any data type, and the latter is specifically used to convert the string to a numeric value. When working with strings, the parseint () function ignores the spaces in front of the string until the first non-whitespace character is found, and if the first character is not a numeric character or a minus sign, parseint () returns Nan; if the first character is a numeric character, parseint () Will continue parsing the second character until all subsequent characters have been parsed or a non-numeric character is encountered, and the decimal point is not a valid numeric character. The parseint function is often given the second argument as the base (that is, how many binary) to use when converting: var num = parseint ("AF", 16);. In order to avoid parsing errors, it is recommended that the cardinality be explicitly specified in any case, so it is always necessary to have 10 as the second parameter when a decimal value is used frequently. The. String () function:
    • If the value has the ToString () method, the method (with no parameters) is called and the corresponding result is returned;
    • Returns "NULL" if the value is null;
    • Returns "Undefined" if the value is undefined
15. In ECMAScript, the object type is the basis of all its instances, that is, any properties and methods that the object type has also exist in more specific objects. Each instance of object has the following properties and methods:
    • Constructor: Holds the function that is used to create the current object.
    • hasOwnProperty (PropertyName): Used to check whether a given property exists in the current object instance, not in the instance's prototype. Where PropertyName must be specified as a string (O.hasownproperty ("name"));
    • isPrototypeOf (object): Used to check if an incoming object is a prototype of another object;
    • Propertyisenumberable (PropertyName): Used to check whether a given property can be enumerated using a for-in statement, and the property name must be specified as a string;
    • toLocaleString (): Returns the string representation of an object that corresponds to the region of the execution environment;
    • ToString (): Returns the string representation of the object;
    • ValueOf (): Returns the string, numeric value, or Boolean representation of the object.
16. Unary increment and decrement operator rules:
    • When applied to a string containing a valid numeric character, it is now converted to a numeric value, and then the operation of the addition minus 1-the string variable becomes a numeric variable;
    • When applied to a string that does not contain a valid numeric character, set the value of the variable to nan-string variable into a numeric variable;
    • When applied to a Boolean value of false, it is now converted to 0 and then the operation plus minus 1-The Boolean variable becomes a numeric variable;
    • When applied to a Boolean value of true, it is now converted to 1 and then the operation plus minus 1-The Boolean variable becomes a numeric variable;
    • When applied to floating-point values, performs a plus minus 1 operation;
    • When applied to an object, first call the object's valueof () method to get a value that can be manipulated, and then apply the preceding rule to the value. If the result is Nan, then the preceding rule-object variable becomes a numeric variable after calling the ToString () method.
17. Unary Plus and minus operators:
    • When a unary plus operator is applied to a non-numeric value, the operator performs the conversion as the number () transformation function does.
    • When a unary subtraction operator is applied to a non-numeric value, a unary plus operator rule is followed, and the resulting value is converted to a negative number.
18. Bitwise NON (NOT)
    • A bitwise non-operation is represented by a wavy line (~), and the execution result is the inverse of the returned value: 25 (00000000000 ... 1101) then ~25 (11111111 ... 0010), the end result is the same as NUM1 =-num1-1, but the bitwise is not at the bottom of the numeric representation, so it is faster.
19. Bitwise AND (and)
    • The bitwise AND operator is represented by a and number character (&), and he has two operands. Essentially, each bit of the two values is aligned, and then an and operation is made (two 1 returns 1, otherwise 0 is returned)
20. Bitwise OR (OR)
    • Represented by (|), similar to and, but only 0 and 0 return 0
21. Bitwise XOR OR (XOR)
    • Represented by an caret (^), there are two operands, if the same 1 (two 1) or the same 0 (two 0), return 0, otherwise return 1
22. Move left
    • Represented by two less than sign (<<), this operator moves all bits of the specified value to the left of the specified number of bits: var oldValue = 2; var newvalue = OldValue << 5 will change 10 to 1000000.
    • The left shift does not affect the sign bit of the operand, so 2 moves 5 bits to the left and returns-64
23. Signed Right Shift
    • The signed right-shift operator is represented by two greater-than sign (>>), which moves the value to the right, but retains the symbol bit.
24. Unsigned Right Shift
    • The unsigned right shift is represented by the three greater than sign (>>>), which moves all 32 bits of the value like right. For positive numbers, the same as a signed right shift. For negative numbers, however, because unsigned right shifts fill empty spaces with zeros instead of filling them with the value of the sign bit, and the unsigned right-shift operator takes a negative binary code as a positive binary, resulting in a very large result after the unsigned right shift
25. The logical non-operator can be used to convert a value to its corresponding Boolean value, while using two logical non-operators, time will simulate the behavior of the Boolean () transformation function, the first logical non-operation will be based on whatever operand returns a Boolean value, and the second logical non-operation to the Boolean value negation , you can get the Boolean value that this value really corresponds to. 26. Logical OR assignment: var myObject = Preferredobject | | Backupobject;
    • Indicates that a fallback value is given to MyObject only if the preferredobject does not contain a valid value
ECMAScript does not contain block-level scopes; not End yet ...

JavaScript Learning notes--3. Basic JavaScript Concepts

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.