var a = function() {
function someSetup(){
var setup = 'done';
}
function actualWork() {
alert('Worky-worky');
}
someSetup();
return actualWork;
}();
why the above code doesn't alert Worky-worky?it shows undefined.thank you
Because you only return the function, not call it.
Perform a(); after this code execution, this will call the function that's returned by anonymous self-executing function, thus actualWork.
you are trying to return a function that has no return type (actualWork()).
It doesn't do the alert() because you didn't call the function properly.
actualWork()
return actualWork; is actually returning a variable, but because you didn't assign anything to that variable you're getting undefined back.
should do the trick.
Related
Here's the function code:
function TestFunction(number){
return function(e){
return `${number}`;
}
}
When I use it on google's devtools command line it returns:
function(e){
return `${number}`;
}
So it looks like the function returned is not created with the number I give to TestFunction, instead it takes the string just like it was written. I have tried to use concatenation instead of interpolation but still not working. What can I do?
There is indeed a closure around the second function, so it will have memory of what num is.
function a(num) {
return function b() {
return `${num}`;
}
}
const c = a(6);
console.log(c());
I'm trying to solve a problem from an online tutorial. Consider the following function:
var badFunction = function() {
return function() {
return "veryBad"
}
}
Here, badFunction returns a function that returns "veryBad". I want to store "veryBad" inside theBad. How can I call the returned inner function? This answer isn't acceptable:
var theBad = "veryBad!";
Neither is this:
var theBad = badFunction();
theBad();
Although both of those work. So how do I call the inner function?
Just call the return value:
var theBad = badFunction()();
This will first call the function badFunction, then after the function finishes, it will then call the returned function. The evaluation looks something like this:
badFunction()();
^^^^^^^^^^^^^
Step 1, calls badFunction
Then, once badFunction is called, it returns an anonymous function expression:
(function() { // <-- This is the return value of badFunction
return "veryBad"
})();
^^
Step 2, call the returned function expression for "veryBad"
Of course, you could store the returned function in an intermediate variable, then call that:
var veryBadFunc = badFunction();
var theBad = veryBadFunc();
This would store the returned function in veryBadFunc which you could call later and store in theBad. This is exactly what you've done in your last try. The catch is that you have to do something with the return value, such as store it in a variable for later use.
Can someone help me to better understand why the second alert box appears with 'undefined' in it? When I call the function without alert function I don't see it.
var n = 1; // global scope
function one() {
alert(n);
}
alert(one());
alert(one()) alerts the return value of the function one. That function does not return a value, so its return value is undefined.
If you want to change the value of the second alert, you need to use return <value> from within one.
var n = 1; // global scope
function one() {
alert(n);
return 42;
}
alert(one());
i have this code..which wrap in anonymous function
(function() {
console.log("writting dataaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
window.onMessage1 = function(messageEvent) {
console.log("writting dataaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
console.log(messageEvent.data["color"]);
return $("form#credit-info-form").append(messageEvent.data["color"]);
};
}).call(this);
Error is 'TypeError: Cannot read property 'data' of undefined'
but when i'm executing above code without anonymous function wrap then it working ..
same error was on function name too i solved that with make 'onMessage1' to 'window.onMessage1' global scope..
any solution how to do with anoymous function ?
Thanks
The messageEvent parameter is undefined, which means you're not passing an argument to window.onMessage1 when you call it.
// this is the parameter---------v
window.onMessage1 = function(messageEvent) {
console.log("writting data");
// you use it here--v
console.log(messageEvent.data["color"]);
//---------------------------and here--------v
return $("form#credit-info-form").append(messageEvent.data["color"]);
};
so when you invoke it, you need to pass an argument that has a data property.
window.onMessage1(my_object);
This will work
(function() {
console.log("writting dataaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
window.onMessage1 = (function(messageEvent) {
console.log("writting dataaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
console.log(messageEvent.data["color"]);
//return $("form#credit-info-form").append(messageEvent.data["color"]);
});
})();
Call the function
onMessage1({'data':{'color':'red'}});
But $("form#credit-info-form") seems that a form with id=credit-info-form and you are appending messageEvent.data["color"], is that what you are doing ? Seems wrong to me.
Also, why you are using the anonymous function ? Since you are making the function global using window.onMessage1 = function(){...}.
I am new to javascript and have a quick question. Say i have the following code:
function entryPoint()
{
callFunction(parameter);
}
function callFunction(parameter)
{
... //do something here
var anotherFunction = function () { isRun(true); };
}
My question is that when callFunction(parameter) is called, and the variable anotherFunction is declared, does isRun(true) actually execute during this instantiation? I am thinking it doesnt and the contents of the anotherFunction are only "stored" in the variable to be actually executed line by line when, somewhere down the line, the call anotherFunction() is made. Can anyone please clarify the function confusion?
It seems the confusion is this line of code
var anotherFunction = function () { isRun(true); };
This declares a variable of a function / lambda type. The lambda is declared it is not run. The code inside of it will not execute until you invoke it via the variable
anotherFunction(); // Now it runs
You almost described it perfectly.
anotherFunction just receives a reference to a newly created Function Object (yes, Functions are also Objects in this language) but it does not get executed.
You could execute it by calling
anotherFunction();
for instance.
You can write a simple test like so:
entryPoint();
function entryPoint()
{
alert("In entryPoint");
callFunction();
}
function callFunction()
{
alert("In callFunction");
var anotherFunction = function () { isRun(); };
}
function isRun()
{
alert("In isRun");
}
And, the answer is no, isRun() does not get called.