TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: JS, what is this called and how does it work?

4 pointsby uptownhralmost 10 years ago
(function(test){console.log(test)})(1)<p>I don&#x27;t think this really exists in other languages outside of JS. Where you can send in a parameter this way. I understand that the test variable becomes what ever you pass in the tail end but how does this work? What is this called so I can better ask other JS developers?

4 comments

cabirumalmost 10 years ago
Immediately Invoked Function Expressions (IIFE).<p><a href="http:&#x2F;&#x2F;benalman.com&#x2F;news&#x2F;2010&#x2F;11&#x2F;immediately-invoked-function-expression&#x2F;" rel="nofollow">http:&#x2F;&#x2F;benalman.com&#x2F;news&#x2F;2010&#x2F;11&#x2F;immediately-invoked-functio...</a><p>Not unique to js, e.g. upcoming php v7 will support these.
ayersalmost 10 years ago
What you have is a self executing anonymous function. Now you know the name you will be able to find out all the information you need. For example: <a href="http:&#x2F;&#x2F;esbueno.noahstokes.com&#x2F;post&#x2F;77292606977&#x2F;self-executing-anonymous-functions-or-how-to-write" rel="nofollow">http:&#x2F;&#x2F;esbueno.noahstokes.com&#x2F;post&#x2F;77292606977&#x2F;self-executin...</a>
评论 #9732525 未加载
echeesealmost 10 years ago
This is the same as:<p><pre><code> var func = (function(test){console.log(test)}); func(1); </code></pre> What&#x27;s going on is you&#x27;re creating an anonymous function, and then calling it.
uptownhralmost 10 years ago
So after looking at this longer, it makes a lot more sense. Basically, things are evaluated left to right. The first (function(){}) evaluates the function(){} and returns the function. Then the function is called with the second (1), and passing that in as a param.
评论 #9732535 未加载