JavaScript Study Notes record my journey

Source: Internet
Author: User

1. What is JavaScript?
(1) HTML is only a markup language that describes the appearance of a Web page. It does not have computing and judgment capabilities. If all calculations are performed, it can be used to determine whether the text box is empty or whether the two passwords are consistent) if the store is placed on the server side for execution, the webpage page will be very slow and difficult to use, and the pressure on the server will also be high, therefore, you must be able to execute some simple operations in the browser to determine that JavaScript is a language executed on the browser.
(2) There is no direct relationship between JavaScript and Java. The only link is JavaScript, formerly known as LiveScript. Later, it learned some Java features and upgraded it to JavaScript. JavaScript is sometimes referred to as JS.
(3) JavaScript is an explanatory language and can be executed at any time without compilation. In this way, even if there is a syntax error, the part without syntax errors can still be correctly executed.
JS Development Environment
(1) Automatic completion of JavaScript and Jqery in.
(2) JS is a very flexible dynamic language, not as rigorous as static languages such as C.
JS entry
(1)

Copy codeThe Code is as follows: <script type = "text/javascript">
Alert (new Date (). toLocaleDateString ());
</Script>

(2) Place JavaScript code in the <script> tag, <script> can be placed in any location such as (3) The <script> placed in (4) In addition to declaring JavaScript on the page, you can also write JavaScript in a separate JS file and introduce <script src = "common. js "type =" text/javascript "> </script>. The benefit of declaring a separate JS file is that multiple pages can also be shared to reduce network traffic.
Event
(1) <a href = "javascript: alert ('hello')"> click me </a>
<A href = "javascript: void (0)"> I won't bring up anything </a> <br/>
<A href = "JSoop.htm" onclick = "alert ('Are you sure you want to jump? ') "> Click me </a>
(2) events also exist in JavaScript. When a button is clicked
1) <input type = "button" value = "Click me" onclick = "alert ('Finally clicked me')"/>
2) "JavaScript:" is only required for the JavaScript in the href of the hyperlink, because it is not an event but a JavaScript: 'Look at the image "http:", "ftp :", "thunder: //", ed2k: //, mailto: // the same network protocol, which is handled by the JS parsing engine, only this column in href.
JS Variables
(1) In JavaScript, strings can be declared using double quotation marks or single quotes. It is mainly used to facilitate integration with HTML and avoid the trouble of escape characters.
(2) var I = 10; // declare a variable named I, pointing to the integer 10. Once it points to 10, I is of the int type, alert (I );
(3) There are two types of JavaScript: null and underfined. null indicates that the value of the variable is null. underfined indicates that the variable has not pointed to any object and has not been initialized.
(4) JavaScript is weak, indicating that the variable cannot: int I = 10. variables can only be declared through var I = 10; they are different from var in C #, rather than type inference in C.
(5) You can directly use the variable without var declaration in JavaScript. This variable is a "global variable", so it is best to add var when using it unless you really want to use a global variable.
(6) JavaScript is dynamic, so var I = 10; I = "abc" is legal.
JavaScript
(1)Copy codeThe Code is as follows: var sum = 0;
For (var I = 0; I <= 100; I ++ ){
Sum = sum + I;
}
Alert (sum );

(2) If the JavaScript code has a syntax error, the browser will pop up an error message to view the error information to help troubleshoot the error.
(3) debugging of JavaScript. You can use VS to debug JavaScript conveniently. Pay attention to the following points during debugging:
1) enable debugging options for IE, and enable Internet Options-advanced. deselect the check box before "Disable script debugging.
2) run the interface in debug mode.
3) set breakpoints, monitor variables, and perform the same operations as C.
Variable Initialization
(1) determine whether to initialize the variables and parameters in JavaScript.Copy codeThe Code is as follows: var r;
If (r = null) {if (typeof (r) = "undefined") {if (! X ){
Alert ("null"); alert ("undefined"); alert ("not X ");
}
}
}

Note: The last method is recommended.
Function Declaration
(1) methods for declaring functions in JavaScript:Copy codeThe Code is as follows: function Add (i1, i2 ){
Return i1 + i2;
}

(2) the return value type, parameter type, and function definition must start with function.Copy codeCode: var r = Add (10, 20 );
Alert (r );
Var r = Add ("tom,", "hello ");
Alert (r );

(3) JavaScript does not require that all paths have return values as in C.
Anonymous Functions
(1)Copy codeCode: var f1 = function sum (i1, i2 ){
Return i1 + i2;
}
Alert (f1 (10, 20 ));

(2) It is similar to the anonymous function in C.
(3) This anonymous usage is especially used in Jquery.
(4)Copy codeThe Code is as follows: alert (function sum (i1, i2 ){
Return i1 + i2;
} (100, 10 ));

Note: anonymous functions in C # are called using delegation.
JS object-oriented Basics
(1) JavaScript does not have the class syntax. It is simulated using the function closure (closure). In the following illustration, we will use the class in C # And the constructor concepts, "classes" such as string and date in JavaScript are called "objects", and declared classes in JavaScript (classes are not classes, but objects ).
(2)Copy codeCode: function Person (name, age) {// declare a function as a class
This. Name = name;
This. Age = age;
This. SayHello = function (){
Alert ("Hello, I am" + this. Name + ", I am:" + this. Age + "years old ");
}
}
Var p1 = new Person ("Han Yinglong", "23 ");
P1.SayHello ();

(3) the class name must be declared. function Person (Name, age) can be regarded as a declared constructor, name, and Age. These attributes are also dynamically added by the user.
Array () object
(1) The Array object in JavaScript is an Array. It is a dynamic Array first and a super complex such as the Array ArrayList and Hashtable in C.
(2)Copy codeThe Code is as follows: var names = new Array ();
Names [0] = "Han Yinglong ";
Names [1] = "get ";
Names [2] = "said ";
For (var I = 0; I <names. length; I ++ ){
Alert (names [I]);
}

(3) There is no need to specify the size and dynamics in advance.
Array () Exercise 1
(1) Use Array to obtain the maximum value in an Array.Copy codeThe Code is as follows: <script type = "text/javascript">
Function MyMax (arr ){
Var max = arr [0];
For (var I = 0; I <arr. length; I ++ ){
If (arr [I]> max ){
Max = arr [I];
}}
Return max;
}
Var arr = new Array ();
Arr [0] = 20;
Arr [1] = 10;
Arr [2] = 34;
Alert (MyMax (arr ));
</Script>

Array () Exercise 2
(1) reverse the order of elements in a string array, {3, 9, 5, 34, 54} {54, 34.5.9.3 }. Do not use reverse functions in JavaScript, prompting that the I and the length-i-1 are exchanged, defining the function.Copy codeThe Code is as follows: <script type = "text/javascript">
Function MyReverse (arr ){
For (var I = 0; I <arr. length/2; I ++ ){
Var temp = arr [I];
Arr [I] = arr [arr. length-I-1];
Arr [arr. length-I-1] = temp;
}
}
Var arr = new Array ();
Arr [0] = "3 ";
Arr [1] = "9 ";
Arr [2] = "5 ";
Arr [3] = "34 ";
Arr [4] = "54 ";
Alert (arr );
MyReverse (arr );
Alert (arr );

Array () Exercise 3
(1) Output A string array in the | split format, for example, Han Yinglong | try | order. Do not use the Join function in JavaScript. arr. join (1) concatenates an array with a separator into a string.Copy codeThe Code is as follows: <script type = "text/javascript">
Function MyJoin (arr ){
If (arr. length <= 0 ){
Return;
}
Var s = arr [0];
For (var I = 1; I <arr. length; I ++ ){
S = s + "|" + arr [I];
}
Return s;
}
Var arr = new Array ();
Arr [0] = "Han Yinglong ";
Arr [1] = "try ";
Arr [2] = "order ";
Alert (MyJoin (arr ));
// Method 2
Alert (arr. join ("| "));
</Script>

Array dictionary usage
(1) Array in JS is a baby, not only an Array, a Dictionary, but also a Stack.
(2)Copy codeThe Code is as follows: var names = new Array ();
Names ["persons"] = "ren ";
Names ["Buckle"] = "kou ";
Names ["hand"] = "shou ";
Alert (names ["persons"]);
Alert (names. persons );
For (var k in names ){
Alert (k );
}

(3) It is used like Hashtable and Dictionary, and it is as efficient as they are.
Simplified declaration of Array ()
(1) Array can also be simplified.
Var arr = [3, 4, 5, 6, 7]; // initialize a Normal Array
This array can be seen as a special case of names ["person"] = "ren"; that is, keys are 0, 1, 2, 3, 4, 5.
(2) Simplified dictionary style Creation
Var arr = {"tom" = 30, "jim =" 30 };
Array, for, and others
(1) For Array-style arrays, join can be used to concatenate them into strings.Copy codeThe Code is as follows: var arr = ["tom", "jim", "kencery"];
Alert (arr. join (","); // join in JS is an array method, unlike the string method in. net.

(2) The for loop can be used like foreach in C.Copy codeThe Code is as follows: for (var e in document ){
Alert (e );
}

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.