Learn JavaScript from scratch (i)-Understanding functions

Source: Internet
Author: User

Wedge

Lao Yao Write this note at the beginning said: "Learn JS, do not understand the event mechanism, the basic can be said to learn JS, is the white school."

I am very ashamed to see this sentence, if I understand the event mechanism, I may not order this note, and now I am the one who learned.

Re-start to look at themselves, engaged in web front-end development This job has been more than half a year, never seriously wrote a technical note. Perhaps I am not a qualified front-end development engineer, empty cavity enthusiasm but difficult to display. Take this as your first note and encourage yourself.

About this note is not intended to write the understanding of the event mechanism, after all, I was a white learning js, swim is clown. Let's start with the basics. (zhouAlex066)

Functions (function)

According to W3school, a function is event-driven, or a reusable block of code that executes when it is invoked.

An example of a simple point: alert (); It is a reusable function that is encapsulated by JavaScript.

Since the alert () alert window is mentioned, there is also a confirm () confirmation window and prompt () to enter the dialog window.

Alert () is the most common function of debugging code when learning JavaScript, and then Console.log ()/console.dir, the number of times to use alert () is gradually reduced, of course, debugging code is not the only two ways, and the main content of this note is not about JavaScript debugging, there are a lot of good debugging notes on the Internet, I will no longer repeat (attached: Nanyi <firebug Console detailed >).

Confirm () is used to display a dialog box with the specified message and OK and Cancel buttons. Click the button to return a pair of Boolean values (Boolean), and if the user clicks the OK button, confirm () returns TRUE. If you click the Cancel button, confirm () returns false.

Prompt () is a dialog box for displaying a prompt user for input. returns NULL if the user clicks the Cancel button of the prompt box. If the user clicks the Confirm button, the text that is currently displayed in the input field is returned.

How the function is declared: the keyword function name (a set of parameters) {The pending code in parentheses};

When a function name is called, the pending code {enclosed in parentheses} is executed, and the function is either event-driven or a reusable block of code that executes when it is invoked. ”

Tip: JavaScript is case sensitive. The keyword function must be lowercase and must be called with the same casing as the function name.

When you call a function, you can pass values to the function, which are called parameters. Parameters can be multiple, separated by commas. These parameters can be used in functions. parameters also fractal participate in the argument, which will appear in the actual example.

Sometimes, we want the function to return the value to the place where it was called. This can be achieved by using the return statement. When you use the return statement, the function stops execution and returns the specified value.

W3school Example:

function MyFunction () {

var x=5;

return x;

}

var myvar=myfunction ();

The value of the MyVar variable is 5, which is the value returned by the function "MyFunction ()".

There are three types of function constructs that I know: (partly from Sina blog)

function functionname (arg0, arg1, ... argN) {statements}//function statement

var function_name = new Function (arg1, arg2, ..., argn,function_body);//function () constructor

var = function (arg0, arg1, ... argN) {statements};//functions Direct Amount

If the function has no explicit return value, or if a return statement with no arguments is called, then the value it really returns is undefined.

Learn JavaScript from scratch (i)-Understanding functions

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.