What this mean in javascript? [duplicate] - javascript

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.

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.

Javascript difference between store the function into variable with function name or without function name? [duplicate]

This question already has answers here:
Why use named function expressions?
(5 answers)
Closed 8 years ago.
What is the difference between these two functions;
var a = function () { console.log("hi"); }
var b = function xyz() { console.log("hi"); }
When i call these two functions both are giving same result. Then what is point in declaring function name 'xyz' ?
The second form is called a "named function expression". It gives you several benefits over normal function expressions:
It makes it a bit easier to debug -- when you have a bunch of functions like those, and you get an error, naming your functions makes it a bit easier to interpret the resulting stack trace.
You can call named functions recursively -- for example, doing:
f = function f() {
f();
};
f();
...works.
You can find more detailed information here.
Using named function expressions also comes with some disadvantages. For example, doing f = function g() { ... } will actually create two entities -- the functions f and g, which can potentially be confusing. (see comments) There are also some intricacies with scoping which can potentially cause bugs if you're not careful. You can find more detailed information here.

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

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.

Difference between 'var foo = function ...' and 'function foo() ...' [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
“Usual” functions vs function variables in JavaScript
What do you call this JavaScript syntax, so I can research it?
Is there a fundamental difference between
function foo()
{
things();
}
and
var foo = function()
{
things();
}
Or is function ... just syntactical sugar?
Thanks in advance.
They are different (but produce similar results). Basically, the first is an actual named function. The second is a regular variable declaration with an anonymous function attached to it. There are some subtle differences...they are summed up nicely here:
JavaScript Function Declaration Ambiguity (Be sure to read the comments too...more good info there)

Categories