Home > Web Front-end > JS Tutorial > body text

JavaScript study notes record my journey_Basic knowledge

WBOY
Release: 2016-05-16 17:53:29
Original
998 people have browsed it

1. What is JavaScript?
(1) HTML is just a markup language that describes the appearance of a web page. It does not have the ability to calculate or judge. If all calculations and judgments are made (such as judging whether the text box is empty, judging whether two passwords have been entered) Consistent) If the store executes it on the server side, the web page will be very slow, difficult to use, and put a lot of pressure on the server. Therefore, it is required to perform some simple operations in the browser. To judge, JavaScript is a A language that executes on the browser side.
(2) JavaScript and Java have no direct relationship. The only relationship is that JavaScript was originally called LiveScript. Later, it absorbed some features of Java and was upgraded to JavaScript. JavaScript is sometimes referred to as JS.
(3) JavaScript is an interpreted language and can be executed at any time without compilation. In this way, even if there are grammatical errors, the parts without grammatical errors can still be executed correctly.
JS development environment
(1) JavaScript and Jqery auto-complete function in VS.
(2) JS is a very flexible dynamic language, not as rigorous as static languages ​​such as C#.
JS Getting Started
(1)

Copy code The code is as follows:

< ;script type="text/javascript">
alert(new Date().toLocaleDateString());


(2) JavaScript code is placed in In the <script> tag, <script> can be placed at any position such as <head>, <body>, and there can be more than one <script> tag. The alert function is a pop-up message window, and new Date() is to create an object of Date class, and the default value is the current time. <br>(3) The <script> placed in <head> has been run before the body is loaded, and the <script> written in the body is executed one by one as the page is loaded. <br>(4) In addition to declaring JavaScript in the page, you can also write JavaScript in a separate JS file and then introduce it into the page: <script src=”common.js” type=”text/javascript ”></script>. The advantage of declaring to a separate JS file is that multiple pages can be shared, reducing network traffic.
Event
(1) Click me
I won’t pop up anything

Click me
(2) JavaScript also has the concept of events, when the button is clicked
1)
2) Only the JavaScript in the href of the hyperlink requires "JavaScript:", because it is not an event, but treats "JavaScript:' like" http:", "ftp:", "thunder://", ed2k://, mailto:// are the same network protocols and are processed by the JS parsing engine. There is only this special column in href.
JS variable
(1) You can use double quotes or single quotes to declare strings in JavaScript, mainly to facilitate integration with HTML and avoid the trouble of escape characters.
(2) var i=10. ; //Declare a variable with the name i, pointing to the integer 10. Once it points to 10, i is of type int, alert(i);
(3) There are two types of null and underfined in JavaScript, and null represents the value of the variable. If it is empty, underfined means that the variable does not point to any object and has not been initialized.
(4) JavaScript is a weak type and cannot represent variables: int i=10. Variables can only be declared through var i=10; Unlike var in C#, it is not type inference like in C#
(5) In JavaScript, you can also use var directly instead of declaring variables. Such variables are "global variables", so unless you really want to use them globally. Variable, otherwise it is best to add var when using it.
(6) JS is dynamically typed, so var i=10;i="abc" is legal.
JavaScript
(1)
Copy code The code is as follows:

var sum = 0;
for (var i = 0; i <= 100; i ) {
sum = sum i;
}
alert(sum);

(2) If the code in JavaScript has syntax If an error occurs, the browser will pop up an error message. Checking the error message can help troubleshoot the error.
(3) JavaScript debugging, using VS can easily debug JavaScript. You need to pay attention to a few points when debugging:
1) IE debugging options should be turned on, Internet Options-Advanced, remove "Disable script debugging" Check the box in front of ".
2) Run the interface in debug mode.
3) Setting breakpoints, monitoring variables and other operations are the same as C#.
Judge variable initialization
(1) Three ways to judge whether variables and parameters are initialized in JavaScript.
Copy code The code is as follows:

var r;
if (r == null) { if (typeof (r) == "undefined") { if (!x) {
alert("null"); alert ("undefined"); alert("Not 🎜>(1) Method of declaring functions in JavaScript:



Copy code


The code is as follows:
function Add(i1, i2) { return i1 i2; } (2) There is no need to declare the return value type, parameter type, and the function definition starts with function



Copy code

The code is as follows:
(3) JavaScript does not require all All paths have return values.
Anonymous function
(1)



Copy code


The code is as follows:
var f1 = function sum(i1, i2) { return i1 i2; } alert(f1(10, 20));
(2) similar to C# anonymous function.
(3) This kind of anonymous usage is particularly common in Jquery.
(4)



Copy code


The code is as follows:
alert(function sum(i1 , i2) { return i1 i2; } (100, 10)); Note: Anonymous functions in C# are called using delegates.
JS object-oriented basics
(1) There is no class syntax in JavaScript, which is simulated by function closure. When explaining below, we still use concepts such as classes and constructors in C#. In JavaScript "Classes" such as string and date are called "objects", and classes are declared in JavaScript (classes are not classes, but objects).
(2)



Copy code


The code is as follows:
function Person(name, age ) { //Declare a function as a class using this.Name = name; this.Age = age; this.SayHello = function () { alert("Hello, I It is " this.Name ", my year is: " this.Age "year 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 declaring the constructor. Name and Age attributes are also dynamically added by the user.
Array() object
(1) The Array object in JavaScript is an array. First of all, it is a dynamic array, and it is a super complex like arrays ArrayList, Hashtable, etc. in C#.
(2)



Copy code


The 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) No need to pre-set the size, dynamic.
Array() Exercise 1
(1) Array exercise, find the maximum value in an array.



Copy code


The code is as follows:



Array( ) Exercise 2
(1) Reverse the order of the elements of a string array, {3,9,5,34,54}{54,34.5.9.3}. Do not use the reverse function in JavaScript. Tip: Swap the i-th and length-i-1 to define the function.




Copy code


The code is as follows:



Dictionary usage of Array
(1 ) Array in JS is a treasure, not only an array, but also a Dictionary and a Stack.
(2)
Copy code The code is as follows:

var names = new Array( );
names["人"] = "ren";
names[" buckle"] = "kou";
names["hand"] = "shou";
alert(names ["人"]);
alert(names.人);
for (var k in names) {
alert(k);
}

( 3) Use it like Hashtable and Dictionary, and it is as efficient as them.
Simplified declaration of Array()
(1) Array can also have a simplified way
var arr=[3,4,5,6,7]; //Ordinary array initialization
This Arrays can be seen as a special case of names["人"]="ren";, that is, the keys are 0,1,2,3,4,5
(2) Simplified creation method in dictionary style
var arr={”tom”=30,”jim=”30};
Array, for and others
(1) For array-style Array, you can use the join method to concatenate it into a string.
Copy code The code is as follows:

var arr = ["tom", "jim", "kencery"];
alert(arr.join(",")); //join in JS is an array method, unlike .net which is a string method

(2 ) for loop can be used like foreach in C#.
Copy code The code is as follows:

for (var e in document) {
alert (e);
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template