What is the difference between a function expression vs declaration in JavaScript?

 

This question already has an answer here:

What is the difference between the following lines of code?

//Function declaration
function foo() { return 5; }

//Anonymous function expression
var foo = function() { return 5; }

//Named function expression
var foo = function foo() { return 5; }

  

  • What is a named/anonymous function expression?
  • What is a declared function?
  • How do browsers deal with these constructs differently?

They‘re actually really similar. How you call them is exactly the same, but the difference lies in how the browser loads them into the execution context.

function declarations loads before any code is executed.

While function expressions loads only when the interpreter reaches that line of code.

So if you try to call a function expression before it‘s loaded, you‘ll get an error

But if you call a function declaration, it‘ll always work. Because no code can be called until all declarations are loaded.

ex. Function Expression

alert(foo()); // ERROR! foo wasn‘t loaded yet
var foo = function() { return 5; }

  

ex. Function Declaration

alert(foo()); // Alerts 5. Declarations are loaded before any code can run.
function foo() { return 5; }

  

As for the second part of your questions.

var foo = function foo() { return 5; }

  

is really the same as the other two. It‘s just that this line of code used to cause an error in safari, though it no longer does.

时间: 2024-08-05 11:09:37

What is the difference between a function expression vs declaration in JavaScript?的相关文章

Function Smackdown: Function Statement vs. Function Expression

I’ve recently been reading ‘JavaScript: The Good Parts,’ by Douglas Crockford. This concise book defines a subset of core JavaScript that’s robust and powerful: The Good Parts. Appendix B identifies The Bad Parts, and sets our competitors - the funct

[JavaScript]function expression(函数陈述式) VS declaration (函数运算式)

摘要:[JavaScript]function expression(函数陈述式) VS declaration (函数运算式) 先前写过一篇[Javascript]Call method(调用函数)关于函数声明,这边进阶一下做一个比较. 但在开始前, 先来回忆一下如何自定一个JS函数 How to create JS custom function 第一种 - declaration (函数运算式) function callTest(){ console.log(123); } callTe

使用"立即执行函数"(Immediately-Invoked Function Expression,IIFE)

一.原始写法 模块就是实现特定功能的一组方法. 只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. function m1(){ //... } function m2(){ //... } 上面的函数m1()和m2(),组成一个模块.使用的时候,直接调用就行了. 这种做法的缺点很明显:"污染"了全局变量,无法保证不与其他模块发生变量名冲突,而且模块成员之间看不出直接关系. 二.对象写法 为了解决上面的缺点,可以把模块写成一个对象,所有的模块成员都放到这个对象里面.

Javascript---Immediately-Invoked Function Expression (IIFE)立即执行的函数表达式

1.一下是几种形式的函数调用: 各种调用的效率:在这编文章中有谈到: http://suqing.iteye.com/blog/1981591 // Either of the following two patterns can be used to immediately invoke // a function expression, utilizing the function's execution context to // create "privacy." (funct

js 立即调用函数 IIFE(Immediately Invoked Function Expression) 【转】

原文链接:https://www.cnblogs.com/ming-os9/p/8891300.html 1 (function(){...})() 3 (function(){...}()) 这是两种js立即执行函数的常见写法. 基本概念: 函数声明:function fname(){...}; 使用function关键字声明一个函数,再指定一个函数名. 函数表达式:var fname=function(){...}; 使用function关键字声明一个函数,但未给函数命名,最后将匿名函数赋予

Javascript自执行函数 (Immediately-Invoked Function Expression)

1. Javascript编译器在遇到function这个关键字时,默认认为它是function声明,而不是表达式. 2. 在已声明的function后面加括号,即可调用它.比如, function foo(){console.log('hello javascript.')} foo(); 3. 括号里面允许javascript表达式,(function(){console.log('hello javascript')}), 这样这个匿名函数就是一个表达式,而不是function的声明.根据

What is the difference between the ways to implement inheritance in javascript.

see also : http://www.w3school.com.cn/js/pro_js_inheritance_implementing.asp http://davidshariff.com/blog/javascript-inheritance-patterns/ Object masquerading with Javascript Posted on October 7, 2010 by Amit Agarwal Often people follow this common a

Function Expression

One of the key characteristics of function declarations is function declaration hoisting, whereby function declarations are read before the code excutes. That means a function declaration may appear after code that calls it and still work: 1 sayHi();

匿名函数立即调用的函数表达式 -IIFE(Immediately-Invoked Function Expression)

javascript 匿名函数有哪几种执行方式: ( function() {}() ); ( function() {} )(); [ function() {}() ]; ~ function() {}(); ! function() {}(); + function() {}(); - function() {}(); delete function() {}(); typeof function() {}(); void function() {}(); new function() {