zoukankan      html  css  js  c++  java
  • JS functions in detail

    notes:

    1. every method is a function , but not every function is a method.
    2. functions are objects
    • scope: variable "visibility", the location where a variable is defined dictates where we have access to that variable.
      • function scope
      • let msg = "I am so happy today";
        function help{
          let msg = "I'am on fire";
          msg; //" I am on fire", local variable
        }
        
        msg; //"I am so happy today" , global variable
      • block scope
      • let radius = 8;
        
        if(radius > 0){
            const PI = 3.14;
            let circ = 2 * PI * radius;
        }
        
        console.log(radius);  // 8
        console.log(PI);   // not defined
        console.log(circ);   //not defined
      • lexical scope

      • function outer() {
            let hero = "balck";
        
            function inner() {
                let cry = `${hero}, please save me!`;
                console.log(cry);
            }
            inner();
        }
        outer(); // balck, please save me!
      • function expressions
      • const square = function (num){
           return num * num;
        }
        square(7); //49
      • higher order functions
        • functions that operate on/with other functions, they can
        1. accept other functions as arguments
        2. return a function
        • function callTwice(func){
              func();
              func();
          }
          
          function laugh(){
              console.log("HAHAHA");
          }
          call(laugh); //pass a function as an arg;
          //HAHAHA
          //HAHAHA
      • returning functions
      • function rangeFunc(min, max){
            return function(val){
                return val >= min && val <= max;
            }
        }
        
        const AgeRange = rangeFunc(18, 100);
        
        AgeRange(17); // false
        AgeRange(68)//true
    • Methods
      • we can add functions as properies on objects
      • we can call them methods
      • const math = {
            multiply: function(x,y){
                return x*y;
            },
            divide: function(x,y){
                return x/y;
            },
            square: function(x){
                return x*x;
            }
        };

        shorthand

      • const math = {
            multiply(x,y){
                return x*y;
            },
            divide(x,y){
                return x/y;
            },
            square(x){
                return x*x;
            }
        }
         math.multiply(3,2)  //6
    • this in methods, use the keyword this to access other properties on the same object
      • const person = {
            first: 'Lily',
            last: 'Li',
            fullName(){
                return `${this.first} ${this.last}`
            }
        }
        person.fullName(); // "Lily Li"
        person.last = "Lyee";
        person.fullName(); //"Lily Lyee"

     

  • 相关阅读:
    arduino编程基础之--程序 元素
    arduino编程基础之--环境搭建
    C语言高手之路--目录
    生活中的数据结构
    Manjaro-KDE配置全攻略转
    多线程程序的奇怪问题记录
    manjaro安装openmv ide
    Linux进程数据结构详解
    Linux ps aux指令詳解--转
    记一次粗心大意的代码错误
  • 原文地址:https://www.cnblogs.com/LilyLiya/p/14261656.html
Copyright © 2011-2022 走看看