Basic Javascript Lesson 1 [Reading Notes]

Source: Internet
Author: User
Tags variable scope

These things are very basic, and you can understand them at a Glance. Some functions need to be remembered. Recently, I 've basically finished reading Javascript DOM programming art. I don't know much about Jquery. Let's take a look at it. Because no source file has a comprehensive instance is not made, some can send me a, thank you ha boychik.pure@gmail.com

Today is the basic knowledge and will be updated later. The day of success will come after all. Please look forward to it :)

1. javascript Components

  • ECMAscript ---- mainly used for browser parsing, for example, defining a variable, function ---- almost all browsers are compatible
  • BOM (browser object model)-browser object model, for browser operations, such as pop-up windows, mobile browser location, printing, etc.-almost incompatible, but rarely used.
  • DOM (document object model) ---- document object model, Operation page functions, such as getting the document's div, class, adding elements, etc. ---- most of them are compatible

2. Variable type

  • Number
  • String
  • Boolean
  • Object
  • Function
  • Undefined
  • Suggestion: a variable stores only one type of data.

3. Object (everything in js is an object)

Basic types: number, string, boolean, and undefined. They are simple and simple (these are not objects)

Composite Type: object, array, date object, composite type is composed of some basic types, including some miscellaneous.

4. Data type conversion ---- display conversion/implicit conversion

1 <script> 2 window. onload = function () 3 {4 var oTxt1 = document. getElementById ('txt1'); 5 var oTxt2 = document. getElementById ('txt2'); 6 var oBtn = document. getElementById ('btn1 '); 7 8 oBtn. onclick = function () 9 {10 alert (parseInt (oTxt1.value) + parseInt (oTxt2.value); 11}; 12 }; 13 </script> 14 

After optimization, if the input is not a numerical value, a prompt is displayed.

1 <script> 2 window. onload = function () 3 {4 var oTxt1 = document. getElementById ('txt1'); 5 var oTxt2 = document. getElementById ('txt2'); 6 var oBtn = document. getElementById ('btn1 '); 7 8 oBtn. onclick = function () 9 {10 var num = parseInt (oTxt1.value) + parseInt (oTxt2.value); 11 12 if (isNaN (num )) 13 {14 alert ('error you entered '); 15} 16 else17 {18 alert (num); 19} 20}; 21}; 22 </script>

 

If you enter two different numbers in the text box, such as 2 and 3, the Click Event of the button will pop up 23 instead of 5, because the content in the text box defaults to string rather than numeric data. The parseInt function can be used to convert the string type.

1 <script>2     var a='13ab';3     alert(parseInt(a));4 </script>

To be accurate, the parseInt function extracts the preceding number. If a string is encountered, it will not continue to return the value.

1 <script>2     var a='ab';3     alert(parseInt(a));4 </script>

If so, there will be NaN (not a number), which means it is not a number.

1 <script>2     var a='12.6';3     alert(parseInt(a));4 </script>

If so, the number 12 is displayed, and the decimal point is omitted.

1 <script>2     var a='12.6';3     alert(parseFloat(a));4 </script>

ParseInt's brother parseFloat function.

NaN (this is a magic thing. He is not equal to himself. It may be different to some extent)

JavaScript outputs Number. NaN in the form of NaN. Note that the result of comparing NaN with other values is always not equal, including itself. Therefore, you cannot compare it with Number. NaN to check whether a value is a Number. Instead, you can only call isNaN () to compare it.

1 <script>2     var a=parseInt('ab');3     var b=parseInt('ab');4     alert(a==b);5 </script>
1 <script>2     var a=parseInt('ab');3     var b=parseInt('ab');4     alert(a==b);5 </script>

Both of the results are flase. The above are display conversions.

Implicit conversion:

1 <script>2     var a='2';3     var b=2;4     alert(a==b);5 </script>

The running result is true. This is an example of implicit conversion. It compares and adds the Data Types on both sides.

1 <script>2     var a='2';3     var b='2';4     alert(a-b);5 </script>

If it is a minus sign, the result is 0. If it is a plus sign, the result is 22. Only addition cannot be converted. multiplication, division, and subtraction are supported.

About =

Equality means not only equal content, but also equal types.

5. variable scope

  • Global variable --- devil =!
  • The existence of global variables is mainly due to the following reasons: 1. Using global variables will occupy more memory (because of its long life cycle), but today when the computer is configured very high, this is not a problem. Unless you use a global variable of a huge object, you must avoid it. 2. The program runs faster with global variables (because the memory does not need to be reallocated), and it cannot be much faster now. 3. For the namespace pollution of local variables, this can be avoided without using too many variables. 4. When the name of a global variable is the same as that of a local variable, the global variable is blocked. In short, global variables can be used. However, when using global variables, make sure that their names are easy to understand and cannot be too short to avoid namespace pollution; avoid using global variables of large objects. (This section is taken from Baidu encyclopedia)
  • Local variable --- Closure

I won't explain it here. There are still a lot of details about closures. Come here today, take a bath and bed, good night ~

  • 1 <script> 2 function aa () {// parent function 3 var a = 1; 4 function bb () {// subfunction, you can get old. 5 alert (a); 6}; 7 bb (); 8}; 9 aa (); 10 </script>
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.