Javascript onMouseMove event syntax [duplicate] - javascript

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.

Related

Why I use 'console.log(this)' and the this do not bind to console? [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 2 years ago.
Now I have some code:
window.testFun = {
say() {
console.log(this);
}
};
window.testFun.say(); // print say
window.console.log(this); // why not console but window?
I am sorry that my English is not very good, but I am actively learning English now, and I hope to be able to actively discuss the code with everyone
The value of this is defined by the function this is used inside.
You are using it in the global scope. You are just passing it to log. It isn't a keyword inside the log function itself (which is built into the browser and you aren't looking at the source code for it).

What is `(function(document) { ... }(document));` [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 5 years ago.
To be honest, firstly I thought I could easily find answer in Google, but strange, there are very little information about it.
Could anybody explain, what does this code do? What is the advantage (purpose) of using this technique?
(function(document) {
...
}(document));
This is an Immediately-Invoked Function Expression (IIFE) which is basically a function that is declared and called all in one go. It's purpose is to encapsulate logic without muddying up the global namespace (any variables that you declare in that function are only visible in the function). You can find more information here, or just Google IIFE.

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

Javascript - what is the purpose of wrapping functions or codes in a function? [duplicate]

This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
This probably is not a new question, but where is the purpose of wrapping a function or codes inside ((function () {...})());? for instance,
//Self-evoking anonymous functions
((function () {
alert("hi");
})());
What's the difference with no wrap,
alert("hi");
I still get the same result - hi
What can you pass/ put in the brackets in the end bit - })());? and why?
Using a function creates a scope. You can have params inside and do more than just alerting.
Now you can do the same without a function, but then you will keep the state on the window object and thats something that you would like to prevent in some cases.

what's the purpose and meaning of e in jquery code [duplicate]

This question already has answers here:
jquery/javascript: function(e){.... what is e? why is it needed? what does it actually do/accomplish?
(4 answers)
What is the purpose of this? (function ($) { //function code here })(jQuery);
(4 answers)
Closed 8 years ago.
In the following code, there are two 'e's, are they about the same object/type or actually about different things?
(function(e) {
var t = {
init: function() {
e(".pic").length && this.show()
}
};
window.Booth = t;
})(jQuery);
Also, I am a little confused with the overall semantics of the code snippet above, any documentation out there can explain it?
In this case, it's an alias for jQuery. Usually people use $, but in this case they didn't.
what you have is an anonymous, self-executing function.
the function is passed the jquery object (which is a function). e(".pic") is the same as $(".pic") or jQuery(".pic") because e is just a reference to jQuery.

Categories

Resources