what does this symbol '()' do after closing '{}'in javascript [duplicate] - javascript

This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
Closed 8 years ago.
I'm really sorry with the title, and im sorry if im asking this noob questions here, because i don't know what the keyword for this in google.
Ok, first i'm new to javascript and still learning this programming language. so i've seen this alot, but i don't know what it's mean
var myapp = function(){
var a = 'a';
var b = function(){
//some code goes here
}
return {
init: function(){
b();
//some code goes here
}
}
}() <-- what is it?;
So i've been wondering what this symbol '()' do at the end. and why many people writing a function inside a variable?

That means that the function is being exectued right after its declaration
On the other hand, declaring the function this way:
var myapp = function(){
lets you use it as an object..
Then you can use myapp.init() for example

Related

var someSome = (function () { ... } ()); a weird javascript asking? [duplicate]

This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
Closed 8 years ago.
I cannot think of the term that JavaScript developers use for this kind of practice.
var someSome = (function () { ... } ());
-- Wrapping function within brackets. I vaguely remember someone was calling this fiif? fiff?
And there were many advantages and it was recommended java scripting practice.
Anyone got clue what I am talking about and why is it a good practice?
it might have been even without assignment like below
(function () { ... } ());
Self executing function. Often used as a wrapper around a function block to immediately invoke it, as well as give it closure.

Create a variable named after a string parameter in JavaScript [duplicate]

This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 8 years ago.
I know this piece of code is wrong, but I wonder if there is any way this would be possible.
var createVariableFromParameter = function(myName) {
var myName = [];
return myName;
}
Thank you.
EDIT: I found the solution here: Convert string to variable name in Javascript
I think it's not possible, and even if it could be done, why would you want to do it? There is no meaning in creating variables with random names...
I don't know why you should need it??
But I think true way is :
function create(myvar) { myvar = [];}
var a;
create(a);
function create() { return [];}
var b = new create();

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.

Awkward way of executing JavaScript code [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Why is this function wrapped in parentheses, followed by parentheses? [duplicate]
(6 answers)
Closed 9 years ago.
In the Google tutorial for implementing Google+ sign-in in Flask application, I discovered that the developer often uses an awkward way of executing JavaScript code:
Instead of doing
var a = foo(bar);
I see this:
var a = (function() {
return foo(bar);
})();
What is the reason to do it the weird way?
This is a poor example. Consider the following:
var a = (function() {
var ret = {};
ret.test = "123";
function imPrivate() { /* ... */ }
ret.public = function() {
imPrivate();
}
return ret;
})();
console.log(a)
a will contain the varible test and the function public, however you can not access imPrivate. This is the common way to handle public vs private variables;
See Why is this function wrapped in parentheses, followed by parentheses? for more info.
var a = (function() {
return foo(bar);
})();
In this case this is really unnecessary, but this is not wrong and it will not throw an error.
But IIF some times uses like module pattern:
var a = (function() {
/* some other code in own scope */
return foo(bar);
})();
In this case IIF is just a module which exports something outside.
The closure function is used to encapsulate some of the attributes / methods in the function. Much like the private / public principle from other languages.
You can find more on this topic here under Module Pattern

Why is there a difference in the way functions are created in javascript? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript: var functionName = function() {} vs function functionName() {}
I have code with functions defined in two ways:
var retrieveData = function (statusValue, statusText)
{
...
}
function retrieveData(statusValue, statusText) {
..
}
retrieveData(1,2);
Can someone explain what the difference is. Seems that the second way of setting up the function is much simpler.
The 1st example creates a pointer to the function stored in the variable retrieveData, this way you can pass functions like any other variable and retrieve and use them dynamically. Other languages have similar constructs.

Categories

Resources