Javascript study Note 3

Source: Internet
Author: User

Javascript study Note 3

1. Javascript composition:

ECMAScript-the core of JavaScript, describes the basic syntax and objects of the language.
DOM (Document Object Model) -- The Document Object Model describes The methods and interfaces that act on webpage content. Document operations, such:

 

 var lis = document.getElementsByTagName('li');
BOM (Browser Object Model): The Browser Object Model describes The methods and interfaces for interacting with The Browser. Window operations, such:

 

 

window.onload = function () {        }

 

2. "=" and "=" Operators (this is a detailed introduction from zhihuilin. Original article address: Click to open the link)

I. Strictly equal operator =

 

= It is called Strict Equals Operator. If the expression a = B is used, the actual operation process is as follows:
Calculates the result of expression a and saves it to the lref variable.
Store the GetValue (lref) result to the lval variable
Calculates the result of expression B and stores the rref variable.
Store the GetValue (rref) result to the rval variable.
Run Strict isolation ity ComparisonThe algorithm judges rval === lval and returns the result directly here Strict isolation ity ComparisonThe algorithm is critical. If x = y is to be calculated, the process is as follows:

1. If Type (x) and Type (y) are different, false is returned.

2. If Type (x) is Undefined, true is returned.

3. If Type (x) is Null, true is returned.

4. If Type (x) is Number, the following judgment logic is entered.

4.1. If x is NaN, false is returned.

4.2. If y is NaN, false is returned.

4.3. If the numeric value of x is equal to y, true is returned.

4.4. If x is + 0 and y is-0, true is returned.

4.5. If x is-0 and y is + 0, true is returned.

4.6. Return false

5. if Type (x) is String, true is returned only when x and y are in the same sequence (the length is equal and the characters at each position are the same). Otherwise, false is returned.

6. If Type (x) is Boolean, true is returned if x and y are both true or false; otherwise, false is returned.

7. If x and y reference the same object, true is returned; otherwise, false is returned.


II. Implementation of equal operator =

Well, after you understand the implementation of =, let's take a look at the implementation of = and compare them?
= It is called Equals Operator (note that there is no Strict). If there is an expression a = B, its actual operation process is as follows:
  1. Calculate the result of expression a, store the result of GetValue (lref) into the lref variable, calculate the result of expression B, and store the result of GetValue (rref) into the rref variable) stored in the rval variable for execution.Abstract Equality ComparisonThe algorithm judges rval = lval and returns the result directly.
    Note that the first four steps are exactly the same as ===. But 5 is different. For =, the call isStrict isolation ity ComparisonAlgorithm, but = is calledAbstract Equality ComparisonAlgorithm. Although there is only one word difference, there is a qualitative difference. Let's take a look at how it is implemented.

    Assume that x = y is to be calculated,Abstract Equality ComparisonThe calculation process is as follows (lengthy, but every step is simple)

    1. If Type (x) and Type (y) are the same

    1.1. If Type (x) is Undefined, true is returned.

    1.2. If Type (x) is Null, true is returned.

    1.3. If Type (x) is Number

    1.3.1. If x is NaN, false is returned.

    1.3.2. If y is NaN, false is returned.

    1.3.3. If the value of x is the same as that of y, true is returned.

    1.3.4. If x is + 0 and y is-0, true is returned.

    1.3.5. If x is-0 and y is + 0, true is returned.

    1.3.6. Return false

    1.4. if Type (x) is String, true is returned only when x and y are in the same sequence (the length is equal and the characters at each position are the same). Otherwise, false is returned.

    1.5. If Type (x) is Boolean, true is returned if x and y are both true or false; otherwise, false is returned.

    1.6. If x and y reference the same object, true is returned; otherwise, false is returned.

    2. If x is null and y is undefined, true is returned.

    3. If x is undefined and y is null, true is returned.

    4. If Type (x) is set to Number and Type (y) is set to String, the result of comparison with x = ToNumber (y) is returned.

    5. If Type (x) is String and Type (y) is Number, use the comparison result of ToNumber (x) = y as the return value.

    6. If Type (x) is Boolean, use the comparison result of ToNumber (x) = y as the return value.

    7. If Type (y) is Boolean, the return value is x = ToNumber (y ).

    8. If Type (x) is String or Number and Type (y) is Object, the return value is x = ToPrimitive (y ).

    9. If Type (x) is Object and Type (y) is String or Number, the return value is the comparison result of ToPrimitive (x) = y.

    10. Return false

    Iii. Summary

    From the above algorithm flow, we can see that a = B is the simplest. If a and B are of different types, false is returned. A = B is much more flexible. JavaScript will try to adjust the types of a and B. For example, if a is a string B as a number, the string is converted to a number and then compared with B. This simplifies the amount of code for programmers.

    3. Determine whether the JavaScript data is true or false

    Use if (value) to determine whether it is true or false:

    When the value is undefined, null, fasle, 0, null String (not defined using new String (), the value is false, and other conditions are true.

    4. for... in statement

    The for... in statement is used to perform cyclic operations on the attributes of an array or object.
    Each time the Code in the for... in loop is executed, an operation is performed on the elements or attributes of the array.
    Syntax:

     

    For (variable in object) {execute code here}
    "Variable" is used to specify a variable. The specified variable can be an array element or an object attribute.

     


     

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.