JavaScript Advanced Programming (3rd Edition) Chapter III reading notes

Source: Internet
Author: User
Tags bitwise bitwise operators hasownproperty

Chapter III Basic Concepts

    1. Everything in ECMAScript (variables, function names, and operators) is case-sensitive.
    2. Identifiers are the names of variables, functions, attributes, or parameters of a function.
    3. The constituent rules for identifiers are: the first character must be a letter, an underscore (_), or a dollar sign ($), and other characters can be letters, underscores, dollar signs, or numbers.
    4. The ECMAScript identifier is in hump case format.
    5. ECMAScript comments include single-line comments (//) and block-level comments (/* * */).
    6. ECMAScript 5 introduces the concept of a strict pattern, which defines a different parsing and execution model for JavaScript.
    7. In strict mode, some of the indeterminate behavior in ECMASCRIPT3 will be handled, and some unsafe operations will throw an error. Start strict mode throughout the script to add "use strict" to the capital, or you can specify that the function is executed in strict mode.
    8. In strict mode, the results of JavaScript execution can vary greatly. Browsers that support strict mode include ie10+, firefox4+, safari5+, opera12+, and Chrome.
    9. The statement in ECMAScript ends with a semicolon, and if the semicolon is omitted, the parser determines the end of the statement. Plus semicolons can improve the performance of your code in some cases, because it eliminates the need for the parser to spend time speculating about where to insert the semicolon.
    10. Keywords and reserved words cannot be used as identifiers. (see page 21 of the book)
    11. ECMAScript variables are loosely typed and can be used to hold any type of data. A variable defined with the var operator becomes a local variable in the scope that defines the variable, and the uninitialized variable holds a special value of--undefined. In strict mode, a variable named eval or arguments cannot be defined, or it can result in a syntax error.
    12. ECMAScript has 5 basic data types: Undefined, Null, Boolean, number, String, a complex type: Object,object essentially consists of a set of unordered name-value pairs.
    13. Using the typeof operator on a value may return one of the following strings:

"Undefined"--if this value is defined;

"Boolean"--if this value is a Boolean value;

"String"--if the value is a string;

"Number"--if the value is numeric;

"Object"--if the value is an object or null;

"Function"--if this value is a function.

It is necessary to differentiate functions and other objects by using the TypeOf operator.

    1. The undefined type has only one value, that is, a special undefined. Executing the typeof operator on an uninitialized variable returns the undefined value, and the TypeOf operator that executes the undeclared variable also returns the undefined value.
    2. The null type also has only one value, which is special null. From a logical point of view, NULL represents an empty object pointer, so the typeof operator detects that null returns "Object".
    3. NULL = = undefined returns true because the = = operator translates its operands in the purpose of the comparison.
    4. Distinguish between null and undefined, the display initializes the variable to undefined, and the variable that holds the object explicitly saves the null value when the object is not actually saved.
    5. The Boolean type is only two literals: true and false,true do not necessarily equal 1,false not necessarily equal to 0.
    6. Conversion rules for various data types and Boolean types

Data type

The value converted to true

Value converted to False

Boolean

True

False

String

Any non-empty string

"" (empty string)

Number

Any non-0 numeric value (including infinity)

0 and Nan

Object

Any object

Null

Undefined

N/A (not applicable)

Undefined

    1. The number type uses the IEEE754 format to represent integers and floating-point values. Octal literals are not valid in strict mode, causing the JavaScript engine that supports the pattern to throw an error.
    2. Saving floating-point values requires twice times the memory space to hold the integer value, so ECMAScript converts the floating-point numbers to integer values whenever possible. You can use the Isfinite () function to determine whether a value is poor. The minimum value for ECMAScript is number.min_value=5e-324 and the maximum value is number.max_value=1.797693134623157e+308.
    3. NaN, which is a non-numeric value (not a number), is a special value that represents a case in which an operand that would have returned a numeric value is a return value. Any number divided by 0 will return nan and will not affect the execution of other code in ECMAScript.
    4. Nan Two major characteristics: any operation involving Nan, will return Nan;nan and any numeric value is not equal, including the Nan itself.
    5. The IsNaN () function is used to determine whether the received parameter is "not a numeric value".
    6. There are 3 functions to convert non-numeric values to numeric values: Number (), parseint (), and parsefloat ()
    7. The number () function can be used for any data type conversion. The conversion rules are as follows:

If it is a Boolean value, True and false are converted to 1 and 0, respectively;

If it is a numeric value, knowledge is simply passed in and returned;

If it is a null value, returns 0;

If it is undefined, return nan.

    1. The conversion rules for strings are as follows:

If the string contains only numbers (including cases preceded by a plus or minus sign), it is converted to a decimal value (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 (leading 0 is ignored);

If the string contains a valid hexadecimal format, such as "0xf", it is converted to a decimal integer value of the same size;

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.

    1. 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 result of the conversion is Nan, the object's ToString () method is called, and then the returned string value is converted again according to the previous rule.
    2. If the first character is not a numeric character or symbol, the parseint () function returns Nan. It is best to specify the cardinality when converting, such as var num1 = parseint ("10", 2); The output result is 2.
    3. Two differences between parsefloat () and parseint (): The first decimal point in the string is valid for parsefloat (), is not valid for parseint (), and parsefloat () always ignores the leading 0, which is the only decimal value that can be resolved. The use of cardinality is not specified with the second argument. Exception: parsefloat ("1234.00") = 1234, the parameter has no decimal point or zero after the decimal point, returns an integer.
    4. The string type is used to represent a sequence of characters consisting of 0 or more 16 Unicode characters, denoted by a single quotation mark (') or double quotation mark (").
    5. Character literal

Literal quantity

Meaning

\ n

Line break

\ t

Index

\b

Backspace

\ r

Enter

\f

Paper Feed

\\

Slash Slash

\’

Single quotation marks ('), used in single-quote strings, for example: ' He said,\ ' hey.\ '

\”

Double quotation marks ("), used in a string expressed in double quotation marks, for example:" He said,\ "hey.\" "

\xnn

The hexadecimal code nn represents a character (where n is 0~f). For example, \x41 represents "A"

\unnn

A Unicode character (where n is 0~f) expressed in hexadecimal code nnnn.

    1. There are two ways to convert a value to a string: toString (), numeric, Boolean, object, and string values, but null and undefined do not have this method. The ToString () method sets the conversion cardinality.
    2. You can also use the Transform function string () If you are unsure whether the value to be converted is null or undefined, and this way you can convert any type of value to a string that 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.

    1. 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. The following properties and methods exist:

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, where the property name must be specified as a string, such as O.hasownproperty ("name").

isPrototypeOf (object): Used to check whether an object is a prototype of an incoming object.

propertyIsEnumerable (PropertyName): Used to check whether a given property can be enumerated using the For-in statement.

Toloaclestring (): Returns the string representation of the object that corresponds to the region where the execution environment is executed.

ToString (): Returns the string representation of the object.

ValueOf (): Returns the string, numeric, or Boolean representation of an object. Usually the same as the return value of the ToString () method.

    1. Operators that can manipulate only one value are called unary operators, including the increment (+ +) and decrement (--) operators, all of which can be pre-and post-placed. These operators adhere to the following rules when applied to non-integer types:

When applied to a string that contains a valid numeric character, it is first converted to a numeric value, and then the operation with the addition minus 1 is performed. The string variable becomes a numeric variable.

When applied to a string that does not contain a valid numeric character, the value of the variable is set to Nan, and the string variable becomes a numeric variable.

When applied to a Boolean value of False, convert the Boolean variable to a numeric variable by first converting it to 0 and then performing the addition minus 1 operation.

When applied to Boolean True, the Boolean variable becomes a numeric variable by first converting it to 1 and then performing a plus minus 1 operation.

When applied to floating-point values, performs a plus minus 1 operation.

When applied to an object, the object's valueof () method is called to obtain a value that can be manipulated, then the preceding rule is applied to the value, and if the result is Nan, the preceding rule is applied after the ToString () method is called. The object variable becomes a numeric variable.

  1. The unary plus operator is represented by a plus sign (+), placed before the numeric value, does not have any effect on the value, but when the unary plus operator is applied to a non-numeric value, the operator converts it like number () transformation function.
  2. The unary minus operator is represented by a minus sign (-), placed before the value, becomes negative, and when applied to a non-numeric value, the unary minus operator follows the same rule as the unary plus operator, and then converts the resulting value to a negative number.
  3. Bitwise operators are used to manipulate values at the most basic level, that is, by the bits in memory that represent the values. For signed integers, the first 31 of the 32-bit is the value used to represent integers, and the 32nd bit is used to represent the symbols for the numeric values: 0 for positive numbers and 1 for negative numbers. For negative words, the use of twos complement, that is, the first to find the corresponding positive number of binary code, and then seek the second binary anti-code, the last binary anti-code plus 1. Note: When working with signed positive numbers, you cannot access bit 31 (that is, the sign bit, starting from 0).
  4. For unsigned positive numbers, the 32nd bit no longer represents the symbol, so the unsigned number can only be positive, and the value of the unsigned integer may be greater because the extra one no longer represents the symbol and can be used to represent a numeric value.
  5. The bitwise non (not) operator is represented by a wavy line (~), and the result of performing a bitwise non is the inverse code of the returned value. The essence of bitwise non is that the negative value of the operand is minus 1.
  6. The bitwise AND (and) operator is represented by a ampersand character (&), and the bitwise-AND operation only returns 1 if the two numeric values correspond to 1 o'clock, and any one is 0 and the result is 0.
  7. The bitwise OR (or) operator is represented by a vertical bar symbol (|) Indicates that a bitwise OR operation returns 1 if a bit is 1, and only returns 0 if the two bits are 0.
  8. The bitwise XOR (XOR) operator is represented by an caret (^), which returns 1 if the corresponding two bits are 1 or 0, which is only one 1 o'clock of the two numeric digits.
  9. The left shift operator is represented by the two less than sign (<<), which shifts all bits of the value to the left by the specified number of digits. The left shift does not affect the sign bit of the operand and fills the right empty space with zeros.
  10. The signed right shift operator is represented by two greater than sign (>>), which moves the value to the right, but retains the sign bit, at which point the left slot fills all the slots with the value of the sign bit.
  11. The unsigned Right shift operator is represented by the three 3 greater than sign (>>>), which moves all 32 bits of the numeric value to the right, and for positive numbers, this method shifts to the right with the signed bit, but for negative numbers, the unsigned right shift is the zero fill space. And the unsigned right shifts the binary codes of negative numbers as positive binary codes, resulting in a large result.
  12. The logical non-operator consists of an exclamation mark (! ) represents any value that can be used in the ECMAScript. Logical non-operators follow these rules:

Returns False if the operand is an object;

Returns true if the operand is an empty string;

Returns False if the operand is a non-empty string;

Returns true if the operand is a numeric value of 0;

Returns False if the operand is any non-0 value (including infinity);

Returns true if the operand is null;

Returns true if the operand is Nan;

Returns true if the operand is undefined.

    1. The behavior of the Boolean () transformation function is simulated using two logical non-operators.
    2. The logic and operators are represented by two numbers (&&) and can be applied to operands of any type, not just Boolean values. 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:

Returns the second operand if the first operand is an object;

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 is a short-circuit operation, that is, if the first operand can determine the result, the second operand is not evaluated.

    1. The logical OR operator consists of two vertical bar symbols (| |) Indicates that if there is an operand that is not a Boolean value, logic or does not necessarily return 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.

Logic or also short-circuit operation. You can use this behavior of logic or to avoid assigning null or undefined values to variables.

    1. The multiplication operator is represented by an asterisk (*) to calculate the product of two numbers. In cases where special values are handled, the multiplication operator follows the following rules:

If the operands are numeric, perform a regular multiplication calculation. If the product exceeds the representation range of the ECMAScript value, the Infinity or-infinity is returned;

If one of the operands is Nan, the result is Nan;

If the inifinity is multiplied by 0, the result is Nan;

If the inifinity is multiplied by a value other than 0, the result is Infinity or-infinity, depending on the symbol of the signed operand;

If there is an operand that is not a numeric value, call number () in the background to convert it to a numeric value, and then apply the rule above.

    1. The division operator is represented by a slash sign (/) that performs the calculation of the second operand in addition to the first operand, with the following rules for handling special values:

If the operand is a numeric value, perform a general division calculation and return Infinity or-infinity if the quotient exceeds the representation range of the ECMAScript value;

If one of the operands is Nan, the result is Nan;

If the infinity is infinity, the result is Nan;

If 0 is brought out, the result is Nan;

If a non-zero finite number is removed by 0, the result is Infinity or-infinity, depending on the symbol of the signed operand;

If Infinity is removed by any non-0 value, the result is Infinity or-infinity, depending on the symbol of the signed operand.

If there is an operand that is not a numeric value, call number () in the background to convert it to a numeric value, and then apply the previous rule.

    1. The modulo (remainder) operator is represented by a percent semicolon (%), and the rules for handling special values are as follows:

If the operands are numeric, perform a general division calculation and return the remainder;

If the divisor is an infinity value and the divisor is a finite number, the result returns Nan;

If the divisor is a finite large number and the divisor is zero, the result is Nan;

If the infinity is infinity, the result is Nan;

If the divisor is a finite number and the divisor is an infinite number, the result is a divisor;

If the divisor is zero, the result is 0;

If there is an operand that is not a number, call number () in the background to convert it to a numeric value, and then apply the rule above.

    1. The addition operator (+), if the two operands are numeric, performs a regular addition calculation, and then returns the result according to the following rule:

If one of the operands is Nan, the result is Nan;

If it is infinity plus infinity, then the result is infinity;

If it is-infinity plus-infinity, then the result is-infinity;

If it is Infinity plus-infinity, the result is Nan;

If it is +0 plus +0, the result is +0;

If it is-0 plus-0, then the result is-0;

If it is +0 plus-0, the result is +0;

If one operand is a string, the following rule is applied:

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, call their ToString () method to get the corresponding string value, and then apply the preceding rule about the string. For undefined and null, the string () function is called separately and the strings "undefined" and "null" are obtained.

    1. The subtraction operator (-) adheres to the following special rules when dealing with various conversions of this data type:

If all two operators are numeric, perform a regular arithmetic subtraction operation and return the result;

If one of the operands is Nan, the result is Nan;

If it is infinity minus infinity, the result is Nan;

If it is-infinity minus-infinity, the result is Nan;

If it is Infinity minus-infinity, the result is Infinity;

If it is-infinity minus Infinity, the result is-infinity;

If +0 minus +0, the result is +0;

If it is +0 minus-0, the result is-0;

If it is-0 minus-0, the result is +0;

If an operand is a string, Boolean, NULL, or undefined, the number () function is called first in the background to convert it 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 the resulting value is Nan, the result of the subtraction is Nan. If the object does not have a valueof () method, its ToString () method is called and the resulting string is converted to a numeric value.

    1. The relational comparer has less than (<), greater than (>), less than or equal (<=), and greater than or equal (>=). The rules are as follows:

If the two operands are numeric, a numeric comparison is performed.

If the two operands are all strings, the character encoding values corresponding to the two strings are compared;

If one operand is a numeric value, the other operand is converted to a numeric value, and then a numeric comparison is performed;

If an operand is an object, the valueof () method of the object is called, and the resulting result is compared with the previous rule, and if no valueof () method is used, the ToString () method is the same as above.

If an operand is a Boolean, it is converted to a numeric value before the comparison is performed.

Note: Any operands that are related to Nan are compared with the result of false.

    1. Equality (= =) and (! =), the operands are converted before the equality of them is compared.

To convert different data types, the rules are as follows:

If one of the operands is a Boolean value, convert it to a numeric--false before comparing equality to convert to 0,true to 1;

If one operand is a string and the other operand is a numeric value, the string is converted to a numeric value before equality is compared;

If one operand is an object and the other operand is not, the valueof () method of the object is called, and the resulting base type value is compared with the preceding rule.

Two operators follow the following rules when making comparisons:

null and undefined are equal.

Before you can compare equality, you cannot convert null and undefined to any other value.

If one of the operands is Nan, the equality operator returns FALSE, and the inequality operator returns TRUE. Even though the two operands are Nan, the equality operator also returns false.

If the two operands are objects, the equality operator returns true if they are not the same object, or False if they point to the same object.

    1. Congruent (= = =) and non-congruent (!==), the operands are not converted until compared, and other aspects are not distinguished from the equality and inequality operators. Note: null==undefined will return true, but null===undefined will return false. Because they are different types of values.
    2. The conditional operator is the same as the Java syntax. Variable = a? B:C;
    3. The simple assignment operator is represented by the equals sign (=), which is the value on the right to the left side of the variable.
    4. The complex assignment operators are mainly:

Multiply/Assign value (*=);

Addition/Assignment (/=);

Modulus/Assignment (%=);

Add/Assign value (+ =);

Reduced/assigned value (-=);

Left Shift/Assignment (<<=);

Signed Right Shift/Assignment (>>=);

Unsigned Right Shift/Assignment (>>>=).

    1. The comma operator is used to declare multiple variables, and when used to assign a value, the comma operator always returns the last item in the expression, such as var num = (5,4,3,2,1)//num=1.
    2. The syntax for the IF statement is: if (condition) statement1 else statement2
    3. The Do-while statement is a post-test looping statement that tests export conditions only after the code in the loop body executes. Before a conditional expression is evaluated, the code in the loop body is executed at least once.
    4. The while statement belongs to the pre-test loop statement, and the exit condition is evaluated before the code inside the loop paste is executed, so the code in the loop body may be used to not be executed.
    5. The For statement is also a pre-test loop statement, but it has the ability to initialize variables and define the code to execute after the loop before executing the loop. Variables defined inside the loop can also be accessed externally.
    6. The for-in statement is a precise iterative statement that can be used to enumerate the properties of an object. The syntax is as follows:

For (property in expression) statement

Before using the for-in loop, first detect that the value of the object is not null or undefined.

    1. Use the Label statement to add tags to your code for future use. The syntax is:

Label:statement

Tagged statements are generally used in conjunction with loop statements such as for statements.

    1. The break and continue statements are used to precisely control the execution of code in a loop, where the break statement exits the loop immediately, forcing the statement following the loop to continue execution. The continue statement, though, exits the loop immediately, but exits the loop and resumes from the top of the loop.
    2. Both the break and continue statements can be used in conjunction with a label statement to return a specific location in the code. The case that is applied to the loop nesting.
    3. The function of the WITH statement is to set the scope of the code to a specific object. The syntax is as follows:

With (expression) statement

In strict mode, the WITH statement is not allowed, otherwise it is considered a syntax error. The large use of with statements can result in degraded performance, which can also make debugging code difficult and not recommended for use in large applications.

    1. Switch statement, you can use any data type, followed by the value of each case is not necessarily a constant, it can be a variable, or even an expression. The switch statement uses the strict equality operator when comparing values, so type conversions do not occur.
    2. Any function can implement the return value at any time by the return statement followed by the value to be returned, and any code after the return statement will never be executed.
    3. Strict mode has some limitations on functions:

The function cannot be named eval or arguments;

The parameter cannot be named eval or arguments;

Two named arguments cannot appear with the same name.

Doing so will result in a syntax error and the code cannot execute.

    1. The arguments in the ECMAScript are represented inside the function by an array of arguments objects that can be accessed by the function body to obtain each parameter passed to the function. The arguments object is just like an array, it is not an instance of an array, you can use the square brackets syntax to access each of its elements, and use the Length property to determine how many arguments are passed in. Therefore, named parameters are only convenient, but are not required. And the arguments object can be used with named parameters. The value of arguments is used to maintain synchronization with the value of the corresponding named parameter (not valid in ECMAScript 5).
    2. Because there is no feature of the function signature, the ECMAScript function is not overloaded, that is, two functions with the same name are defined, and the name belongs only to the post-defined function.

JavaScript Advanced Programming (3rd Edition) Chapter III reading notes

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.