TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

“var functionName” vs “function functionName”

141 点作者 baptou12大约 11 年前

15 条评论

yiransheng大约 11 年前
An example case where &quot;var&quot; method is useful:<p><pre><code> function make_dot_product(dim) { if (dim === 3) { var dot = function(a,b) { return a.x*b.x+a.y*b.y+a.z*b.z } } else if (dim === 2) { var dot = function(a,b) { return a.x*b.x+a.y*b.y } } return dot } </code></pre> vs.<p><pre><code> function make_dot_product(dim) { if (dim === 3) { function dot(a,b) { return a.x*b.x+a.y*b.y+a.z*b.z } } else if (dim === 2) { function dot(a,b) { return a.x*b.x+a.y*b.y } } return dot } </code></pre> The second case is erroneous, as it will always return 2-dimensional dot function (even if you call make_dot_product(20)). The is because function nameSpace() is defined at parse time, so inside the make_dot_product scope, two instance of dot functions are created, with the second overwriting the first. If we use var f = function() {...} this can be avoided.
评论 #7674341 未加载
评论 #7674303 未加载
评论 #7675485 未加载
评论 #7674757 未加载
scarmig大约 11 年前
An additional wrinkle: recursive functions. If you&#x27;ve got a recursive function, you absolutely must use a function declaration to introduce the function identifier. Otherwise you inadvertently create a closure. Consider:<p><pre><code> var a = function(b) { if (b === 0) { return true; } return a(--b); }; var b = a; a = function() { return false; }; b(5); </code></pre> As an alternative, you should go<p><pre><code> var a = function a2(b) { if (b === 0) { return true; } return a2(--b); }; var b = a; a = function() { return false; }; var a2 = function() { return false; }; b(5); </code></pre> Perhaps counterintuitively, a2 doesn&#x27;t leak into the containing scope, which makes it safe to use within the function it declares.<p>Some older browsers fuck this up though, IIRC.
评论 #7674712 未加载
AdrianRossouw大约 11 年前
i know it&#x27;s sort of side effect driven, but i like function hoisting.<p>it&#x27;s one of my main tools to fight the callback pyramid of doom.<p>use named functions, place them below the code that uses them.<p>all flat.
评论 #7673030 未加载
评论 #7672734 未加载
评论 #7672599 未加载
mattmanser大约 11 年前
I honestly don&#x27;t understand people who regularly use variables to declare functions (with the exceptions of the <i>extremely</i> rare cases it might be useful).<p>My POV is you simply shouldn&#x27;t be putting a function in a variable, it&#x27;s supposed to be reusable code and you&#x27;ve just made it&#x27;s position a completely pointless dependency as well as obfuscated the code&#x27;s meaning.<p>A named function is clearly supposedly immutable, reusable code (save monkey patching). A function in a variable looks like you might change it.<p>I want to stress, there are very good reasons to assign a function to a variable. There are also very good reasons to declare a function within the scope of another too. But they are few and far between.<p>To willy nilly declare functions like they&#x27;re variables. Bizarre. Perhaps someone who does it can explain what it gains them? The place I first saw it was in Crockford&#x27;s the Good Parts, but he seems to have stopped doing it when you go look at his code.
评论 #7672820 未加载
评论 #7672662 未加载
评论 #7673115 未加载
评论 #7673224 未加载
评论 #7672753 未加载
评论 #7674020 未加载
评论 #7672768 未加载
评论 #7676427 未加载
评论 #7674146 未加载
schrodinger大约 11 年前
One big difference is that:<p><pre><code> function functionName() </code></pre> will show up in a stack trace with a name, whereas the other won&#x27;t.<p>You can also do this:<p><pre><code> var f = function functionName() { }</code></pre>
评论 #7672717 未加载
评论 #7672942 未加载
评论 #7672711 未加载
reconor大约 11 年前
Using var to declare a variable later assigned to an anonymous function can help with managing larger JavaScript projects where scoping and data member access is important. Here is a pattern I tend to apply rather frequently, included is an example for a custom instance object, which would be created with &#x27;new&#x27;, and a Namespace object:<p><pre><code> var MyObject; (function() { var staticPrivateVariable; MyObject = function(opts) { &#x2F;&#x2F; Do Initialization Stuff }; MyObject.prototype.publicInstanceFunction = function(opts) { &#x2F;&#x2F; Do instance stuff }; })(); var Namespace; (function() { var staticPrivateVariable; Namespace = function(opts) { &#x2F;&#x2F; Do Initialization Stuff }; Namespace.publicFunction = function(opts) { }; function privateFunction(opts) { &#x2F;&#x2F; Do some private stuff } })();</code></pre>
lotides大约 11 年前
I&#x27;ve been trying to learn to code (I&#x27;m a designer) for a while now. I&#x27;ve read books, played around with making things and I&#x27;m now taking the JavaScript course on Codecademy. It taught me:<p><pre><code> var functionName = function() { &#x2F;&#x2F; whatever } </code></pre> I had no idea there were other ways to write a function. I still don&#x27;t understand what the benefits or drawbacks are of each method. Maybe I haven&#x27;t made it that far yet. But I literally have no idea what most of you are talking about in this thread. I feel like I visited a post written in chinese.<p>The scope of learning to code seems overwhelming. Finding small bugs in syntax and design is stressful. What I&#x27;m supposed to learn (language, style, syntax, frameworks) seems to change faster than I can learn it. I wish I would have started all of this earlier ... like when I was 10.
评论 #7675073 未加载
评论 #7674775 未加载
评论 #7674804 未加载
评论 #7674971 未加载
评论 #7674964 未加载
grumblestumble大约 11 年前
I&#x27;m &#x27;ardkore and rigidly enforce `var functionName = function functionName() {}`, under threat of flaying.
评论 #7673821 未加载
评论 #7673666 未加载
subbu大约 11 年前
Its called function hoisting. Ref: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a>
评论 #7672420 未加载
yawboakye大约 11 年前
The primary difference is &quot;hoisting&quot; <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a>. Closure can be created in both cases.<p>Also, functions have names.<p><pre><code> var funcName = function () {}; funcName.name; &#x2F;&#x2F; &quot;&quot; function func2() {}; func2.name; &#x2F;&#x2F; &quot;func2&quot; </code></pre> Thus, anynomous functions are truly <i>anonymous</i> whether or not they&#x27;re assigned to a variable. `func2` can be assigned to a variable too. It&#x27;d retain its name (`func2`).
yiransheng大约 11 年前
One pattern I have seen emerging is<p><pre><code> Object.prototype.method = function Object$method() { ... } </code></pre> an interesting mixture of both method.
gopalv大约 11 年前
Neither would be any good, really for better coding.<p>namespace.functionName = function() { }<p>I&#x27;m not talking about performance here, but from a sheer maintainability nightmare that JS creates. If you care about this, don&#x27;t let your functions be unbound.<p>The only reason to store a function in a var is because it is a closure.<p>And in a way, a closure is data encapsulation in a totally non OO way.
评论 #7675776 未加载
评论 #7676595 未加载
badman_ting大约 11 年前
I recommend named functions, because a stack trace 12 deep of all anonymous functions is a real bummer.
yiedyie大约 11 年前
But:<p><pre><code> functionOne = function() { &#x2F;&#x2F; Some code</code></pre> };<p>(i.e. without the var) is represented same as:<p><pre><code> function functionTwo() { &#x2F;&#x2F; Some code</code></pre> }<p>is it?
评论 #7672593 未加载
niix大约 11 年前
Also hoisting.