JavaScript Advanced Programming Notes (i)

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

1. The ECMA specifies the following components of the language: syntax, type, statement, keyword, reserved word, operator, object


2. What is ECMAScript compatible
Supports all "types, values, objects, properties, functions, and program syntax and Semantics" described by ECMA
Supports Unicode character standards.
In addition, the following extensions are available for compatible implementations.
Add "more types, values, objects, properties, and functions" that are not described by the ECMA.
Supports "program and regular expression syntax" that is not defined by ECMA.


3.5 Simple data types: Undefined (ambiguous, undefined), null (NULL), Boolean, number, String
1 Complex data types--object, Object essentially consists of a set of unordered name-value pairs. The object in ECMAScript is actually a set of data and functions.


4.typeof return Value:

"Undefined"--if this value is undefined or does not exist: Var A or//var a
"Object"--if this value is an object or null;
"Function"--if this value is a function.


5.typeof is an operator and not a function, so parentheses are not required, although they can be used. Alert (typeof (Message)); Alert (typeof 95);


6.typeof NULL returns "Object" because the special value NULL is considered to be an empty object reference.


The 7.Undefined type has only one value, that is, a special Undefined. When declaring a variable with VAR but not initializing it, the value of this variable is undefined


8. 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.


9. In fact, the undefined value is derived from a null value, so Undefined==null is true


Value of Boolean () converted to true: value converted to false:
Non-empty string "" (empty string)
Non 0 numeric values (including infinity) 0 and NaN
Any object null


11, var octalNum1 = 070; Eight-binary 56
var octalNum2 = 079; Invalid octal value--resolves to 79
var octalNum3 = 08; Invalid octal value--resolves to 8
var hexNum1 = 0xA; Hex 0x starts with
var hexNum2 = 0x1f; Hex of 31
In arithmetic calculations, all values in eight and hexadecimal are eventually converted to decimal values.
Positive zero and negative 0 are considered equal (congruent)


12, 3.125e7 equals 31250000 0.0000003 equals 3e-7 digits after the decimal point


13. Range of values
Due to memory limitations, ECMA does not save all the values in the world. The minimum value that the ECMA can represent is saved in Number.min_value-the value is 5e-324 in most browsers, and the maximum value that can be represented is saved in Number.MAX_VALUE-in most browsers, this value is 1.7976931348623157e+308. If the result of a calculation results in a value that exceeds the range of JavaScript values, the value is automatically converted to a special Infinity value. Specifically, if the value is negative, it is converted to-infinity (negative infinity), and if the value is positive, it is converted to Infinity (positive infinity). As mentioned above, if a calculation returns a positive or negative Infinity value, the value will not continue to participate in the next calculation because Infinity is not able to participate in the calculated value. To determine whether a value is poor (in other words, between the minimum and maximum values), you can use the Isfinite () function. This function returns True when the parameter is between the minimum and maximum values


14. Any operation that involves Nan (for example, NAN/10) will return Nan
Nan is not equal to any value, including the Nan itself.
IsNaN ()
0 divided by 0 returns NaN, positive number divided by 0 returns Infinity, negative number divided by 0 returns-infinity.


Number (), parseint () parsefloat ()


the conversion rules for the. Number () function are as follows.
The Boolean value, True and false, are converted to 1 and 0, respectively.
A numeric value that is simply passed in and returned.
a null value that returns 0.
undefined, returns NaN.
if it is a string, follow these rules:
If the string contains only numbers (including cases preceded by a plus or minus sign), it is converted to a decimal value, which is "1"
will become 1, "123" will become 123, and "011" will become 11 (note: The leading 0 is ignored);
If the string contains a valid floating-point format, such as "1.1", it is converted to the corresponding floating-point value (again, the
slightly leading 0);
If the string contains a valid hexadecimal format, such as "0xf", it is converted to a decimal integer of the same size
value;
if the string is empty (contains no characters), it is converted to 0;
If the string contains characters other than the above format, it is converted to NaN.
if it is an object, the ValueOf () method of the object is called, and the returned value is converted according to the preceding rule. If the conversion
result is NaN, the object's ToString () method is called, and the returned characters are converted again according to the previous rule
string value.
the unary plus minus operator operates the same as the number () function.


17. Because the number () function is complex and unreasonable when converting a string, it is more commonly used when working with integers
parseint () function.

converting an empty string returns NaN if the first character is a numeric character, parseint () continues to parse the second character, straight


var num = parseint ("0xAF", 16); 175
In fact, if you specify 16 as the second argument, the string can be without the preceding "0x"
parsefloat () and parseint () is that he can identify the first decimal point, and two differences are that it will always be a
slightly leading 0. The hexadecimal-formatted string is always converted to 0. Because parsefloat () resolves only the decimal value because


ToString ()
Numeric, Boolean, object, and string values have the ToString () method. However, the null and undefined values do not have this method.
Num.tostring (2)//Convert to 2 binary
The String () function follows the following translation rules:
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;
If the value is undefined, then "undefined" is returned.


var o = new Object; Valid, but omit parentheses are not recommended

Each instance of the


20. Object has the following properties and methods. The ECMA is not responsible for defining the host object, so the host object may or may not inherit object.
constructor: Holds the function that is used to create the current object.
hasownproperty (PropertyName): Used to check the given property in the current object instance instead of the instance


isprototypeof (object): Used to check if an incoming object is a prototype of an incoming object
propertyisenumerable (PropertyName): Used to check whether a given property can use the For-in statement

tolocalestring (): Returns the string representation of an object that corresponds to the region of the execution environment.
tostring (): Returns the string representation of an object.
valueof (): Returns the string, numeric, or Boolean representation of an object. Usually the same as the return value of the ToString () method


21. Operators include: arithmetic operators, bitwise operators, relational operators, equality operators
When you perform a pre-increment and decrement operation, the value of the variable is changed before the statement is evaluated.
Pre-increment and decrement operations are equal to the precedence of executing statements


22. Bitwise Operators 40 pages


23. Not logical NOT! A logical non-operator can also be used to convert a value to its corresponding Boolean value!
Logic and &&
In the case where an operand is not a Boolean value, the logic and operation do not necessarily return a Boolean value, at which point it follows the following rules:
if the first operand is an object, returns the second operand;
if the second operand is an object, the object is returned only if the value of the first operand evaluates to true;
if the two operands are objects, the second operand is returned;
returns null if one of the operands is null;
if one of the operands is Nan, a nan is returned;
if one of the operands is undefined, the undefined is returned.
logic and operations are short-circuiting operations where the second operand is no longer evaluated if the first operand can determine the result.
for logic and operations, if the first operand is false, the result is no longer likely to be true regardless of the value of the second operand.
undefined values cannot be used in logic and operation, otherwise an error will be
logical OR OR | |
If there is an operand that is not a Boolean value, the logic or not necessarily returns a Boolean value, at which point it follows the following rules:
if the first operand is an object, the first operand is returned;
returns the second operand if the evaluation result of the first operand is false;
if the two operands are objects, the first operand is returned;
returns NULL if all two operands are null;
if the two operands are Nan, a nan is returned;
if the two operands are undefined, the undefined is returned.
A logic or operator is also a short-circuit operator. In other words, if the first operand evaluates to true, the second operand is not evaluated.


. +
if the two operands are strings, the second operand is stitched together with the first operand;
If only one operand is a string, the other operand is converted to a string, and then the two strings are stitched together.
If an operand is an object, numeric, or Boolean value, the ToString () method that calls them gets the corresponding string value .
then apply the preceding rules about strings. For undefined and null, the String () function is called separately and the character is obtained
string "undefined" and "null".
     -
If an operand is a string, Boolean, NULL, or undefined, the number () function is called in the background first to
it is converted to a numeric value, and then the subtraction calculation is performed based on the preceding rule. If the result of the conversion is Nan, the result of the subtraction is Nan;
If an operand is an object, the ValueOf () method of the object is called to get the numeric value that represents the object. If you get
value is Nan, the result of subtraction is Nan. If the object does not have a valueOf () method, its toString () is called
method and converts the resulting string to a numeric value.
><, .....
If all two operands are strings, compare the character encoding values of the two strings.
if one operand is a number, the other operand is converted to a numeric value, and then a numeric comparison is performed.
If an operand is an object, call the ValueOf () method of the object and use the resulting result to follow the previous rules
row comparisons. If the object does not have a valueOf () method, the ToString () method is called and the resulting results are based on the previous
the rule execution comparison.
If an operand is a Boolean, it is converted to a numeric value before the comparison is performed.
var result = "Brick". toLowerCase () < "Alphabet". toLowerCase ();//false. toLocaleUpperCase ()


25. Because of the type conversion problem with equality and inequality operators , it is recommended to use both congruent and non-identical operators in order to maintain the integrity of the data types in the code.


The Do-while statement is a post-test looping statement that tests export conditions only after the code in the loop body is executed.
In other words, the code in the loop body is executed at least once before the conditional expression is evaluated. The while statement belongs to the pre-test loop statement
for (;;) {//Infinite loop
DoSomething ();
}

var count = 10;
var i = 0;
for (; i < count;) {//equals while
alert (i);
i++;
}
The for-in statement is a precise iterative statement that can be used to enumerate the properties of an object.
If the value of the variable representing the object being iterated is null or undefined, the loop body is not executed.
Break continue


The function of the WITH statement is to set the scope of the code to a specific object, and the purpose of defining the with statement is primarily to simplify the work of writing the same object multiple times:
var qs = location.search.substring (1),
HostName = Location.hostname,
url = location.href;
Equals:
With (location) {
var qs = search.substring (1),
HostName = HostName,
url = href;
It is not recommended to use the WITH statement when developing large applications because of the performance degradation caused by the large use of with statements and the difficulty of debugging code.


The. Switch statement uses the congruent operator when comparing values, so type conversions do not occur


29. Either let the function always return a value, or never return a value.
You do not need to specify a function's return value, because any ECMAScript function can return any value at any time. These two words don't understand
In fact, a function that does not specify a return value returns a special undefined value.


Arguments[0] The length of the number function of the arguments.length argument is the number of formal parameters.

Named parameters that do not pass a value are automatically assigned the undefined value.
All parameters in the ECMA pass a value, and it is not possible to pass arguments by reference.


31. A primitive type value refers to a simple data segment, whereas a reference type value refers to an object that may consist of multiple values.
5 basic data types are accessed by value, because the actual values saved in the variable can be manipulated.
Unlike other languages, JavaScript does not allow direct access to in-memory locations, which means that the object's memory space cannot be manipulated directly.
When you manipulate an object, you are actually manipulating the object's reference rather than the actual object. To do this, the value of the reference type is accessed by reference. (This is not a strict statement, when copying a variable that holds an object, the action is a reference to the object.) However, when you add a property to an object, you are manipulating the actual object. )


32. Copy the value of the base type and create a new value on the variable object. When you copy a value of a reference type, a copy of the pointer is copied,
Therefore, changing one of the variables will affect the other variable

JavaScript Advanced Programming Notes (i)

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.