call self invoking function from outside body function [duplicate] - javascript

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();

Related

Invoking function declaration in JavaScript [duplicate]

This question already has answers here:
What are the precise semantics of block-level functions in ES6?
(2 answers)
Function declaration in block moving temporary value outside of block?
(1 answer)
Closed 7 months ago.
I have a doubt regarding how the function declaration are invoked in JavaScript.
I have read somewhere that function declaration can be accessed anywhere within the function it was declared on.
Lets say I declare a function within a block of code so it will be accessible anywhere or maybe outside the block as well.
But when I try to invoke the function before the block of code, I get an TypeError. But this error is not happening when I invoke the function after the block of code. Please explain why the function is not getting invoked before the block of code.
function globalFunc() {
//..
// ..
funcName(); // not accessible - TypeError
{
function funcName() {
console.log("Hey");
}
}
funcName(); // accessible
}
globalFunc();

Unexpected behaviour calling function from function [duplicate]

This question already has answers here:
What is the difference between a function call and function reference?
(6 answers)
Closed 2 years ago.
Recently I started learning about JS, and I got stuck in the following issue:
I am trying to call a function "draw_point" when a "mousemove" event happens.
When trying the following, the code works as expected (the function gets called):
svg.on('mousemove', draw_point(true));
But when replacing the caller function like this, it stops working and I don't get any error messages to troubelshoot.
svg.on('mousemove', function () {
draw_point;
});
Any explanation of what is going on?
Both examples are wrong, but for different reasons. In the first, you're calling the functiondraw_point immediately and passing its return value to the .on call. In the second, you're creating an anonymous function that wraps draw_point, but you're not calling draw_point. This is legal because javascript allows empty statements like myVar; or 3;.
What you want to do is pass the function draw_point, but not call it. It will be called when the handler runs:
svg.on('mousemove', draw_point);

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.

Why javascript function parentheses can not access the outside? [duplicate]

This question already has answers here:
Why a Name Function Expression not available outside function body [duplicate]
(3 answers)
How do JavaScript closures work?
(86 answers)
Closed 7 years ago.
I'm curious, why not just javascript function scope it? Why just add a parenthesis can not access it? My guess is that the parentheses and javascript related operations, but do not know exactly why this child principle and design?
(function test(){
console.log( test );
})();
test();//Uncaught ReferenceError: test is not defined IE8- is ok
or
(function test(){
console.log( test );
});
test();//Uncaught ReferenceError: test is not defined IE8- is ok
When you wrap a function in parentheses like you did, it does put it in a new scope.
It also acts like a return value, which is why it can be called as an IIFE, or Immediately Invoking Function Expression.
Another way to re-write it, which will make more sense is like so:
var myFunc = (function test(){
alert('Hello!');
});
myFunc(); // Works!
test(); // Doesn't work!
To learn more about this, you should read about IIFE - Immediately-invoked function expression.
The short version - the function runs in its own scope, unless you pass parameters to it, like so:
(function test(x) {
alert(x); //alerts "yey!";
})("yey!");

(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.

Categories