Getting started with javascript
1. Introduction to JavaScript
The birth of JavaScript language is mainly to complete the data verification of the page, so it runs on the client and needs to run the browser to parse and execute the JavaScript code,
js is a product of netacape Netscape. It was initially named livescript. In order to attract more java developers, it was renamed JavaScript
Js is a weak type and java is a strong type
A weak type is a variable type.
Strong typing means that when a variable is defined, the type is determined and immutable.
int a = 12;
var a a = 10; Digital type a = "aaaa"; String type
characteristic:
1. Interactivity (what he can do is the dynamic interaction of information)
2. Security (direct access to local hard disk is not allowed)
3. Cross platform (any browser that can interpret js can run, regardless of the platform)
2. Combination of JavaScript and html code
2.1 the first method
Just use the script tag in the head or body tag to write javascript code
2.2 the second method
Create a separate js file
<!- Now you need to use script to import an external js file to execute – >
<!- The SRC attribute is specifically used to import js file paths (either relative or absolute) – >
<!- Script can import js files or define js code
However, one of the two functions can not be used at the same time – >
<script type="text/javascript" src="1.js"></script>
4. Variables
What are variables? A variable is the name of something that can hold some values
Variable type of javascript:
Value type; number
String type; string
Object type; object
Boolean type; boolean
Function type; function
Special values in javascript
Undefined. When all js variables are not given initial values, the default value is undefined
Null null
The full name of NAN is Not a Number Non numeric
js defines the format of variables
var variable name
var variable name = value;
<script type="text/javascript"> var a; alert(a); //undefined a = 12; //typeof () is a function provided by javascript, which can be returned by taking the data type of the variable alert(typeof(a));//number a = "abc"; alert(typeof (a));//string var b = 11; var c = "abc"; alert(b * c); //NaN is non numeric </script>
5.3 relation (comparison) operation
Equals: = = equals is a simple comparison of numeric values
All equal to: = = = in addition to comparing the face value of two values, the data types of two values will also be compared
<script type="text/javascript"> var a ="12"; var b = 12; alert(a == b);// true alert(a === b);//false </script>
7.4 logic operation
And operation:&&
Or operation:||
Inverse operation:!
In JavaScript language, it can be used as a boolean variable
0 null undefined "" (empty string) is considered false
var a = 0; if (a){ alert("Zero is true"); }else{ alert("Zero is false"); } var a = null; if(a){ alert("null For true"); } else{ alert("null False"); } var a = undefined; if(a){ alert("undefined For true"); } else{ alert("undefined False"); } var a = ""; if(a){ alert("Empty string is true"); } else{ alert("Empty string is false"); }
&&And operation:
There are two situations:
First: when all expressions are true, return the value of the last expression,
Second: when one of the expressions is false, return the value of the first false expression.
||Or operation:
First: when all expressions are false, return the value of the last expression,
Second: when one of the expressions is true, return the value of the first true expression.
And there is a short circuit with &&;
Short circuit means that when the & & or 𞓜 operation has a result, the subsequent expression will not be executed
var a = "abc"; var b = true; var c = null; var d = false; alert(a && b); //true alert(b && a); //abc alert(a && d);//false alert(c && d);//null alert(a && d && c)//false alert(d||c);//null alert(c||d);//false alert(a||c);//abc alert(c||a);//true
7. Array
7.1 array definition method
Array definition in js:
var Array name=[] //Empty array var Array name=[1,'abc',true] //Define an array and assign elements at the same time var arr = []; //Define an empty array arr[0] = "b"; alert(arr.length);//1 arr[2] = "abc"; alert(arr[1]);//undefined alert(arr.length);//20 // For arrays in javascript language, as long as we assign values through the array subscript, the maximum subscript value will automatically expand the capacity of the array //Traversal of array for (var i = 0; i < arr.length; i++) { alert(arr[i]); }
8. Function
8.1 two methods of function definition
First, you can use the function keyword to define functions
The format used is as follows:
function Function name (formal parameter list){ Function body }
<script type="text/javascript"> function fun(){ alert("Nonparametric function fun()Called"); } //Function call --- will be executed fun(); function f(a,b){ alert("Nonparametric function f()Called a="+ a +" b="+ b); } f(22,33); //How to define functions with return values in JavaScript? //Just use the return value return directly in the function body //Define a function with a return value function fuct(a,b){ return a+b; } alert(fuct(22,33)); </script>
Second use of function
The format is as follows:
var Function name = function(Formal parameter list){ Function body }
var fun = function (){ alert("This is a parameterless function!"); } fun(); var func1 = function (a,b){ alert("This is a parametric function" +a +"and"+ b); } func1(2,3); var func2 = function (a,b){ return a+b; } alert("The return value is"+func2(2,3));
Note: functions in java are allowed to be overloaded, but the overloaded functions in js will directly override the meaning of the previous layer
var func2 = function (a,b){ alert("This is a parametric function" +a +"and"+ b); } var func2 = function (a,b){ return a+b; } alert("The return value is"+func2(2,3));
8.2 arguments invisible parameter of function (only in function function)
It is a variable that does not need to be defined in a function, but can be directly used to obtain all parameters. We call it an invisible parameter
Invisible functions are especially like java based variable length functions
public void func(object ... args)
The variable length parameter is actually an array
Then the invisible parameters in js are the same as the variable length parameters in java foundation, and the operation is similar to array
<script type="text/javascript"> function fun(a,b){ alert(arguments.length);//See number of parameters for (var i = 0; i < arguments.length ;i++) { alert(arguments[i]); } alert("This is a parametric function"); } fun(1,'ab',true); //Requirements: write a function to calculate the sum of all parameters function func(){ var sum = 0; for (var i = 0; i < arguments.length; i++) { if(typeof(arguments[i])== "number" ){ sum += arguments[i]; } } return sum; } alert("And yes"+func(2,3,4,"abc","bcs",true,5)); </script>
9.js Custom objects in object Custom objects in form var Variable name = new object(); //Object instance (empty object) Variable name.Attribute name = value //Define an attribute Variable name.Function name = fuction(Formal parameter){}//Define a function Object access: Variable name.Attribute name/Function name(); ```java <script type="text/javascript"> var obj = new Object(); obj.name = "zhaochen"; obj.age = 18; obj.fun = function (){ alert("name: "+this.name+" age: "+this.age); } alert(obj.name); alert(obj.age); obj.fun(); </script>
{} custom objects in curly braces
Format:
var variable name = {attribute, method}