Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中将函数分配给变量&;用声明来称呼它_Javascript_Function - Fatal编程技术网

在Javascript中将函数分配给变量&;用声明来称呼它

在Javascript中将函数分配给变量&;用声明来称呼它,javascript,function,Javascript,Function,我来自Python背景&最近开始学习Javascript。我对函数是如何分配给变量的,以及它们是如何返回数据的有点挂念 请考虑以下对象: const stats = { max: 53.8, min: 34.9, std: 6.5 }; 当我刚刚声明一个普通函数&log时,我得到了适当的输出: function half(stats) { return (stats.min + stats.max) / 2.0; }; console.log(half(sta

我来自Python背景&最近开始学习Javascript。我对函数是如何分配给变量的,以及它们是如何返回数据的有点挂念

请考虑以下对象:

const stats = {
    max: 53.8,
    min: 34.9,
    std: 6.5
};
当我刚刚声明一个普通函数&log时,我得到了适当的输出:

function half(stats) {
    return (stats.min + stats.max) / 2.0;
};

console.log(half(stats));
然而,我所指的教程使用了一种有点奇怪的方式来完成同样的事情,如下所示:

const half = (function() {

    return function half(stats) {
        return (stats.min + stats.max) / 2.0;
    };
})();

console.log(half(stats));
我不明白的是:

  • 需要两个嵌套返回(函数似乎相当简单,我不明白为什么需要两个返回)
  • 函数
    关键字之前开始的括号&以较大的块结束
  • 最有趣的是,
    ()在整个块的末尾,这意味着函数一经声明就被调用
  • 此外,当我试图用我在Python中的经验以更简单的方式重写相同的内容时,这似乎也能起到作用。像这样:

    const half = function(stats) {
        return (stats.min + stats.max) / 2.0;
    };
    
    console.log(half(stats));
    
    有人能给我解释一下教程提供的代码中发生了什么吗?另外,也许你可以回答我上面提出的3个问题。谢谢大家!

    请参考此。 (接近末尾)

    请参阅此。
    (接近尾声)

    这里有几个概念你必须知道

    功能声明

     function half(stats) {
            return (stats.min + stats.max) / 2.0;
        };
        
        console.log(half(stats));
    
     const half = function(stats) {
            return (stats.min + stats.max) / 2.0;
        };
        
        console.log(half(stats));
    
    const half = (function() {
    
        return function half(stats) {
            return (stats.min + stats.max) / 2.0;
        };
    })(); // this will immediately execute without any invocation
    
    console.log(half(stats));
    
    函数表达式

     function half(stats) {
            return (stats.min + stats.max) / 2.0;
        };
        
        console.log(half(stats));
    
     const half = function(stats) {
            return (stats.min + stats.max) / 2.0;
        };
        
        console.log(half(stats));
    
    const half = (function() {
    
        return function half(stats) {
            return (stats.min + stats.max) / 2.0;
        };
    })(); // this will immediately execute without any invocation
    
    console.log(half(stats));
    
    阅读更多-

    立即调用的函数表达式

     function half(stats) {
            return (stats.min + stats.max) / 2.0;
        };
        
        console.log(half(stats));
    
     const half = function(stats) {
            return (stats.min + stats.max) / 2.0;
        };
        
        console.log(half(stats));
    
    const half = (function() {
    
        return function half(stats) {
            return (stats.min + stats.max) / 2.0;
        };
    })(); // this will immediately execute without any invocation
    
    console.log(half(stats));
    
    请在此阅读更多信息-


    闭包-

    这里有几个概念你必须知道

    功能声明

     function half(stats) {
            return (stats.min + stats.max) / 2.0;
        };
        
        console.log(half(stats));
    
     const half = function(stats) {
            return (stats.min + stats.max) / 2.0;
        };
        
        console.log(half(stats));
    
    const half = (function() {
    
        return function half(stats) {
            return (stats.min + stats.max) / 2.0;
        };
    })(); // this will immediately execute without any invocation
    
    console.log(half(stats));
    
    函数表达式

     function half(stats) {
            return (stats.min + stats.max) / 2.0;
        };
        
        console.log(half(stats));
    
     const half = function(stats) {
            return (stats.min + stats.max) / 2.0;
        };
        
        console.log(half(stats));
    
    const half = (function() {
    
        return function half(stats) {
            return (stats.min + stats.max) / 2.0;
        };
    })(); // this will immediately execute without any invocation
    
    console.log(half(stats));
    
    阅读更多-

    立即调用的函数表达式

     function half(stats) {
            return (stats.min + stats.max) / 2.0;
        };
        
        console.log(half(stats));
    
     const half = function(stats) {
            return (stats.min + stats.max) / 2.0;
        };
        
        console.log(half(stats));
    
    const half = (function() {
    
        return function half(stats) {
            return (stats.min + stats.max) / 2.0;
        };
    })(); // this will immediately execute without any invocation
    
    console.log(half(stats));
    
    请在此阅读更多信息-



    闭包-

    除非它不在任何变量上闭包。这只是一种生活。虽然这在理论上可以回答这个问题,但在这里包括答案的基本部分,并提供链接供参考。我会注意这一点。谢谢你的建议@除了它没有关闭任何变量。这只是一种生活。虽然这在理论上可以回答这个问题,但在这里包括答案的基本部分,并提供链接供参考。我会注意这一点。谢谢你的建议@巴马里特是一名律师。但是在这种情况下我看不出它有什么用途。你能给教程添加一个链接吗?也许有一些额外的背景可以解释他们为什么这样做。这能回答你的问题吗@九个细节哪一个答案解释了为什么在这种情况下使用它?生活中没有需要隐藏的范围。@Barmar我不看OP提到的教程就说不出来,因此我没有提到具体的答案。这是一个问题。但是在这种情况下我看不出它有什么用途。你能给教程添加一个链接吗?也许有一些额外的背景可以解释他们为什么这样做。这能回答你的问题吗@九个细节哪一个答案解释了为什么在这种情况下使用它?IIFE中没有需要隐藏的范围。@Barmar我不看OP提到的教程就说不出来,因此我没有提到具体的答案。那么,对于IIFE,它是否能够立即执行而不带任何参数,因为它只返回未被调用的内部函数?如果是这样,内部功能和立即执行的目的是什么?像您的示例中那样使用函数表达式不是更简单吗?不需要用例IIFE。那么,对于IIFE,它是否能够立即执行而不带任何参数,因为它只返回未调用的内部函数?如果是这样,内部功能和立即执行的目的是什么?像您的示例中那样使用函数表达式不是更简单吗?不需要用例IIFE。