zoukankan      html  css  js  c++  java
  • [Javascript] Funciton Expression

    //This will load the code into the memory no matter 
    //you call it or not
    function diffOfSquares(a,b){
        return a*a -b*b;
    }
    
    //This code will only load into memory when you call it.
    var diff = function diffOfSquares(a,b){
        return a*a-b*b;
    };
    
    diff(9,5); //Call diff function instead of diffOfSquares 
    
    //So why we still need diffOfSquares, since we don't call it
    
    /**
        Anonymour Function
    */
    var diff = function(a,b){
        return a*a-b*b;
    };
    
    //You can use console.log(diff); It will print out the
    //function instead of the value, so you can check what
    //this function refer to.
    
    /********************************************/
    
    var greeting = function(){
        alert("Thank for fucking!");
    };
    
    closeTerminal(greeting);
    
    function closeTerminal(message){
        
        ...
        message();
        ....
    }
    
    //We pass greeting as param to the closeTerminal function
    /*
        function closeTerminal(greeting){
            ...
            greeting();
            ...
        }
    */
    //The out put would be 
    //"Thank for fucking!"
    
    
    /*********************************************/
    //According to difference cases, the greeting message 
    //may be different.
    //For example, newCustomer & oldCustomer, we want to 
    //take them differently.
    
    var greeting;
    ... //Some code to sets new newCustomer to ture of false
    if(newCustomer){
        greeting = function(){
            alert("Thanks for funcking!");
        };
    }else{
        greeting = function(){
            alert("Welcome to be fucked!");
        };
    }
    
    closeTerminal(greeting);
    //if newCustomer is true, --> Thanks for funcking!
    //if newCustomer is false, --> Welcome to be fucked!
  • 相关阅读:
    搬运好文章->>>>>子网划分详解
    搬运好文章->>>>>子网掩码详解
    搬运好文章->>>>>IP地址和MAC地址详解
    搬运好文章->>>>>计算机中进制之间的关系和转换
    extend 与 append 的区别
    数据类型---字符串
    多引号的作用,字符串格式化
    列表复制的几种方法
    十六进制和二进制转换
    python奇偶数求和
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3887151.html
Copyright © 2011-2022 走看看