Questions tagged [self-invoking-function]

A self-invoking function is a function that is invoked (started) automatically, without being called.

That is a function which invokes itself as soon as possible when the browser is interpreting your ecma-/javascript. Therefore, its very unlikely that you can successfully act on DOM elements here. It will be executed as soon as it is encountered in the JavaScript.

110 questions
912
votes
4 answers

JavaScript plus sign in front of function expression

I’ve been looking for information about immediately invoked functions, and somewhere I stumbled on this notation: +function(){console.log("Something.")}() Can someone explain to me what the + sign in front of the function means/does?
jOpacic
  • 9,453
  • 12
  • 36
  • 58
23
votes
3 answers

Self-invoking anonymous functions

In JavaScript, it's not uncommon to see self-invoking functions: var i = (function(x) { return x; })(42); // i == 42 While I'm certainly not comparing the languages, I figured such a construct would be translatable to C#, provided a language…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
22
votes
2 answers

Spring @Transactional Annotation : Self Invocation

I know when a transactional method is called from inside the same class it wouldn't be run in a transaction. Spring creates a proxy for transactional methods and wraps them in a try-catch block and rolls back if an exception occurs. Consider the…
15
votes
6 answers

Named Self Invoking Function

Is there ever a reason to use a named self invoking function? For example: (function foo() { alert('Hello World! Named Self Invoking Function Here'); })(); As far as my learning has taken me, this acts the same as an anonymous self invoking…
Nealbo
  • 529
  • 4
  • 20
9
votes
2 answers

What's the difference between these three form of self-invoking anonymous function?

Possible Duplicate: Are “(function ( ) { } ) ( )” and “(function ( ) { } ( ) )” functionally equal in JavaScript? I'm reading the document below. http://addyosmani.com/resources/essentialjsdesignpatterns/book/#patternity When I looked though…
synthresin
  • 931
  • 1
  • 9
  • 23
5
votes
0 answers

Spring @Cachable method within the same class (self-invocation, proxy issue) - What is the best way to solve it?

I'm trying to call a @Cacheable method from within the same class. And it didn't work. Because of: In proxy mode (the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation (in effect, a…
hynuah_iia
  • 429
  • 7
  • 14
5
votes
4 answers

Self Invoking Function as jQuery ducument ready callback

What is the difference between $(function() { // bind some event listeners }); and $(function() { // bind some event listeners }());
5
votes
3 answers

JavaScript self-invoking functions

Possible Duplicate: Difference between (function(){})(); and function(){}(); Are “(function ( ) { } ) ( )” and “(function ( ) { } ( ) )” functionally equal in JavaScript? I just wondered whether there is a difference (regarding the functionality)…
LSFR77
  • 61
  • 1
  • 4
4
votes
3 answers

What are the differences between these three types of module pattern?

1)function () { // code here... }(); 2)(function () { // code here... })(); 3)(function () { // code here... }()); What are the differences (especially third variant)? Are they all the same?
DrStrangeLove
  • 11,227
  • 16
  • 59
  • 72
4
votes
1 answer

Can Self Invoking Async function returns public functions

I'm actually new to JavaScript. So here's my question. From what I learned. I can create a public function (getHello) inside a self invoke function (Example1) & call the created public function from another self invoke function (Example2) like shown…
lala
  • 1,309
  • 9
  • 21
4
votes
3 answers

Self invoking function inside jQuery event is not working

Self invoking function defined inside jQuery event is not working, but why? $('div').on('click', function(){ $('div').text($('div').text() + 1) (function(){ $('div').text($('div').text() + 0) })(); });
vter
  • 1,881
  • 1
  • 20
  • 38
4
votes
4 answers

JS - self invoking function vs regular function

I'm a little confused. Everywhere i look, people say that i should use self invoking functions for scoping reasons, to avoid global namespace pollution. But in this aspect, self invoking function seems to act identically to regular function, as in…
user3667832
  • 257
  • 2
  • 5
  • 17
4
votes
3 answers

In JavaScript, why can't I immediately invoke function declarations?

Only functions expressions can be immediately invoked: (function () { var x = "Hello!!"; // I will invoke myself })(); But not function declarations? Is this because function declarations are hoisted and already execute immediately? EDIT:…
4
votes
4 answers

Using self invoking functions to be variable independent

Possible Duplicate: Javascript closure inside loops - simple practical example I'm trying to use a self invoking function so that each function in objects will return a different message.