May 30, 2021 Article blog
JavaScript is an object-based and event-driven (Event Driven) explanatory scripting language with secure performance that enables dynamic effects and interactions in client web pages and enables a real-time, dynamic interaction between users and Web pages.
JS composition: ECMAScript (core of JS), DOM (document object model), BOM (browser object model)
主要用于网页特效、服务端开发、命令行工具、桌面程序、APP、控制硬件—物联网、游戏开发
1, write in the line
<input type="button" value="按钮" onclick="alert('Hello World')" />
2, written in the script label
<script>
alert('Hello,world!');
</script>
由<script>...</script>包含的代码就是JavaScript代码,他将直接被浏览器执行。
3, write to the external js file, introduced on the page
<script type="text/javascript" src="/js/Hello.js"></script>
Putting code in a separate file is more conducive to maintaining the code, and multiple pages can reference the same .js file individually.
JavaScript syntax is similar to Java, with each statement; A t the end, the statement block is used. Note: JavaScript is strictly case sensitive.
Concept of a variable: A variable is a parameter that assigns a value. Use variables to easily obtain or modify data in memory Declaration of variables: Use the keyword var when declaring variables, pay attention to the space between the keyword and the variable name, or declare multiple variables in a row to separate the variables with commas.
var age;
var age, name, sex; a ge = 10; name = ‘zs’;
Note: Variable names must be a JavaScript identifier and should follow the following standard naming rules:
Assignment of variables: In JavaScript, the variable is assigned using . A ny data type can be assigned to a variable, the same variable can be assigned repeatedly, and can be a variable of a different data type, but can only be stated once with var. To display variables, you can use the .log (x) to open Chrome's console to see the results.
var age; age = 18;
var age = 18;
Characters that start with //until the end of a line are treated as comments that are for developers to see and are automatically ignored by the JavaScript engine. A nother block comment is to use / . . . / Wrap up multiple lines of characters as comments. For example:
<html>
<head>
<script> //这是一行注释
alert('Hello,world!');
/*从这里开始是块注释
块注释结束*/
</script>
</head>
<body>
···
</body>
</html>
The data types of JS fall into two broad categories
JavaScript does not distinguish between integers and floats, and is represented by number, which is a legitimate number type:
123;/integer 123 0.456;/floating points 0.456 1.2345e3;//equivalent to 1234.5-99;/negative NaN;//when the result cannot be calculated, use NaN for Infinity; // for infinity
Number has accuracy issues:
0.2 + 0.1 = 0.30000000000000004 0.1 + 0.2 !== 0.3 //true
So it's best not to judge whether floats are equal
Used to represent a sequence of characters consisting of zero or more 16-bit Unicode characters, or strings. S trings are arbitrary text enclosed in single or double quotes, such as 'abc', 'xyz', and so on. S ingle and double quotes are just a representation, not part of a string, so there are only three characters in the string 'abc', a, b, and c. Features: Immutable When you re-assign a string, it actually re-opens up memory space, for example:
var lang=“Java”; lang=lang+“Script”;
The above code first creates a space hold string "Java", and then, when running to the next line of code, re-opens a space in memory that holds "JavaScript" and the variable lang points to the newly opened space. These actions all occur in the background, affecting site performance, so don't write a lot of string stitching in general code.
null represents an "empty" value, unlike 0 and the empty string '', 0 is a value, '' means a string with a length of 0, and null is empty. Undefined means "undefined."
The Boolean value is exactly the same as the Boolean algebra representation, with only true and false values for a Boolean value, which are case sensitive. Boolean values can be represented directly by true and false, or by Boolean operations:
true;//这是一个true值
false;//这是一个false值
2>1;//这是一个true值
2>=3;//这是一个false值
Note: Boolean values are often used in conditional judgment sentences.
Gets the variable type, and the returned value is the string type Results are:
typeof 10 //"number"
typeof "10" //"string"
function fn(){
...
}
typeof fn //"function"
//age未声明
typeof age //"undefined"
typeof null //"object"
typeof undefined //"undefined"
First, object object
Second, there are two ways to create an object
第一种:
var obj = new Object();
obj.name = "###";
obj.age = 15;
第二种:
var obj = {
name:"###",
age:15
}
Access to object properties
There are two ways to access a property: an object. Property Name or Object ("Property Name")
Four, array
Arrays are also a kind of object. An array is a language structure that expresses a collection of sequential values.
Create an array. Note: different from java array length variable var users s new Array(3); u sers[0]=3; u sers[1]=9; u sers[2]=5; u sers[3]=999; T he values within the array are called elements. E ach element can be read quickly through an index (subscript). The index is an integer from zero.
Functions are a core concept for any language. F unctions allow you to encapsulate as many statements as you want, and you can call execution anywhere, at any time. F unctions in ECMAScript are declared by the funnction keyword, followed by a set of parameters and function bodies. grammar:
function functionName(arg0,arg1...){
statement
}
example:
function sayHi(name,message){
alert("Hello "+name+","+message);
}
sayHi("ly","how are you?");//函数的调用
Some of the functions that come with them
You can have a return value or you can not return a value when you return a function definition in ECMAScript. W hen the function is finished, the results are not always printed. W e expect the function to give me some feedback (such as the result of the calculation returned for subsequent operations), which allows the function to return something at this time. T hat is, the return value. The function returns a return value through return
Return value syntax:
//声明一个带返回值的函数
function 函数名(形参1, 形参2, 形参...){
//函数体
return 返回值;
}
//可以通过变量来接收这个返回值
var 变量 = 函数名(实参1, 实参2, 实参3);
The result of a function's call is a return value, so we can operate directly on the result of the function's call.
Return value detail: If the function does not display the use of the return statement, then the function has the default return value: Undefined If the function uses the return statement, then the value after return becomes the return value of the function If the function uses the return statement, but there is no value after return, the return value of the function is also: after the return statement is used by the undefined function, the function executes return The statement stops and exits immediately, which means that all other code after return will no longer execute.
Parameter ECMAScript does not mind the number of parameters passed, nor does it care about the data type of the argument. A rguments in ECMAScript are represented by an array that can be accessed inside the function body through the arguments object. Arguments objects are just like arrays (not Array instances)
Ginseng and arguments:
Formal parameters: When declaring a function, in order to function more flexibly, some values can not be fixed, for these fixed values. W e can set parameters for the function. T his parameter has no specific value, only a positional role, we usually call it formal parameters, also known as parameters. Actual parameters: If the function is declared, the parameters are set, then the corresponding parameters need to be passed in when the function is called, and we call the incoming arguments as actual parameters, also known as arguments.
var x = 5, y = 6;
fn(x,y);
function fn(a, b) {
console.log(a + b);
}
//x,y实参,有具体的值。函数执行的时候会把x,y复制一份给函数内部的a和b,函数内部的值是复制的新值,
Recommended lessons: JavaScript micro-class, ES6 micro-class