Many basic content in JavaScript is basically the same as in Java, so there is no need to repeat it separately, including:
Various arithmetic operators, comparison operators, and logical operators;
if else statement, switch statement;
for loop, while loop, do while loop;
tags, breaks, continue;
try catch throw statement.
You can view the reference link after the article.
The following content is different parts of JavaScript.
This article first talks about the differences in the above content.
1. Consolidated judgment
There is a congruent judgment in the comparison operator of JavaScript ===, which is used to determine whether the values and types are equal.
2.for/in loop
The for/in loop in JavaScript is a bit like the enhanced for loop in Java, but it is used to traverse the properties of an object.
The code copy is as follows:
var person={fname:"John",lname:"Doe",age:25};
for (x in person)
{
txt=txt + person[x];
}
where x is the attribute name and person[x] is the value of the attribute.
3.With statement
With the With statement, you do not need to repeatedly specify the reference object when accessing object properties and methods. In the With statement block, all attributes and methods that are not recognized by JavaScript are related to the objects specified in the statement block.
Function: Create default objects for a program.
Format: with (<Object>){ <State Group> }
Right now:
The code copy is as follows:
With Object {
Statements
}
For example: When using the write() or writeeln() method related to the Document object, the following form is often used:
The code copy is as follows:
document.writeln("Hello!");
If you need to display a large amount of data, the same document.writeln() statement will be used multiple times. At this time, you can put all statements with Document object as reference object into the With statement block like the following program, so as to achieve the purpose of reducing the number of statements.
The code copy is as follows:
<!DOCTYPE html>
<html>
<head>
<title>withTest.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<script type="text/javascript">
with (document) {
write("Hello!");
write("<br>The title of this document is: /"" + title + "/".");
write("<br>The URL of this document is: " + URL);
write("<br>Now you don't need to write the prefix of the document object every time!");
}
</script>
</body>
</html>
4. Break the line
You can use a backslash to disconnect a sentence of code:
The code copy is as follows:
document.write("Hello /
World!");
But you can't make a statement like this:
document.write /
("Hello World!");