Daily basics of javascript
Starting from today, I will lead new friends to learn about javascript, step by step to the realm of great god, and start from the Getting Started stage.
Let's take a look at the history of javascript. Otherwise, we will have a very big misunderstanding about javascript. We will say too much about its history and I cannot remember it, at the beginning of school, I have never been too involved.
Javascript is not a relative of the java language that we often use to develop background programs. Their use range is also very different. js is only used in html for adding, deleting, modifying, and querying document nodes, build an explanatory language for communication with the server. This is just the simplest understanding. Next we will learn javascript in detail. Well, we will start with the syntax, although, I am not talking nonsense anymore, but I still feel so cool. Well, don't mind. If you want to cultivate your friends, please be patient with this habit.
I have to declare that the person who reads this article is a friend with HTML by default. If the person who doesn't even understand the following code, please understand it first and then practice it again, the only secret can not be practiced by ordinary people. What should I do if I get lost?
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
This kind of code should be familiar to everyone. What I have to say is that the script tag is different from what we see in the textbook. I wrote it in the body tag, it is also written at the end of the body Tag. This is because the webpage loading sequence is from top to bottom, rendered by node, and resources are loaded from top to bottom one by one. As for the resource response speed, this is an aside from the server and the current access situation. When the page is rendered one by one from top to top, the javascript code is parsed. If the javascript code has operations on the document node, it can get the Node object correctly, otherwise, there is a risk of code execution errors. So our script tag is not written in the head tag.
Let's take a look at the syntax. We will first explain it in the method of getting started with the program. Otherwise, some friends may not be able to directly operate the document node. When talking about the program, we will have to contact OOP later, so now we are talking about classes, functions, and variables. If you feel unfamiliar, don't be afraid. I used to do the same. But gradually I can understand it. I believe you are the same, in javascript, classes and functions have the concept of mutual conversion, so there are still many problems with understanding. So I decided to start with the function, so we will not allow everyone to access the concept of classes first.
function WriteMyName(){ console.log("My name is MrDream"); } WriteMyName();
I used chrome browser. Click F12 to go to the console panel and debug the code. You should also use this browser. Later, we will see more debugging, you will also like this browser and will naturally get used to using it for code debugging.
Previously, I used the function keyword to declare a function WriteMyName. In the method body, I only wrote a simple console. log ("My name is MrDream"), and then directly add a bracket with the function name
WriteMyName (), so that you can execute the content in the previous function body. The content is to print a sentence, and the printed content is My name is MrDream. Now you only need to understand the console. log is the meaning of printing.
A brief summary of the function body declaration syntax function name () {function body}
Let's take a look at the functions with parameters.
function WriteMyName(_your_name){ console.log(_your_name); } WriteMyName("My name is MrDream");
The difference between declared functions and the preceding functions is that the printed content is passed in the form of parameters. The advantage of this is that when we call WriteMyName, only the fixed content is printed. However, now we can write WriteMyName ("") in this way, and write any name in a pair of double quotation marks so that we can print it out, is it more convenient? Let's take a look.
The flexibility is much higher than the previous one. We can pass any string name to the function body. Remember, strings must be enclosed by a pair of quotation marks. Otherwise, errors may occur, at present, you still cannot understand why. Next, we will continue to talk about the declaration of variables. First, we must understand what the variables are and what the functions of the variables are.
var five = 5;var six = 6; function add(){ console.log(five+six);}
We also use the function name + brackets to call the function add (). Let's take a look at the effect.
There is still a print statement in the function body. The print result is + 5 = 11. No error. What if we want to add other values? Is it necessary to modify the variable? By the way, the previous var is the keyword used to declare the variable. We declare a five and a six, assign values to them, and then print the two variables to add.
Do you think we need to modify the variables in the function body every time we want to print it? This is very troublesome. Let's try a function that passes parameters.
var five = 5;var six = 6; function add(num1,num2){ console.log(num1+num2);} add(five,six);
The five variable is input at the positions of the num1 and num2 functions, and the correct result is printed by six. In this way, other values can be passed in conveniently.
Now let's take a look at it. We can pass in variables, numbers, and positive and negative values. If you need to use an unfixed value in the function body for computation, we need to write this function in a function with parameters.
To sum up, what have we learned today?
First, what is javascript used?
Second, where should the javsscript code be placed on the page?
Third, what keywords are used for variable declaration?
Fourth, we learned how to declare a function (with parameters, without parameters, with the reasons for parameters). What keywords are used?
Haha is a step closer to the great gods. I hope you will continue to make persistent efforts and make some achievements.
Articles you may be interested in:
- Introduction to examples of using Javascript WebSocket (simple getting started tutorial)
- Getting started with node. js Web application framework Express
- JavaScript onkeydown event entry instance (a key on the keyboard is pressed)
- Nodejs simple getting started tutorial (1): module Mechanism
- AngularJS Getting Started: Hello World!
- Learning Environment Construction for AngularJS getting started tutorial
- AngularJS getting started tutorial (1): static template
- AngularJS getting started tutorial (2): AngularJS Template
- Angularjs Basics