The basics of JavaScript daily must learn _javascript tips

Source: Internet
Author: User
Tags script tag

From today onwards, I will lead new friends, from understanding JavaScript start, step by step into the realm of the great God, the other No nonsense, now, we start from the beginning of 1.1 points.

We still introduce the origin of JavaScript, otherwise, we will have a very large misunderstanding of JavaScript, its history, we are too much to say, I also remember, the beginning of school history has not and the lattice

JS and we often used to develop a background program of the Java language is not a relative, their use is very far away, JS is only used in HTML, to the document node to make a check, to build a communication with the server an explanatory language, it is only the simplest understanding, We're going to go into the detail of JavaScript in the back. OK, we start from the grammar, although, I said no more nonsense, but, I feel I still so long-winded, well, we do not mind, want to cultivate into the great God's friends, also please bear with me this habit.

Also have to declare a point, read this article, the default is a friend of the HTML base if the following code does not understand the person, please first understand, then to practice, the secret of the secret is not common people can cultivate, if the obsession, how to do?

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
 
 

This kind of code, we should be very familiar with it, I have to say that the script tag is different from what we see in the textbook, I write it in the body tag, and it is written in the body tag, which is because the loading order of the Web page is from top to bottom, and is rendered by the node, Resources are also from the top down, loading, as for the resource response speed, on the server and the current access situation, this is a digression. When the page is rendered from the top down to the script label, it begins parsing the JavaScript code, and if the JavaScript code has an operation on the document node, it can get the node object correctly, otherwise there is a risk of code execution error. So our script tag is not written in the head tag.

Let's look at the grammar again, we first use the way of introduction to the program, otherwise, direct operation of the document node, some friends may not accept, to the program, we have to contact with OOP, so we now talk about classes, functions, variables, if you feel unfamiliar, do not be afraid, I used to be so, but , gradually I can understand, believe you also, in JavaScript, class and function have the concept of mutual transformation, so there are a lot of understanding problems, so I decided to start from the function, let everyone contact the concept of class

function Writemyname () {
      console.log ("My name is Mrdream");
    }
    
Writemyname ();

I this is with the Chrome browser, click F12, into the console panel, debugging code, we also have to get used to this browser, the back of everyone to see me debugging more, we will also like this browser, nature will also be accustomed to using it for code debugging.

I used the function keyword to declare a functional writemyname, in the method body I only wrote a simple Console.log ("My name is Mrdream"), and then directly with the function name plus a bracket

Writemyname (), so you can execute the function in front of the contents of the inside, the content is, print a word, print the content is the "my" name is Mrdream, now everyone as long as you understand that Console.log is to print the meaning on the line.

A brief summary of function Body Declaration syntax function function name () {function Body}

Now let's take a look at the function with the parameter

function Writemyname (_your_name) {
      console.log (_your_name);
    }
    
Writemyname ("My name is Mrdream");

Now the function of the declaration is different from the previous function, that is, the printed content is passed in the form of parameters, then the advantage is that we call Writemyname () before the time, just print the fixed content, but now we can write Writemyname ("careless"), A pair of double quotes inside write any name, we can print out, is not more convenient ah, let's take a look at

The flexibility is much higher than before, we can pass arbitrary string names to the function body, remember, the string, must take the outside of a pair of quotation marks, otherwise, there will be errors, the current you do not understand, this is why, next, we will continue to speak the declaration of variables, first of all, we have to understand, the variable is what, The role of a variable

var five = 5;
var six = 6;
    
function Add () {
  console.log (five+six);
}

We also use the function name + parentheses for function call Add (), let's look at the effect

Inside the function body is still a print statement, the result of printing is 11,5+6=11, there is no mistake, if we want to do other value addition, how to do? Do you want to modify the variable, right, the preceding VAR is used to declare the variables of the keyword, we declare a five and a six, and assign them a value, and then, print the two variables Add.

Do you think that every time we want to print, we have to modify the variables inside the function body, which is very troublesome, so we'll try a function to pass the parameter

var five = 5;
var six = 6;
    
function Add (num1,num2) {
  console.log (num1+num2);
}
    
Add (Five,six);

We passed in the position of the function num1,num2 the variable five,six also print the correct result, so that we can easily pass the other values.

Now look, we can pass in variables, or we can pass in numbers, both positive and negative, and if you need to use an unfixed value to compute the function body, we need to write the function in the function with the parameter.

To sum up, what have we learned today?

First, what is JavaScript used for?

Second, the Javsscript code on the page where the most appropriate

Third, the variable declaration, with what keyword

Four, we learned the function of the declaration (with parameters, without parameters, with parameters of the reason), with what keyword

Haha away from the great God and a step closer, I hope we continue to persevere, there will be some harvest.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.