zoukankan      html  css  js  c++  java
  • ES6 new syntax of Default Function Parameters

    Default Function Parameters.md
    Default Function Parameters

    function getSum(a,b){
        a = (a !== undefined) ? a : 1 ;
        b = (b !== undefined) ? b : 41;
    
        console.log(a+b);
    }
    
    getSum();
    getSum(1,2);
    getSum (10);
    getSum(null, 6);
    

    In ES5,if the argument is not specified,its value would be set to undefined.
    if we do this,what will be happen?

    function getSum(a, b) {
        a = 1;
        b = 41;
        console.log(a + b);
    }
    getSum();
    getSum(1,2);
    getSum(10);
    getSum(null, 6);
    

    if this?

    function getSum(a, b) {
        console.log( a + b);
    }
    getSum();
    getSum(1,2);
    getSum(10);
    getSum(null, 6);
    

    //ES6
    function getSum(a = 1, b = 41 ) {
        console.log(a + b);
    }
    
    getSum();
    getSum(1, 2);
    getSum(10);
    getSum(null, 6);
    


    In ES6 sets default values trying to streamline this process.

    var getAnswer = function(number = 42, item = "universe"){
        console.log(number + " is the answer to " + item);
    }
    getAnswer(undefined, "life"); //42 is the answer to life.
    
    var getName = function(firstName = "John", lastName = "Doe") {
        console.log(firstName + " " + lastName);
    }
    
    getName("Jone"); //Jone Doe.
    
    var defaultName = "John";
    var getName = function(firstName = defaultName, lastName = "Doe"){
        console.log(firstName + " " + lastName); 
    };
    getName(); //John Doe
    
    var getFirstName = () => "John";
    
    var getName = function( firstName = getFirstName(),lastName = "Doe"){
        console.log(firstName + " " + lastName);
    }
    
    getName(); //John Doe
    
    

    How to check the numbers of the arguments?
    we can check the numbers of the arguments by arguments.length.

    var getName = function(firstName, lastName = "Doe"){
        console.log(arguments.length);
    }
    getName("John");  //1
    

    Even thougn the second argument get a default value, arguments.length only returns the number of arguments passed to it.

    var getPrice = function(quantity = price, price = 5){
        console.log(quantity + " ," + price);
    }
    
    getPrice();
    


    This is a TDZ reference Error.
    Because the parameters scope just between the parentheses(...).It isn't in a function body scope.
    dynamic function

    var getNumber = new Function("number = 42", "return number;");
    console.log(getNumber());
    

    summary

    the type of default arguments
    1.set a default value to the parameter in the function declaration statement itself.

    2.function default values can be any valid expression.

    3.function call

    4.We can also access the other variables in the expression used as the default value.
    Question what is dynamic function?

  • 相关阅读:
    使用 Docker 安装 Jenkins 的最佳方式
    使用 Docker 在 Linux 上托管 ASP.NET Core 应用程序
    分布式缓存 Redis 集群搭建
    [译]RabbitMQ教程C#版
    如何解决 React 官方脚手架不支持 Less 的问题
    [译]RabbitMQ教程C#版
    [译]RabbitMQ教程C#版
    快速签发 Let's Encrypt 证书指南
    [译]RabbitMQ教程C#版
    [译]RabbitMQ教程C#版
  • 原文地址:https://www.cnblogs.com/InnerPeace-Hecdi/p/8870632.html
Copyright © 2011-2022 走看看