函数声明 - 函数表达式 - 范围 [英] Function declaration - Function Expression - Scope

查看:73
本文介绍了函数声明 - 函数表达式 - 范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中,函数声明和函数表达式在范围方面有什么区别?功能声明意味着我们正在污染全球空间。是否与函数表达式相同?

In javascript, What is the difference between function declaration and function expression in terms of scope? function declaration means we are polluting the global space. Is it the same case with function expression?

函数声明

function sum(){
 // logic goes here
}

函数表达式

var sum = function(){}


推荐答案

就污染封闭范围而言,两者都是等价的。请注意,它不一定是全局范围 - 它是声明函数的范围(在其他函数中允许本地函数)。在您的示例中,两个方法都将名为 sum 的变量(函数对象)引入本地范围。

As far as polluting the enclosing scope goes, both are equivalent. Note that it is not necessarily the global scope - it is the scope in which the function is declared (local functions are permitted within other functions). In your example, both methods introduce a variable (function object) named sum into the local scope.

这篇关于函数声明 - 函数表达式 - 范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆