About javascript function [duplicate] - javascript

This question already has answers here:
JavaScript plus sign in front of function expression
(4 answers)
Closed 9 years ago.
I found a strange behaviour in Javascript:
function() {
return 10;
}();
This construction doesn't work on all browser because it has a syntax error. But this construction works (returns ten):
+function() {
return 10;
}();
Why?

The + lets the js engine make the difference between this function expression and a function definition.
For more readability, we usually use
(function() {
return 10;
})();
See related article

Related

What does this syntax mean: const foo = () => {} [duplicate]

This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 5 years ago.
I have recently come across this code with which I am unfamiliar:
const foo = () => {
/*code block here*/
}
As far as I can tell, it means the same thing as:
const foo = function () {
/*code block here*/
}
Is that a correct assumption, or are there differences?
What is the correct name to refer to this bit of code?
What exactly is the '=>' doing? I've never seen it in Javascript before.
This is ES6 arrow function. It's basically same as function (){},
with some differences such as not rebinding this.
Reference on MDN

What are the difference between these two closure syntax? [duplicate]

This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 7 years ago.
I often use the closure syntax
var something = (function () {
//TODO: do something
}());
and, I often find people use this syntax
var something = (function () {
//TODO: do something
})();
If both the two behaves the same way, what are the differences between the two?
There's no real difference. Both statements contain function expressions that evaluate to functions that are immediately executed.

What's the difference between these two IIFE invocation syntaxes? [duplicate]

This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 7 years ago.
(function f() {
// do something
}());
vs
(function g() {
// do something
})();
Note the position of the ending parentheses.
Do these execute in the same manner?
Do they restrict the scope of variables differently?
They are the same. Just two notations to do the same thing.
Some people find one notation more intuitive than the other. Just a matter of preference, nothing more than that.

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