function-expression

Why use named function expressions?

青春壹個敷衍的年華 提交于 2019-12-16 19:32:20
问题 We have two different way for doing function expression in JavaScript: Named function expression (NFE) : var boo = function boo () { alert(1); }; Anonymous function expression : var boo = function () { alert(1); }; And both of them can be called with boo(); . I really can't see why/when I should use anonymous functions and when I should use Named Function Expressions. What difference is there between them? 回答1: In the case of the anonymous function expression, the function is anonymous —

Function declaration or function expression

假如想象 提交于 2019-12-10 13:39:54
问题 I just ran into a problem when defining a function in a block scope. Consider the following program: try { greet(); function greet() { alert("Merry Christmas!"); } } catch (error) { alert(error); } I expected this program to alert Merry Christmas! . However in Firefox is gives me the following ReferenceError : ReferenceError: greet is not defined On Opera and Chrome it alerts the greeting as I expected it to. Evidently Firefox treats the function inside the block scope as a FunctionExpression

function declaration and function expression performance difference

送分小仙女□ 提交于 2019-12-01 01:40:17
I have used JSperf to test a small sample of code . According to a few articles I came across, both should have similar performance with test2 having a little edge. But here it's the complete opposite. Can someone explain why the huge difference? Edit : I also understand the differences between both of them. Please don't mark this as a duplicate of this or other questions which talk about the semantic differences and do not answer my question regarding performance. Thank you. With the powerful optimizations JavaScript engines are using these days, Micro-benchmarks like this produce somewhat

function declaration and function expression performance difference

女生的网名这么多〃 提交于 2019-11-30 19:41:59
问题 I have used JSperf to test a small sample of code. According to a few articles I came across, both should have similar performance with test2 having a little edge. But here it's the complete opposite. Can someone explain why the huge difference? Edit : I also understand the differences between both of them. Please don't mark this as a duplicate of this or other questions which talk about the semantic differences and do not answer my question regarding performance. Thank you. 回答1: With the

What is the point of using a named function expression? [duplicate]

别等时光非礼了梦想. 提交于 2019-11-28 11:35:11
This question already has an answer here: var functionName = function() {} vs function functionName() {} 38 answers Why use named function expressions? 5 answers I'm going through this blog about the difference between function declarations and function expressions. It gives these two examples. They call the first an "anonymous function expression" and the second a "named function expression." // anonymous function expression var a = function(){ return 3; } // named function expression var b = function bar(){ return 3; } I tested these two in Chrome's JS console and I see the following: a() =>

Where is the immutable binding record of the identifier in a named function expression stored in JavaScript?

两盒软妹~` 提交于 2019-11-28 08:18:36
问题 Recently I ran into some interesting facts about named function expressions (NFE). I understand that the function name of an NFE can be accessed within the function body, which makes recursion more convenient and saves us arguments.callee . And the function name is not available outside the function body. For example, var foo = function bar() { console.log(typeof bar); }; typeof foo; // 'function' typeof bar; // 'undefined', inaccessible outside the NFE foo(); // 'function', accessible inside

Why can’t I assign values to a variable inside a named function expression with the same name?

为君一笑 提交于 2019-11-27 22:27:27
This is a named function expression with the name test . Inside, I assign 123 to a variable, also named test . Then test is logged. The function prints its body in the console, but not 123 . What is the reason for such behavior? (function test() { test = 123; console.log( test ); }()); Where does my explanation of function execution fail? Start of function execution: test is a local variable that references the function itself Local variable test is reassigned to number 123 console.log(test) shows the number 123 . I believe this piece of the ecma spec explains this behavior. This relates

What is the point of using a named function expression? [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-11-27 06:19:17
问题 This question already has an answer here: var functionName = function() {} vs function functionName() {} 38 answers Why use named function expressions? 5 answers I'm going through this blog about the difference between function declarations and function expressions. It gives these two examples. They call the first an "anonymous function expression" and the second a "named function expression." // anonymous function expression var a = function(){ return 3; } // named function expression var b

Why can’t I assign values to a variable inside a named function expression with the same name?

最后都变了- 提交于 2019-11-26 20:59:10
问题 This is a named function expression with the name test . Inside, I assign 123 to a variable, also named test . Then test is logged. The function prints its body in the console, but not 123 . What is the reason for such behavior? (function test() { test = 123; console.log( test ); }()); Where does my explanation of function execution fail? Start of function execution: test is a local variable that references the function itself Local variable test is reassigned to number 123 console.log(test)

Calling a javascript function recursively

China☆狼群 提交于 2019-11-26 14:04:55
I can create a recursive function in a variable like so: /* Count down to 0 recursively. */ var functionHolder = function (counter) { output(counter); if (counter > 0) { functionHolder(counter-1); } } With this, functionHolder(3); would output 3 2 1 0 . Let's say I did the following: var copyFunction = functionHolder; copyFunction(3); would output 3 2 1 0 as above. If I then changed functionHolder as follows: functionHolder = function(whatever) { output("Stop counting!"); Then functionHolder(3); would give Stop counting! , as expected. copyFunction(3); now gives 3 Stop counting! as it refers