Summary of JavaScript basic video tutorial (Chapter 051-060)

Keywords: Javascript Attribute less

<!DOCTYPE html>
    <meta charset="utf-8" />
    <title>051-060 Chapter summary</title>
051. Function introduction
- Function is also an object
- Functions can encapsulate some functions (codes) and execute them when necessary
- Function can save some code to call when needed
- Use typeof When checking a function object, it returns function
<script type="text/javascript">
    //We seldom use constructors to create a function object in actual development
    //The code to be encapsulated can be passed to the constructor as a string
    var fun11 = new Function("console.log('this is a first code')")
    //The code in the function will execute when the function is called,Call function syntax: function object()
    //When a function is called, the encapsulated code in the function executes in order
     * Using function declarations to create a function
     *  Syntax:
     *      function Function name ([parameter 1, parameter 2... Parameter N]){
     *          Sentence...
     *      }
    function fun12(){
        console.log("This is my second function~~~");
     * Using function expressions to create a function
     * var Function name = function([parameter 1, parameter 2... Parameter N]){
     *   Sentence....
     *  }
    var fun13 = function(){
        console.log("I am the code encapsulated in anonymous functions");

052. Parameters of function
//One or more parameters (formal parameters) can be specified in () of the function
//Multiple parameters are used and separated. Declaring a parameter is equivalent to declaring a corresponding variable inside a function
//But not assigned
<script type="text/javascript">
    // Define a function to find the sum of two numbers
    function sum21(a,b){
        console.log("a = "+a);
        console.log("b = "+b);
    //When you call a function, you can()Arguments specified in (actual arguments)
    //The argument will be assigned to the corresponding parameter in the function
     * When a function is called, the parser does not check the type of the argument,
     * Therefore, it should be noted that it is possible to receive illegal parameters. If possible, it is necessary to check the type of parameters
     * The argument of a function can be any data type
    sum21(true , false);
     * When a function is called, the parser does not check the number of arguments
     *  Extra arguments will not be assigned
     * If the number of arguments is less than the number of parameters, the parameters without corresponding arguments will be undefined

053. Return value of function
//You can use return to set the return value of a function
//Syntax: return value
return The latter value will be returned as the result of function execution,
//You can define a variable to receive the result
//The statement after return will not execute in the function
//If the return statement is not followed by any value, it is equivalent to returning an undefined,
//If return is not written in the function, undefined will also be returned
return Can be followed by any type of value
<script type="text/javascript">
    // Define a function to find the sum of two numbers
    function sum3(arg1,arg2){
        var sum = arg1 + arg2
        return sum
    var result = sum3(5,3)

054. Arguments can be any value
<script type="text/javascript">
    // Define a function to determine whether a number is even or not, and return true,Otherwise return false
    function isOu(num){
        return num%2 ==0
    var result = isOu(15)
    // An argument can be any data type or an object
    // When we have too many parameters, we can encapsulate them into an object and pass them through the object
    function sayhello(obj){
        var say = "My name is""I am this year"+obj.age
        return say
    var person = {
        age: 18
    var result = sayhello(person)
    // An argument can be an object or a function
    function fun4(a){
        console.log("a ="+ a);
     * sayhello()
     *  - Calling function
     *  - Equivalent to the return value of the function used
     * sayhello
     *  - Function object
     *  - Equivalent to using function objects directly

055. Type of return value
//The return value can be any data type
//It can also be an object or a function
<script type="text/javascript">
    function fun51(){
        alert("Function is about to execute~~~~");
        for(var i=0 ; i<5 ; i++){
            if(i == 2){
                //Use break You can exit the current loop
                //continue Used to skip the current cycle
                //Use return You can end the entire function
        alert("Function completed~~~~");
    function fun52(){
        //Return an object
        return {name:"Sand monk"};
    var a = fun52();
    console.log("a = "+a);
    function fun53(){
        //Declare another function inside the function
        function fun54(){
            console.log("I am fun54");
        //take fun4 Function object as return value
        return fun54;
    a = fun53();

056. Execute function now
//After the function is defined, it is called immediately. This function is called immediate execution function
//Functions executed immediately are often executed only once
<script type="text/javascript">
        alert("I am an anonymous function;
        console.log("a = "+a);
        console.log("b = "+b);

057. Methods and enumeration properties
<script type="text/javascript">
    var obj7 = {
        name:"Sun WuKong",
        address:"Flower and fruit hill"
    //Enumerating properties in objects
    //Use for ... in Sentence
     * Syntax:
     *  for(var Variable in object){
     *  }
     * If there are several properties in the statement object, the loop body will execute several times
     *  Each time it executes, it assigns the name of a property in the object to the variable
    for (var n in obj7){
        console.log("Attribute name:"+n)
        console.log("Attribute value:"+obj7[n])

058. global scope
- Scope refers to the scope of a variable
- stay JS There are two scopes in: scope
- Written directly in script Label JS Code, all in global scope
- Global scopes are created when the page is open and destroyed when the page is closed
- There is a global object in the global scope window,
//It represents a browser window, which is created by the browser and can be used directly
- In the global scope:
//The created variables will be saved as properties of the window object
//All functions created will be saved as methods of window objects
- Variables in the global scope are global variables,
//Accessible from any part of the page

//Variable declaration ahead
- Use var The variables declared by the keyword will be declared (but will not be assigned) before all code execution,
//However, if the var keyword is not applicable when declaring variables, the variables will not be declared in advance
//Function declaration ahead of time
- Functions created using function declarations function function(){}
//It will be created before all code is executed, so we can call the function before the function declaration
//Functions created using function expressions are not declared ahead of schedule, so they cannot be called before declarations.
<script type="text/javascript">
    var a = 10;
    var c = "hello";
    function fun(){
        console.log("I am fun function");
    // Variable declaration ahead
    console.log("a2 = "+a2);
    var a2 = 123;
    //Function declaration, which will be created in advance
    function fun81(){
        console.log("I am a fun function");
    //Function expression, not created in advance
    var fun82 = function(){
        console.log("I am fun2 function");

059. Function scope
- The function scope is created when the function is called. After the function is executed, the function scope is destroyed
- Each time a function is called, a new function scope is created, and they are independent of each other
- Variables in the global scope can be accessed in the function scope
//Function scope variables are not accessible in the global scope
- When a variable is operated in the function scope, it will first look for it in its own scope and use it directly if there is one
//If not, look up the scope until you find the global scope,
//If it is still not found in the global scope, an error ReferenceError will be reported
- To access global variables in a function, you can use the window object
<script type="text/javascript">
    var a01 = "Overall situation a"
    function fun91(){
        var a01 = "I am fun91 Of a"
        var b01 = "I am fun91 Of b"
        function fun92(){
     * There is also a declaration advance feature in the function scope,
     *  Variables declared with the var keyword are declared before all code in the function executes
     *  Function declarations are also executed before all code in the function is executed
    function fun93(){
        function fun94(){
        var a02 = "I am a"
    //In functions, not applicable var Declared variables become global variables
    function fun95(){
        d01 = "d01"
    //Defining a parameter is equivalent to declaring a variable in a function scope
    var e = 23
    function fun96(e){
    fun96() //undefined

060. debug(Debugging)
<script type="text/javascript">
    var a60 = 123
    function fun60(a60){
    fun60() //undefined
    console.log(a60) // 123
    var a61 = 123
    function fun61(a61){
    fun61(789) //789
    console.log(a61) //123
    var a62 = 10
    var b60 = 20
    c60 = true
    function fun62(){
    var d60 = 30



All basic course links:

 1. Summary of JavaScript basic video tutorial (Chapter 001-010)           2. Summary of JavaScript basic video tutorial (Chapter 011-020)          3. Summary of JavaScript basic video tutorial (Chapter 021-030)        4. Summary of JavaScript basic video tutorial (Chapter 031-040)

5. Summary of JavaScript basic video tutorial (Chapter 041-050)           6. Summary of JavaScript basic video tutorial (Chapter 051-060) 7. JavaScript basic video tutorial summary (Chapter 061-070) 8. JavaScript basic video tutorial summary (Chapter 071-080)

9. JavaScript basic video tutorial summary (Chapter 081-090) · 10. JavaScript basic video tutorial summary (Chapter 091-100) · 11. JavaScript basic video tutorial summary (Chapter 101-110) · 12. JavaScript basic video tutorial summary (Chapter 111-120)

13. JavaScript basic video tutorial summary (chapters 121-130) · 14. JavaScript basic video tutorial summary (chapters 131-140)

In addition, please pay attention to my Sina micro-blog

Posted by kentopolis on Mon, 02 Dec 2019 01:35:12 -0800