(function(symbolName) { - What does this mean [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What do parentheses surrounding a JavaScript object/function/class declaration mean?
What does (function($) {})(jQuery); mean?
I am trying to understand how Edge works so I can use my own code,
I have not come accross this before, but what does this mean:
(function(symbolName) {
//CODE
})("stage");

It's an anonymous function that is defined and then called with the argument "stage"

It is the similar to doing:
var myfunc = (function (symbolName) {
//CODE
});
myfunc("stage");
OR
function myfunc(symbolName) {
//CODE
}
myfunc("stage");
except that when the function is defined in either of these ways it will be 'hoisted' to the top of the block scope - but thats a whole other topic.

In Javascript you can ddefine anonymous functions by simply typing:
(function(){alert("Hello")}); /* ok, this do nothing, but it is correct */
It is also possible to call a function directly:
(function(){alert("Hello")})(); /* alert is displayed */
If the function has arguments, you have to specify the arguments:
(function(args){alert(args)})("Hello"); /* alert is displayed with the passed arguments */
I suggest you this tutorial.

Related

Disallowing function to be called from Console in Javascript [duplicate]

This question already has answers here:
How to disable JavaScript function calls from the browser console?
(6 answers)
Closed 3 years ago.
Alright so I've a function (not exactly the one written bellow, that one is just an example)
function test()
{
alert("Hello");
}
Now the problem is that people are able to go into console and just type "test()" and call the function.
Is there a way to prevent this? I dont want of people to call functions from the console.
You can narrow the context that your function is executed in, eg:
(function() {
function test() { /* Do anything */ }
test();
// Call test anywhere here
})()
test(); // Error: test is undefined

Javascript function declaration - different behaviour? [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 5 years ago.
By trying to override a function I occurred in this odd behaviour. I hope to find an answer after having searched and read about function declaration methods without success.
In a script, if I declare this
var someFunction = function(){
alert("a");
}
someFunction();
someFunction = function(){
alert("b");
}
By calling someFunction I will have an output of "a"
But if I declare the two functions in this way
function someFunction(){
alert("a");
}
someFunction();
function someFunction(){
alert("b");
}
My output will be "b"
What is the difference here? I understand the first example is assigning to a variable an anonymous function. But the second example is totally unexpected and new to me.
I tested on all browsers and the output is the same.
The difference is the fact you are calling an anonymous function in the first example, Javascript gets evaluated top to bottom. In the case of an anonymous function it isn't actually executed UNTIL it is called later on.

call self invoking function from outside body function [duplicate]

This question already has an answer here:
Function in if condition clause
(1 answer)
Closed 6 years ago.
So i have the following code:
(function init_chart(){
// body function
})();
it's working as expected. but when i tried to call the init_chart() from outside the function, console said it is undefined. then i tried to add the following line inside init_chart() function:
window['init_chart'] = this;
now console said init_chart is not a function.
You are creating a named function expression function, so basically the function is not defined on the global scope. The easiest way to get around it is to do the following:
function init_chart(){
// body function
};
init_chart();

Self Invoking functions difference [duplicate]

This question already has an answer here:
What is this javascript syntax? !function(){} [duplicate]
(1 answer)
Closed 7 years ago.
What is the difference between these two self invoking functions? The function does not work if the ! (not) symbol is added.
Please clarify if any one has clear understanding.
// first
(function( $ ) {
// ...
})( jQuery );
// second
!function($){
alert("test1");
}(jQuery), function(){
alert("test2");
}(jQuery);
!function () {} means negative function (similar to if (!someVar) {}), to make sure what result is before making it negative, function must be executed. So ! can be replaced with +.
You have something wrong with //second, because you create two anonymous functions and between there is ,. I think you are getting syntax error in this place, so next function does not work (and is not executed, because no one is calling this function)

What this mean in javascript? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Explain JavaScript's encapsulated anonymous function syntax
I have just read a javascript book but I have seen this code:
1(function() {
// code
})();
what is this ? is a special function ?
As written, it has a syntax error.
I'm guessing it was more like:
(function() {
// code
})();
or
(function() {
// code
}
)();
Break it down:
(FOO)() // calls FOO with no arguments.
And
function() { //creates a function that takes no arguments.
// code
}
Hence together it would create a function that takes no arguments, and then call it. I can't see why you would apart from just showing that you can.
It looks like the intent was to declare the function inline/anonymous and immediately execute it.

Categories