JavaScript is a descriptive language, but it is not difficult to learn. As long as you study it carefully, you will definitely learn it well. I believe that when you read this article, you must have learned HTML. Use JavaScript to have better interaction with web pages. Let’s get to the topic.
1. JavaScript
1.What is JavaScript
JavaScript is a descriptive language, and is also a scripting language based on object and event drive (Event Driven).
2. Characteristics of JavaScript
JavaScript is mainly used to add interactive behavior to HTML pages.
JavaScript is a scripting language with similar syntax to Java.
JavaScript is generally used to write client scripts.
JavaScript is an interpretive language.
3. The composition of JavaScript
ECMScript standard (standard that specifies all attributes, methods and objects)
BOM Browser Object Model: Interact with HTML
DOM Document Object Model: Access and manipulate HTML documents
4. Basic JavaScript structure
<script language=”javascript” type=”text/javascript”></script>Language=”javascript” is used to indicate that the language used is javascript
5.JavaScript execution principle
1. The browser client sends a request to the server. (The address entered by the user in the browser address bar)
2. Data processing: The server side processes a page containing javaScript.
3. Send response: The server sends the page to the browser client to process the HTML file containing javascript, and then the browser client parses the HTML tags and JavaScript tags from top to bottom, presenting the page effect to the user.
two. How to introduce JavaScript into web pages
1. Use the <script> tag.
2. Use external JavaScript files.
I want to run JavaScript on multiple pages to achieve the same effect, usually using files with external files as .js.
How to reference a file with .js as an extension:
<script src=”../1.js”></script>
Note: External files cannot contain <script></script>
3. Directly in HTML tags
<input name='btn' type="button" value="Pop up message box" onclick="javascript:alert("Welcome");"/>3.JavaScript core syntax
1. Declaration and assignment of variables
The declaration of variables is only used in var, and the naming specification of variables is similar to Java. Var num=1;
In JavaScript, variables can be used directly without declaration, but this usage is not recommended.
2. Data Type
Undefined(Undefined type)
Null (null type)
Number (number type)
String (String type)
Boolean (Boolean type)
3. The difference between undefined and null
null means "no object", that is, there should not be a value there. Typical usage is:
(1) As a parameter of a function, 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", which means there should be a value here, but it has not been defined yet. Typical usage is:
(1) When a variable is declared but has no assignment, it is equivalent to undefined.
(2) When calling the function, the parameter that should be provided is not provided, and the parameter is equal to undefined.
(3) The object has no attribute assigned to the value, and the value of the attribute is undefined.
(4) When the function does not return a value, undefined is returned by default.
4. There are only 6 cases, and the result is determined to be false.
null,false,undefined,0,"",NaN
5. Some common methods of strings
toString(); returns the string
toLowerCasee(); replace the string with lowercase.
toUpperCase(); convert string to uppercase
charAt(index); returns the string at the specified location
indexOf(str,index); Find the first occurrence of a specified string in the string
Substring(index,index); returns a string located between the specified index index1 and index2 (including index1 and index2 but not index2)
Split(str);Split string into character array
6. Three ways to create an array and assign values to the array
01. Var num=('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: Set or return the number of elements in the array
Join(): Put all the speech speeds of the array into a string and divide them by splitting characters.
Sort(): Sort array
Push(): Add one or more elements to the end of the array and put back the new length.
<script type="text/javascript">
//01. Method 1: Create an array and assign a value to the array
// var fruit = new Array('apple', 'orange', 'peach', 'bananer');
//02. Method 2: Create an array first, and then assign a value to the array through subscript
var fruits = new Array(4);
fruits[0] = 'apple';
fruits[1] = 'orange';
fruits[2] = 'peach';
fruits[3] = 'bananer';
//03 Method 3: Similar to Method 1, but the symbol is changed
//var fruitss = new Array['apple', 'orange', 'peach', 'bananer'];
//04. Access data with subscript 3 in the array
document.write(fruits[0]);
//05 Put the element into the string through the join method of the array and divide it with the specified delimiter
var result = fruits.join(',');
//06Sort the array through the sort method
fruits.sort();
//06. Add one or more elements to the end and finally return the new length of the array
var length= fruits.push('wert','foot');
document.write('/n' + length);
//07. Iterate over the array
for(var item in fruits)
{
alert(fruits[item]);
}
</script>
7. Operators
8 Program debugging
Solution 1: Debugging in VS
Steps: 01. Set the page to be debugged as the start page
02. Set breakpoint
03. Press F5 to start debugging
Solution 2: Chrome browser debugging
Steps: 01. Click F12 to call out the tool
02. Set breakpoint
03. Refresh the page
Solution 3: IE browser
Steps: 01.F12, developer tools
02. Switch to the Script tab
03. Set breakpoint
04. Start debugging
05. Refresh
4. Functions in JavaScript
1. Commonly used system functions
01. parseInt("string");
parseInt() function first checks the character at position 0 and determines whether it is a valid number. If not, it returns NaN and does not perform other operations. However, if the character is a valid parameter, the function will view the character at position 1 and perform the same test. This process will continue until it is found that the character is a valid character. At this time, the character converts the previous string into a number.
eg:
var num1=parseInt("78.9")//The return value is 78
var num2=parseInt("afa78.9")//The return value is NaN
02.parseFloat("string");
Its usage is similar to parseInt, except that the first point that appears in the string will be considered a valid character.
eg:
var num1=parseInt("78.9")//The return value is 78
var num2=parseInt("afa78.9")//The return value is NaN
2. Custom functions
In JavaScript, a custom function is composed of function, function name, a set of parameters and JavaScript statements to be executed in parentheses.
Let's take a look at the syntax:
function function name (parameter 1, parameter 2,..)
{
//JavaScript statement
[return return value]
}
function is a keyword that defines a function and must be there.
Parameters 1 and parameter 2 are parameters of the function, because JavaScriptp's type is weak, and there is no need to provide a type when given the parameter.
The beginning and end of a function defined by {}.
The return statement is used to specify the value returned by the function.
2. Calling functions
To execute a function, you must first call this function, and you must formulate the function name and the parameters followed.
eg:
<script type=”text/javascript”>function show(){}show();</script>3. Anonymous functions
Anonymous functions are functions without names, also called closures, which allow temporary creation of a function without a specified name. Most often used as a value for callback parameters, many novices don't know about anonymous functions. Let’s analyze it here.
function function name (parameter list) {function body;}
If you are creating anonymous functions, it should be:
function(){function body;}
Because it is an anonymous function, there are generally no parameters passed to him.
Why create anonymous functions? Under what circumstances will anonymous functions be used? There are two common scenarios for anonymous functions, one is the callback function, and the other is to directly execute the function.
eg:
<script language="javascript">var a = "a";(function(){ var a="b"; alert(a);})();alert(a);</script>In the above code, two alert boxes will be output in sequence. The content of the first alert box is b and the second one is a. I thought there was an anonymous method in this Script tag and that when b was first popped up, then alert (a) after touching anonymous method, a popped up.
The above basic JavaScript tutorial - a must-read article for getting started is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.