Javassript Foundation Fourth day

Source: Internet
Author: User

First, preface

Yesterday we learned that a very important concept of Js is called a function, which is a encapsulation mechanism for redundancy and garbage code. The simple thing is, in order for the program to perform better and faster, we extract some repetitive code into a small box with a name, and we can use the box directly when we need it.

Second, the introduction

In the front-end or in the background language function is a successful program of the smallest package module, like building blocks you need different kinds of modules to form a finished product. Then for the function each one should have its own characteristics.

Third, the key content㈠Four forms of the function

① has parameters, no return value (usually no return value when the default return value is undefined)

function Fun (b) {    Console.log (a+b);}

② no parameters, no return value (typically used for code reuse or functional code snippets)

function Fun () {    console.log ("Hello World");}

③ have parameters and return values (the recommended function can be written like this)

function Fun (b) {    return a +b;}

④ no parameters, with return values

    

function Fun (b) {    Console.log (a+b);}
Overloading of the ㈡ function

  

Unlike the background language,JS functions are not overloaded with functions. When it comes to overloading, we need to know a little bit about what overloading means.

Overloading: That is, the function is named the same, but the number of formal parameters of the function or the different data types will cause different. In short, there are two identical names at the time of the roll call, but the body is really fat and thin. All two of them are classmates in this class so they are all there.

In Js There is no overload of this gratitude, then a lot of people will ask, if I am in the work of the name is not the same as what to do, then see the following code

function Fun (b) {    return a +b;} function Fun (a,b,c) {    = a| | 0;     = b| | 0;     = c| | 0;     return a+b+c;} Console.log (fun);

I believe a lot of people test this code when the output is 3 So think I said it wrong. But that's not true. If we open the test tool step-by-step debugging, we will find that when the function call is called the parameter 3 function. So why is it equal to 3 ? Because I added a short-circuit operation here to avoid the occurrence of errors. ( do not understand the short-circuit operation, please look at Js 3 days content ). In Js There is no overload, as defined above the first definition will be overwritten by the following.

㈢ selection Structure

Scope: The area in which the variable is acting;

1) Global scope: A global scope variable is a global variable. That is, it can be accessed anywhere in the entire page. (Note: Variables defined in the function that do not use var are also global variables).

2) Local scope: Local scope variables are local variables. can be accessed only in functions.

㈣ anonymous function and function expression style

In yesterday's introduction, we learned that the function definition is in the form of functions ([ parameter list ]) { function body }; In fact, one of the ways to define it is to use a function expression to name

var function () {    return 0;}

This approach is similar to the definition of a variable, and the function that follows it is called an anonymous function without a name. The variable name in front of it can be considered a function name.

    

The difference between the expression of a function table and the way a function is named:

      1. function has a name in its declaration
      2. A function in a function expression has no name, is an anonymous function, and the name of the preceding variable can be regarded as the name of the function.
      3. When a function is pre-parsed, the function declaration is first advanced to the front of the scope, and the function expression does not elevate
      4. Functions in a function declaration can be called before a declaration, but functions of a function expression must be called after the function expression
   pre-parsing of ㈤ functions

    Before the program actually executes, all the code is scanned again, declaring the variables, and raising the parameters to the front of the current scope.

function Fun (A, b) {    console.log (c);    Console.log (a);    Console.log (b);     var c = +;} Fun (all);

What values will be printed in the current Code Console.log (c) , I think a lot of people have the same idea as I started because the local variable c is defined below, So the output should be the time . But in fact we are all wrong, because the function is to be pre-parsed, so the declaration of the variable refers to the current scope of the first, as follows:

function Fun (A, b    ) {var  c;    Console.log (c);    Console.log (a);    Console.log (b);     = +;} Fun (all);

    So C is not worth it, because it only declares that it is not assigned so it returns undefined.

㈥ self-executing functions      
var function () {    console.log("Hello")};fun ();(function() {console.log ("Hello");} ());(function() {    console.log ("Hello");}) ();

Since we use function expressions to define functions, we can use the part of the front of the parentheses instead of the right part of the assignment symbol when we call it, and then enclose the replaced part in parentheses to form a self-executing function.

Features: self-executing function is no name, and will be called immediately after. Used in one-time use.

   ㈦ callback function
    var function (a,b,ff) {        return  ff (A, b);}    Console.log (10,20,function(A, b) {return a+b;}))

    

If you look at it, let's break it down a little bit.

function fn (b) {        return A +b    ;    } var function (a,b,ff) {    return  ff (A, b);}    Console.log (Fun (10,20,fn));

In fact, the essence of the callback function is to pass the function as a parameter.

Iv. Summary

I think the final point of this is to keep in mind that every function has a return value, and sometimes it may print but sometimes it will not print. You need to judge yourself a lot.

Javassript Foundation Fourth day

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.