JavaScript He is a descriptive language, using JavaScript to be able to interact with the Web page better, the following cut into the topic to explain.
One. JavaScript
1. What is JavaScript
JavaScript is a descriptive language and is a scripting language that is object-based and event-driven (driven) and has security.
Summary of features of 2.JavaScript
JavaScript is used primarily to add interactive behavior to HTML pages.
JavaScript is a scripting language, and syntax is similar to java.
JavaScript is typically used to write client-side scripts.
JavaScript is an explanatory language.
The composition of the 3.JavaScript (we'll get to know it here, in the future if you know more about JS you will be exposed to)
Ecmscript Standard (Standard for all properties, methods and objects)
BOM Browser object Model (Browser object models): Interacting with HTML
DOM Document Object model: Accessing and manipulating HTML documents
4.JAVASCRIPT Basic Structure
<script language= "javascript" type= "Text/javascript" ><!--language= "JavaScript" Used to indicate that the language used is javascript-></script>
5.JavaScript Execution principle (Request/Response principle)
1. The browser client sends the request to the server side. (The address that the user entered in the browser's address bar)
2. Data processing: The server side processes a page that contains JavaScript.
3. Send the response: The server side will contain JavaScript HTML file processing page sent to the browser client, and then by the browser client from top to bottom parsing HTML tags and javascript tags, the page effect is presented to the user.
Two How to introduce JavaScript in a Web page 1. Use <script> tags. 2. Use an external JavaScript file.
You want to run JavaScript in multiple pages to achieve the same effect, usually by using an external file that is a. js file.
Files that refer to the. js extension in the file:
<script src= ". /1.js "></script>
3. Directly in the HTML tag
<input name= ' btn ' type= "button" value= "Pop-up message box" onclick= "Javascript:alert (" welcome You ");" />
Three. JavaScript core syntax
1. Declaration and assignment of variables
variables are declared with Var only, and the naming conventions of variables are similar to Java. Var Num=1;
Variables can be used directly in JavaScript without declaring them, but this usage is not recommended.
2. Data type
Undefined (undefined type)
Null (NULL type)
Number (numeric type)
String (String type)
Boolean (Boolean type)
The difference between 3.undefined and null
Null means "No object", that is, there should be no value at all. Typical uses are:
(1) As a function parameter, the parameter of the function is not an object.
(2) As the end point of the object prototype chain.
Object.getprototypeof (object.prototype)//null
Undefined means "missing value", that is, there should be a value here, but there is no definition. Typical uses are:
(1) The variable is declared, but when it is not assigned, it is equal to undefined.
(2) when calling a function, the argument that should be supplied is not provided, which equals undefined.
(3) The object does not have an assigned property, and the value of this property is undefined.
(4) When the function does not return a value, undefined is returned by default.
4. There are only 6 cases where the verdict is false.
null,false,undefined,0, "", NaN
5. Some common methods of string
ToString (); return string
Tolowercasee (); Replace the string with lowercase.
toUpperCase (); Convert a string to uppercase
CharAt (index); Returns the string at the specified position
IndexOf (Str,index); finds the position of the first occurrence of a specified string in a string
Substring (Index,index); Returns the string between Index1 and Index2 at the specified index (including index1 not included Index2)
Split (str); split a string into a character array
6. Three ways to create arrays and assign values to an array
num= Var (' 1 ', ' 2 ');
02.var num=new Array (2);
Num[0]=1;
num[1]=2;
03.var num=[' 1 ', ' 2 '];
common methods and properties of arrays
Length: Sets or returns the number of elements in the array
Join (): Puts all the speech speed of the array into a string, which is split by the delimiter.
Sort (): Sort an array
Push (): Adds one or more elements to the end of the array and puts back the new length.
<script type="Text/javascript">//01. Method One: Create an array and assign values to the array//var fruit = new Array (' Apple ', ' orange ', ' peach ', ' bananer '); //02. Mode Two: First create an array, and then assign a value to the array by subscript varFruits =NewArray (4); fruits[0] ='Apple'; fruits[1] ='Orange'; fruits[2] ='Peach'; fruits[3] ='Bananer'; //03 Way Three: And the way a similar just changed the symbol//var fruitss = new array[' Apple ', ' orange ', ' peach ', ' bananer ']; //04. Accessing data labeled 3 in the arraydocument.write (fruits[0]); //05 Use the Join method of an array to place the element in a string and split it with the specified delimiter varresult = Fruits.join (','); //06 sorting An array by the Sort methodFruits.sort (); //06. Add one or more elements to the end, and finally return the new length of the array varLength= Fruits.push ('Wert','Foot'); document.write ('\ n'+length); //07. Iterating through an array for(varIteminchfruits) {alert (Fruits[item]); }</script>
7 Program debugging
Scenario One: Debug in VS
Step: 01. Set the page to be debugged as the start page
02. Set Breakpoints
03. Press F5 to start debugging
Scenario Two: Chrome browser debugging
Step: 01. Click F12 to bring up the tool
02. Set Breakpoints
03. Refresh the page
Scenario Three: IE browser
Step: 01. F12, developer tools
02. Cut to the Script tab
03. Set Breakpoints
04. Start Debugging
05. Refresh
Four. Functions in JavaScript
1. Common system functions
parseint ("string");
The parseint () function first looks at the character at position 0, determines whether he is a valid number, if not, returns Nan, does not perform another operation, but if the character is a valid argument, the function will look at the character at position 1 for the same test. This process continues until the character is found to be a valid character, at which time the character converts the previous string to a number.
eg
var num1=parseint ("78.9")//return value is 78
var num2=parseint ("afa78.9")//return value is Nan
02.parseFloat ("string");
His usage is similar to parseint, except that the first point appearing in the string is considered a valid character.
eg
var num1=parseint ("78.9")//return value is 78
var num2=parseint ("afa78.9")//return value is Nan
2. Custom Functions
in JavaScript, custom functions are composed of functions, function names, and a set of JavaScript statements to be executed in parentheses.
Let's take a look at the syntax:
Function name (parameter 1, parameter 2,..)
{
JavaScript statements
[Return value]
}
function is a keyword that defines functions and must have.
Parameter 1, and parameter 2 is the parameter of the function, because the type of JAVASCRITP his province is a weak type, all of which are not necessary to provide a type when given a parameter
The start and end of the function defined by {}.
The return statement is used to specify the value returned by the function.
2. Calling functions
The name of the image. Method names
3. Anonymous functions
An anonymous function is a function without a name, also called a closure function (closures), which allows temporary creation of a function without a specified name. Most often used as the value of the callback function (callback) parameter, many novice friends do not understand the anonymous function. Here's a look at the analysis.
Function name (argument list) {functional body;}
If you are creating an anonymous function, it should be: function () {body;}
Because it is an anonymous function, it is generally not given a parameter.
Why do you create anonymous functions? The anonymous function is used under what circumstances. Anonymous functions mainly have two common scenarios, one is the callback function, the other is the direct execution function.
<script language="javascript">var"a";( function () { var a="b"; alert (a);}) (); alert (a); </script>
Output Result: 1 First output B
2 Output A
Cause: The script tag has an anonymous method when it is called from the
Basic JavaScript basics------Getting Started