What is meaning of "(function(window, undefined){})(window)" in JavaScript? [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?
(function(window, undefined){})(window);
What is the meaning of this code? I have seen it in many documents, especially in jQuery documents.
How does this work, and why is it defined thus?

You are scoping a piece of code..
By.
Defining it within an anonymous function //function(){...}
Executing it. //(function{})(args)
Also, passing the window parameter allows for faster resolution of the meaning of that variable within your block of code.

Related

When () isn't appended to the name of a JavaScript function is the result a function pointer? [duplicate]

This question already has answers here:
When do I use parentheses and when do I not?
(5 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 1 year ago.
I was going through a Svelte tutorial where they bind a function to on:click in a button and they show how if you put () after the function name it doesn't work right, it runs once when the page loads and then when you click the button the function isn't ran. They said to remove the () from the function name so that you bind to a pointer to the function instead of the function itself. If you do that then it works as expected. I did further research and it looks like pointers to functions in JavaScript don't really exist. So what's going on here? Do pointers to functions exist or do they not exist, and what's the difference in Svelte when binding to a function that has () (what's that called?) after its name and a function that doesn't?

Javascript onMouseMove event syntax [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 3 years ago.
I am working on some JavaScript code and I run into this syntax that I have never seen and I am trying hard to understand but cannot find good examples.
Can someone please describe what might be going on here?
function onMouseMove(event) {
(function(ev) {
// some piece of code
})(event);
}
This syntax is used to create an inner scope using a function and that function is immediately invoked with the event object.

What (jQuery) means in the end of function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
(10 answers)
Closed 7 years ago.
I have a function:
var waitingDialog = (function($){
.....
return{
}
})(jQuery);
Also could you explain what $ means in the function? Is it going to work without that?
It means that jQuery (if it exists) will be passed to the function. $ is merely the name that variable will take in the scope of the function.
There is a section about that in official jQuery website : https://learn.jquery.com/plugins/basic-plugin-creation/#protecting-the-alias-and-adding-scope

How is this working !function(){console.log("hi")}() [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
I read this on the javascript garden site. Can someone explain how it works?
!function(){console.log("hi")}()
The 'executing' parens at the end can't be done legally after a function expression. A typical (more sensical, IMO) way of writing this is with more parentheses:
(function(){console.log('hi')})()
By prepending the ! before the function expression, the JS interpreter reads the function and then runs it. This is because of the precedence of the ! operator vs. calling a function with the final ()
Look at this answer
tl;dr it defines a function to print out 'hi' and immediately calls it.

Why can't one set an alias to document.getElementById()? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript function aliasing doesn't seem to work
Set document.getElementById to variable
Code would be more efficient if this was possible:
var min = document.getElementById;
and then call document.getElementById() using min().
Not trying to write minified code but in this particular case one can reduce scope lookup and shorten some lines.
Is this a syntax issue or a limiation on the language?
When you call foo.bar() then this is set to foo inside bar
When you copy foo.bar to window.bar then call window.bar(), this is set to window.
getElementById has to operate on a DOM document object.

Categories

Resources