node. JS Tutorial 06-Functions

Source: Internet
Author: User

Objective:

This article describes the functions in node. js, which is a bit simpler than the previous one, and is actually the same as the function in our JavaScript .

Well, no more nonsense, let's get to the point.

node. JS functions:

"Example 1: Creating a basic parameter function"

  In JavaScript, we use function-defined functions, and the syntax of node. JS itself can be considered pure JS, so the creation of functions is the same.

function SayHello (_name) {    console.info ("Hello:" + _name);} SayHello ("guying");

In the above code, we create a basic parameter function, and then call it below, passing in the required parameters, and running the result as follows:

"Example 2: function passed as a parameter"

In JavaScript, a function can be passed into another function as a parameter. We can define a function first, then pass it, or we can define the function directly where the parameter is passed.

The use of functions in node. JS is similar to JavaScript, for example, let's take a closer look, I wrote the notes:

1 //addition2 functionAdditive (NUM1, num2) {3Console.info (num1 + "+" + num2 + "=" + (num1+num2));4 }5 6 //Subtraction7 functionsubtraction (NUM1, num2) {8Console.info (NUM1 + "-" + num2 + "=" + (num1-num2));9 }Ten  One //multiplication A functionmultiplication (num1, num2) { -Console.info (NUM1 + "*" + num2 + "=" + (num1*num2)); - } the  - //Division - functionDivision (NUM1, num2) { -Console.info (NUM1 + "/" + num2 + "=" + (num1/num2)); + } -  + //Calculator, incoming calculation method and two numbers A functionCalculator (functionname, NUM1, num2) { at functionname (NUM1, num2); - } -  - //incoming multiplication and two parameters -Calculator (multiplication, 3, 5);

In the above code, we created four methods, all passed two parameters, then the corresponding calculation, and finally output the calculation results.

The focus is the last function Calculator(), which requires three parameters: FunctionName, NUM1, num2.

FunctionName represents the function name that needs to be run, note that it is not the return value of the incoming function , but the function itself !

It then uses the incoming function to operate on the inside (L23 line). Finally, 27 lines, we call the calculator () function, pass in the multiplication function and two parameters.

The results of the operation are as follows:

node. js anonymous function:

We can pass a function as a variable.

But we don't have to go around this "define first, then pass" circle, and we can define and pass the function directly in the parentheses of another function:

1 function Execute (someFunction, value) {2  someFunction (value); 3 }45 Execute (function(word) {Console.log (Word)}, "guying");

In the code above, we have created an Execute function that requires two arguments, and the function function is to pass the second argument into the first function argument.

Then, in line 5th, we call the Execute function, passing in an anonymous function and a parameter. The meaning of an anonymous function expression is to output the passed-in Word parameter. The second parameter is "Guying".

With the action of execute, the result is as follows:

In this way, we don't even have to name the function, which is why it is called an anonymous function.

A bit small around, hehe, we do not faint, combined. Net/java, not really, combined with JavaScript to understand is OK.

How the function pass lets the HTTP server work:

Look back at our 3rd section. Create an HTTP server:

1 var http = require ("http"); 2 3 http.createserver (function(request, Response) {4     response.writehead (200 , {"Content-type": "Text/plain"}); 5     Response.Write ("Hello world!" ); 6     Response.End (); 7 }). Listen (88);

Here we use the HTTP object createserver to create the server, we pass in a request with a response to the anonymous function, the code:

1 var http = require ("http"); 2 3 function onrequest (Request, Response) {4     response.writehead ($, {"Content-type": "Text/plain"}); 5     Response.Write ("Hello world!" ); 6     Response.End (); 7 }89 http.createserver (onrequest). Listen (88);

In fact, every time we send a request, we perform a onrequest, and you can use console.log ("A request has been in."), and then go to repeat the request page. Look at the effect. Oh.

Summary:

Or that sentence:

A bit small around, hehe, we do not faint, combined. Net/java, not really, combined with JavaScript to understand is OK.

Well, thank you for your support, if you feel good, just a praise. (* ^_^ *) Your support is my motivation.

node. JS Tutorial 06-Functions

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.