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!
  • 相关阅读:
    Jdbc 事务
    Spring
    【Java集合的详细研究1】Collections类常用方法总结
    Java Number类
    Java中值类型和引用类型的区别
    Java常量池的理解
    Java重写与重载之间的区别
    Java中Animal b = new Dog();Dog c = new Dog();的区别
    java类构造器的理解
    Javascript history pushState onpopstate方法做AJAX SEO
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3887151.html
Copyright © 2011-2022 走看看