JS Advanced Programming Third Edition---

Source: Internet
Author: User
Tags cdata

Dom is not just for JS, many other languages also implement the DOM, but Dom is already an important part of JS.

BOM is a part of the JS implementation, but there is no relevant standards, so each browser has its own implementation, the problem is solved in HTML5.

1, <script> Properties:

Async (Asynchronous Load): Indicates that the script is downloaded immediately and does not affect other actions on the page, only valid for external scripts. <script async= "Async" type= "Text/javascript" </script>

CharSet: The character set of the code is specified through the SRC attribute, and most browsers ignore this value.

Defer: The script can be deferred until it is fully parsed and displayed before execution, only valid for external scripts. <script defer= "defer" type= "Text/javascript" </script>

An alternative attribute of type:language that represents the content type of the scripting language that the code is written in (often referred to as the MIME type. The MIME type that is transferred on the server is usually application/x-javascript, but setting this value in type may cause the script to be ignored and can be used in non-IE browsers application/ JavaScript and Application/ecmascript, this property is not required.

The difference between defer and async: Once async is downloaded, it starts, before window.onload, which means that <script> with Async does not execute in the order in which they appear in the page, If the scripts are dependent on each other and are related to the order of execution (workaround: AMD), the problem will occur, with defer being performed in the order in which they appear in the page before the Domcontentloaded event.

Recommendation: Asynchronous scripts do not modify the DOM during loading, and asynchronous scripts execute before the Load event, but may be executed before or after the domcontentloaded event is triggered.

Note: If the <script> tag is nested in the page, the <script> can not appear in the code, and if it is to be escaped with "\", the JS interpreter in the browser will appear in the Code </script> Treated as an end tag.

function A () {Console.log ("<\/script>");}

A ();

If the code does not contain defer and async two properties, the browser will be in the order of <script>, if the <script> tag in the

<body>
<p> Embark, Bainianshuren </p>

<script type= "Text/javascript" src= "1.js" ></script>
<script type= "Text/javascript" src= "2.js" ></script>
</body>

2. Delay:

<title> Untitled Document </title>
<script type= "Text/javascript" defer= "defer" src= "1.js" ></script>
<script type= "Text/javascript" defer= "defer" src= "2.js" ></script>

Add defer is to tell the browser to download immediately and defer execution. Deferred to the

A way to ensure that the same code can be executed in XHTML: Add CDATA Fragments to your code, most browsers do not support CDATA fragments, and can be resolved with annotations, as in the following code

XHTML mode is triggered when the MIME type of the page is specified as "Application/xhtml+xml", and not all browsers support this way of providing XHTML documentation.

<script type= "Text/javascript" >
<! [cdata[
function Compare (A, B)
{
if (a<b)
{
document.write ("A less than B");
}
else if (a>b)
{document.write ("a greater than B");}
Else
{
document.write ("a equals B");
}
}
]]>
Compare (4,2);
</script>

Benefits of using external JS files:

1) can be maintained

2) cacheable: The browser can cache all external JS files linked according to the specific settings.

3) Adapt xhtml:xhtml and HTML to include external files in the same way.

<noscript>--------------------------------When the browser does not support JS script or support JS script but is not enabled when the script is invalid.
<p> Browser does not support or does not enable javascript</p>
</noscript>

3, JS strictly case-sensitive: such as typeof is a keyword, and typeof can be a variable.

ECMAScript switch to strict mode:

Add use strict to the top of the code

function A () {
"Use strict";//This code is a compilation instruction that tells the JS engine to switch to strict mode for execution.
}

Reserved words:

4. Variables:

function Test () {

Message= "HI"; Global variable, message has a value, as long as test () is called once, the variable message has meaning, but not easy to maintain

}

Test ();

Console.log (message);

The following is a local variable

function Test () {

var message= "HI"; Local variable, message is undefined

}-------Local variables have been destroyed.

Test ();

Console.log (message);

typeof: is an operator rather than a function that detects the data type of a variable

A function is an object, not a data type.

For non-declared variables can only be typeof operation, if you do other operations will be an error.

Using the typeof operation on uninitialized and undeclared variables returns undefined.

The null value is an empty object pointer, and if you define a variable to hold the object, initialize the variable to null.

Number type: the first digit of the octal literal value must be 0, if the literal value exceeds the range, the leading 0 is ignored, and the 16-bit literal value must be 0x.

Saving floating-point values requires twice times the amount of memory space to hold the integer value.

The Isfinite () function to test whether between the maximum and minimum values.

IsNaN (): accepts a parameter to determine if the parameter is "not a number" and this is also appropriate for the object.

There are 3 functions that convert a non-numeric value to a numeric value, number ()--can be used for any data type (including objects), parseint (), parsefloat (), and two for converting a string to a numeric value.

Console.log (Number ("DFASFDSA"));---------------NaN If the string contains numbers, floats, and hexadecimal conversions to the corresponding values

Console.log (number (false));----------------------0

Console.log (number (true));-----------------------1

Console.log (number (4545));----------------------4545

Console.log (number ());----------------------------0

Console.log (number (undefined));-----------------NaN

var num1=parseint ("54dfdsa"); Console.log (NUM1);--------------------54

var num1=parseint (""); Console.log (NUM1);------------------------------NaN

var num2=parseint ("0xaf"); Console.log (num2);----------------------175

parseint () and parsefloat () are parsed from characters, the first decimal point in the character is valid, but the second decimal point is invalid.

Once the string in the ECMAScript is created, it cannot be changed, and the original string is destroyed if it is to be changed, and then the variable is filled with the new value.

Only null and undefined do not have the ToString () method in the JS data type, and ToString () can pass a cardinality, such as:

var num=10;console.log (num.tostring ());------------------------------output to a

String () follows the following conversion principles:

If the value has the ToString () method, the method is called (with no arguments) and the corresponding result is returned.

If the value is null, the *null* is returned.

If the value is undefined, the *undefined* is returned.

Object: is a set of data-plus functions.

Each instance of object has the following properties and methods:

Constructor: Holds the function used to create the current object, and for Var ob=new objec () The constructor constructor is object ().

hasOwnProperty (PropertyName): Used to check whether a given property exists in the current object instance, not in the instance's prototype. The property name (PropertyName), which is the parameter, must be specified as a string.

propertyIsEnumerable (PropertyName): Used to check whether a given property can be enumerated using the For-in statement. The property name as a parameter must be specified in the form of a string.

Tolocationstring (): 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.

All objects have these basic methods and properties.

Predecessor operator: First plus minus re-assignment, ++age equivalent to Age=age+1

Post operators: Increment and decrement operations are performed only after the statement that contains them is evaluated.

For example: Var num1=2;var num2=20;console.log (num1--+num2), Console.log (num1+num2), output 22 and 21 because the first NUM1 is the original value used (2), The second num1 is used after the decrement value (1)

When applied to different values, the increment and decrement operators follow the following rules:

1) When a valid numeric string is included, it is converted to a number, plus minus, and the string variable becomes a numeric variable.

2) when applied to a string that does not contain a number, sets the value of the variable to nan and the string variable to a numeric variable.

3) when applied to a Boolean (false) value, it is converted to 0 before the add and subtract operation.

4) apply to True when converted to 1 and then add and subtract operations.

5) when applied to floating-point numbers, add and subtract operations directly.

6) When applied to an object, first call the object's valueof () method to get an actionable value, and if the result is Nan, then use the preceding rule after calling the ToString () method.

Bit operation:

1) Bitwise NON ~ (NOT): Returns the inverse of the numeric value, the essence of the bitwise non is the operand and negative minus 1, the advantage is fast

2) Bitwise AND &: Only the corresponding bits of the two operands are 1 o'clock to return 1, and any one is 0, and the result is 0.

3) Bitwise OR | (OR): One is 1 of the case returns 1, only two bits are 0 o'clock to return 0

4) Bitwise XOR (XOR): Only one is returned at 1 o'clock and 1 if two bits are 1 or 0 returns 0

5) move left <<: Moves the specified number of digits to the left, and the left shift does not affect the sign bit of the operand.

6) Signed right shift >>: Reserved sign bit, opposite to left shift operation.

7) Unsigned right shift >>>: For positive numbers, the unsigned right shift is the same as a signed right shift; The unsigned right shift is filled with 0 bits, and the signed right shift is filled with a sign bit. The unsigned right shift operator treats the binary of the negative number as a positive binary code, because the negative number is represented by its absolute twos complement, so the unsigned right shift results are very large.

8) Boolean operator: Non, with, or

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 0

Returns False if the operand is not 0

If the operand is null, NaN, undefined returns true

Logic and operations are short-circuiting operations, and if the first operand determines the result, then the second operand is not evaluated.

Operational rules for logic and operations:

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 result of the first operand is true.

If the two operands are objects, the second operand is returned.

If one of the operands is null, undefined, and nan, then null, Undefined, and Nan itself are returned.

Logical OR short-circuit operation

Logical OR operational rules:

Returns the first operand if the first operand is an object

If the evaluation result of the first operand is false, the second operand is returned.

If one of the operands is null, undefined, and nan, then null, Undefined, and Nan itself are returned.

If the two operands are objects, the first operand is returned.

Rules for multiplication operators:

The division operator rules in cases where special values are handled:

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 use the rule above.

To find a model:

Addition Operator:

Subtraction Operator:

Relational operator (<,<=,>,>=):

Equality operators:

Console.log (undefined==null);-----------------------------------------true

Console.log (undefined===null);----------------------------------------false

It is recommended to use congruent and non-congruent

Conditional operator: Var num= (8>0)? " 8 greater than 0 ":" 8 less than 0 "; Console.log (num);

Statement:

1) if.......else If......else If......else

2) Do-while: Before the expression is evaluated, the code in the loop body is executed at least once.

3) While: The export condition is evaluated before the code in the loop body executes.

4) for

5) for-in

for (var propname in window) {console.log (propname);}

The for-in statement throws an error if the value of the variable is null or undefined. Therefore, before the loop, you must first determine whether the value of the variable is null or undefined.

6) Break and Continue statement: Exit the loop immediately, the difference is that the break executes the loop body behind, the current loop body is not executed, and the continue will continue to execute from the top of the loop.

var num=0;
for (Var i=1;i<10;i++)
{
if (i%5==0) {
Break
}
num++;
}
Console.log (num);-----------------------the result is 4.

If you change the break to continue the result is 8.

7) With statement: The scope of the code is placed in a specific object. Syntax errors are reported in strict mode with the WITH statement.

8) switch statement:

Add a break to avoid situations where multiple case codes are executed at the same time

switch ("Hello World")----------------here is the congruent operator used, so the type conversion does not occur.
{
Case ' Hello ' + ' world ':
Console.log ("Congratulations found");
Break
Case "Lili":
Console.log ("not Found");
Break

Default

Console.log ("Keep trying Oh");

}

7) Function: A function can contain multiple return statements, and a return statement can have no return value.

function sum (A, B)

{

return a+b;

Console.log ("Hello");

}

SUM (52,34);----This function exits after the return is executed, and the subsequent code is never executed.

Functions with multiple return statements:

function Sub (A, b)

{

if (a<b)

Return-(B-A);

Else

return a-B;

}

Sub (2,8);

The following code returns the undefined value, which is used when the function needs to be terminated prematurely and no return value is required.

function Say (name,message) {

Return

Console.log (Name+message);

}

Say ("Marry", "Hello World");

Parameters of the function: ECMAScript does not care about passing a few arguments, even if the defined function receives two parameters, it is not necessarily necessary to pass two parameters when calling, because ECMAScript internally is the parameter as an array to represent. The function receives this array at all times. You can use the arguments object to access a parameter array to get each parameter passed to the function. Use Arguments[0], argument[1] to represent and access parameters.

For example:

function Add ()
{
return arguments[0]+arguments[1];
}
Add (3,8);

The length of the arguments object is determined by the parameters passed in. It is not determined by the number of named arguments when the function is defined, and named arguments that do not pass the value are automatically assigned the undefined value, which is the same as defining no initialization. All parameters in ECMAScript are passed as values, not reference pass parameters.

Overloading: Because there is no characteristic of function signatures, there is no real overload in ECMAScript, which mimics the overloads of the method by examining the type and number of incoming parameters and reacting differently.

You do not need to specify a function value for a function, because any function returns any value at any time.

JS Advanced Programming Third Edition---

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.