1, what is JavaScript?
(1) HTML is just a markup language that describes the appearance of a Web page, there is no calculation, judging ability, if all calculations, judgments (such as the decision to determine whether the text box is empty, to determine whether the password is entered in the same two times) store on the server side of the page will be very slow, use it is difficult to use, the pressure on the server is very large, Therefore, it is required to perform some simple operations in the browser to determine that JavaScript is a language that is executed on the browser side.
(2) JavaScript and Java have no direct relationship, the only relationship is JavaScript formerly known as LiveScript, and later learned some of the Java features, upgraded to Javascript,javascript sometimes referred to as JS.
(3) JavaScript is an explanatory language that can be executed at any time without compiling, so that even if there are errors in syntax, the part that does not have grammatical errors can be executed correctly.
The development environment of JS
(1) The automatic completion of javascript,jqery in VS.
(2) JS is a very flexible dynamic language, not as strict as C # static language.
JS Introduction
(1)
Copy Code code as follows:
<script type= "Text/javascript" >
Alert (new Date (). tolocaledatestring ());
</script>
(2) JavaScript code into the <script> tag,<script> can be placed in any position (3) Put into the (4) In addition to the page can be declared JavaScript, you can also write JavaScript in a separate JS file, and then introduced in the page: <script src= "Common.js" type= "Text/javascript" ></script>. The advantage of declaring to a separate JS file is that multiple pages can also be shared, reducing network traffic.
Event
(1) <a href= "Javascript:alert (' Hello ')" > click me </a>
<a href= "javascript:void (0)" > I don't eject anything </a><br/>
<a href= "jsoop.htm" onclick= "alert" (' Are you sure you want to jump? ') ' > click me </a>
(2) JavaScript also has the concept of events, when the button is clicked
1) <input type= "button" value= "click Me" onclick= "alert (' finally clicked on Me ')"/>
2 only JavaScript in the hyperlink's href requires "javascript:" Because he is not an event, but rather "javascript:" as "http:", "ftp:", "thunder://", ed2k://, mailto://the same network protocol, referred to the JS resolution engine processing, only the href in this special column.
JS variable
(1) JavaScript can either use double quotes to declare strings, or use single quotes to declare strings, primarily for easy and HTML integration, avoiding the hassle of escape characters.
(2) var i=10; Declare a variable, first name I, point 10 to this integer, once pointing to 10,i is the int type, alert (i);
(3) There are two kinds of null,underfined in JavaScript, NULL indicates that the value of the variable is NULL, underfined indicates that the variable has not pointed to any object, uninitialized.
(4) JavaScript is a weak type and cannot be used to represent a variable: int i=10. Only through Var i=10, declaring variables, unlike var in C #, not type inference in C #.
(5) JavaScript can also be used without var to declare variables, directly, such variables are "global variables", so unless you do want to use global variables, otherwise it is best to add var.
(6) JS is dynamic type, so var i=10;i= "abc" is legal.
Javascript
(1)
Copy Code code as follows:
var sum = 0;
for (var i = 0; I <= i++) {
sum = sum + i;
}
alert (sum);
(2) If the code in JavaScript has syntax errors, the browser will pop up the error message, look up the wrong information can help troubleshoot errors.
(3) JavaScript debugging, using VS can be very convenient for JavaScript debugging, debugging need to pay attention to several points:
1 IE debugging options to open, Internet Options-Advanced, remove the "Disable script debugging" check before.
2) to run the interface in a debug manner.
3 set breakpoints, monitoring variables and other operations like C #.
Judgment variable Initialization
(1) in JavaScript to determine the variable, the parameters are initialized three ways.
Copy Code code as follows:
var R;
if (r = = null) {if (typeof (r) = = "undefined") {if (!x) {
Alert ("null"); Alert ("undefined"); Alert ("No X");
}
}
}
Note: The last method is recommended
Declaration of functions
(1) methods of declaring functions in javascript:
Copy Code code as follows:
function Add (I1, I2) {
return i1 + i2;
}
(2) There is no need to declare a return value type, a parameter type, and a function definition to start
Copy Code code as follows:
var r = Add (10, 20);
Alert (R);
var r = Add ("Tom,", "Hello");
Alert (R);
(3) JavaScript does not require all paths to have return values, as in C #.
anonymous functions
(1)
Copy Code code as follows:
var f1 = function sum (i1, I2) {
return i1 + i2;
}
Alert (F1 (10, 20));
(2) similar to anonymous functions in C #.
(3) This anonymous usage is particularly much used in jquery.
(4)
Copy Code code as follows:
Alert (function sum (i1, I2) {
return i1 + i2;
} (100, 10));
Note: Anonymous functions in C # are invoked using delegates.
JS Object-Oriented Foundation
(1) JavaScript does not have the class syntax, is uses the function closure (closure) to simulate, the following explanation or uses in C # class, the constructor and so on concept, the JavaScript string,date and so on "class" All is called "the object", A class is declared in JavaScript (class is not a class, is an object).
(2)
Copy Code code as follows:
function person (name, age) {//Declaration of functions as a class
This. name = name;
This. Age = age;
This. SayHello = function () {
Alert ("Hello, I am" + this.) Name + ", I am this year:" + this. Age + "old");
}
}
var p1 = new Person ("Han Guanlong", "23");
P1. SayHello ();
(3) The class name must be declared, and the function person (name,age) can be regarded as a declaration constructor, name,age these properties are also dynamically added by the user.
Array () object
(1) The array objects in JavaScript are arrays, first a dynamic array, and a super complex like an array arraylist,hashtable in C #.
(2)
Copy Code code as follows:
var names = new Array ();
Names[0] = "Han Guanlong";
NAMES[1] = "get";
NAMES[2] = "said";
for (var i = 0; i < names.length; i++) {
Alert (Names[i]);
}
(3) No need to make a predetermined size, dynamic.
Array () Exercise 1
(1) Array practice to find the maximum value in an array.
Copy Code code 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) Reverses the order of elements of an array of strings, {3,9,5,34,54}{54,34.5.9.3}. Do not use the inverse function in JavaScript, hint: I and length-i-1 are exchanged to define functions.
Copy Code code 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) The output of a string array as a | split form, such as: Han Guanlong | try | The order. Instead of using the Join function in JavaScript, Arr.join (1) divides the array into a string using a split linked.
Copy Code code 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 Guanlong";
ARR[1] = "Try";
ARR[2] = "Order";
Alert (Myjoin (arr));
The second method
Alert (Arr.join ("|"));
</script>
Dictionary usage of array
(1) JS in the array is a baby, not only an array, or a dictionary, or a stack.
(2)
Copy Code code as follows:
var names = new Array ();
Names["man"] = "ren";
names["buckle"] = "Kou";
names["Hand"] = "Shou";
Alert (names["person"]);
alert (names person);
For (var k in names) {
Alert (k);
}
(3) as hashtable,dictionary, and as efficient as they are.
Simplified declaration of Array ()
(1) Array can also have a simplified way
var arr=[3,4,5,6,7]; Normal array initialization
This kind of array can be regarded as names["person"]= "Ren", the special case, namely key is 0,1,2,3,4,5
(2) Simplified way of creating dictionary style
var arr={"Tom" =30, "jim=" 30};
arrays, for and other
(1) for array-style arrays, you can use the Join method to stitch to a string.
Copy Code code as follows:
var arr = ["Tom", "Jim", "Kencery"];
Alert (Arr.join (",")); A join in JS is a method of array, unlike a string in. net
(2) A For loop can be used like a foreach in C #.
Copy Code code as follows:
for (var e in document) {
Alert (e);
}