The loop structure, loop nesting and function introduction in JS

Source: Internet
Author: User
Tags true true

"Steps of the looping structure"
*① declaring a loop variable
*② Judging Cycle conditions
*③ execution of the loop body (all code in the while {}) operation
*④ Updating loop variables
*
* Then, loop execution ②③④
*
*
* "Data type supported by loop condition in JS"
*①boolean:true true False False
*②string: Non-empty string for vacuum string is False
*③null/nan/undefined: All is False
*④object: All is True
*⑤number:0 is false, all non-0 are true

While loop characteristics: First judge, then execute;
Do-while cycle characteristics: First execution, then judgment; even if the initial conditions are not established, the Do-while cycle is executed at least once;

[For Loop]
* 1, for Loop has three expressions, respectively: ① define the cyclic variable ② judge the loop condition ③ update the loop variable
* Between three expressions, separated by;
* For loop three expressions can be omitted, two, one indispensable
*
* 2, for loop characteristics: First judge, then execute;
* 3, for loop three expressions, can have a multi-part, separated by commas, but the second part of the judging condition with && link, the final result needs to be true/False

"Loop control Statement"
*
* 1, break: Terminate this layer loop, continue to execute the statement behind the loop;
* When the loop has multiple layers, break will only skip a layer of loops;
* 2, continue: Skip this cycle, continue to perform the next cycle;
* For a For loop, after continue executes, the Loop variable UPDATE statement continues execution n++
* For while,do-while,continue execution, continue to perform cyclic condition judgment, so when using these two loops, it must be noted that continue must be used after n++;

[Loop nesting features]
* The outer loop turns once, and the inner layer loops around one turn;
*
* [do graphic problem thinking]
*
* 1, determine the graph of a total of several lines, that is, the number of outer loop;
* 2, to determine that each line has several elements, representing a few inner layers of the loop;
* 3, determine the number of each element, that is, the number of cycles per inner layer;
* Tips: Usually, find out the number of each element, the relationship with the line number, that is, the maximum value of the current inner layer loop;

Browser console Print output
*
* Console.log ("Please enter a number between 0-6 \n111");
*
* \ n = line break
*
* \ t tab, so the cursor is returned to the next tab stop. (Each tab stop, 4 characters; If the previous tab stop is less than 4 characters, the following is displayed in the next pane; If the previous tab stop is 4 characters long, then the contents are empty one after the other;)
*/
Console.log ("Please enter a number between 0-6 \n1111\t2\t333");

"Declaration and invocation of functions"
* 1, Function declaration format:
* >>>function function name (parameter 1, parameter 2, ...) {
*//function body
* return result;
* }
* Format of the >>> function call:
* Call directly: function name (value of parameter 1, value of parameter 2, ...);
* Event Invocation: Event name = function name ();
* 2, the function of the Declaration of several emphasis:
The declaration of the *① function name must conform to the small hump rule (first letter lowercase, then each word capitalized);
*② parameter list: can have parameters, can have no parameters, respectively called the parameter function, no parameter function;
*③ a parameter list when declaring a function, called a "parameter list" (the name of a variable);
* The argument list when calling the function, called the "argument list" (the value of the variable);
* Parameters that are actually valid in the function depend on the assignment of the argument, and the parameter that is not assigned will be undefined;
*④ function If the return value is required, return the result using return;
* When calling a function, use the var variable name = function name () to accept the return result;
* If the function does not return a value, the accepted result is undefined.
Scope of variables in the *⑤ function:
* In the function, the variable declared with VAR, the default is the function local variable, only in the function content can be used;
* variables declared without VAR, default to global variables (global variables in functions, must be used after function call);
* The formal parameter list of a function, which is a partial list of functions, which can only be used inside the function;
The *⑥ function declaration has no precedence over the function call. That is, the invocation statement can be written before the declaration statement.

"Code execution order in JS":
* JS in the code to run, will be checked, loaded, that is, declaring variables, functions and other operations;
* and then into the execution phase (the assignment of variables is the execution phase)
*
* Therefore, the declaration of a function belongs to the check loading stage, and the function invocation belongs to the execution phase. Therefore, the function call statement is written before the function declaration statement, and there is no relationship.
*
* So, the above code executes the inflow
*--------Check the loading stage---------
* Var sum;//declaring variables
* Function FuncN () {}//Declaration functions
*
*--------Implementation phase-------
* Console.log (NUM);
* NUM=10;
* FuncN ();//code in {} executing function

"Declaration and invocation of anonymous functions"
* 1, declare an anonymous function, directly assigned to an event;
* Window.onload=function () {}
* 2, the use of function expressions to declare an anonymous function;
* Declare function expression var func=function () {}
* Call Function Expression: func ();
* >>> using an anonymous function expression, the call statement must be followed by the declaration statement, otherwise an error (compared to the general function declaration and the call difference?) )
* 3. Declare and invoke the anonymous function directly using the self-executing function:
*①! function (parameter 1) {} (the value of parameter 1);//start with any operator, use it generally!
*② (function () {} ())//use () to enclose the anonymous function and the following parentheses
*③ (function () {})//use () to wrap only an anonymous function expression
*
* Three types of writing features:
*① structure clear, beginning add! , End Plus (), not easy to mess, recommended use;
*② can indicate that the anonymous function and the following () as a whole, recommended to use;
*③ cannot indicate that the function and after () are a whole, not recommended for use;

[Arguments Object]
*
* 1, Function: Used to save the calling function, the assigned value of the argument list.
* >>> When we call a function and assign a value using an argument, the argument is actually saved to the arguments array, and the parameter can be called using arguments[n], even if there is no formal parameter;
* 2, the number of arguments arrays, depending on the argument list. Not related to formal parameters (order starting from 0);
* However, when the parameters, arguments, and arguments of the nth position are present, the shape participates in the arguments is synchronous. (That is, modify one of the values in the function and the other will change synchronously)
*
* 3, Arguments.callee is an important attribute of arguments, used to return a reference to the function where arguments is located;
*
* Arguments.callee () can invoke its own function execution;
*
* The function itself is called recursive, so Arguments.callee () is a common way of recursive invocation;
*
*
* this:
* Point to the scope of the function call statement, that is: Who called the function, this point to whom;

The loop structure, loop nesting and function introduction in JS

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.