<script>
var sample = function() {
(function() {
return "something"
})();
// how can I return it here again?
}
</script>
Is there a way to return the returned value from the anonymous function in the parent function again or do I need to use a defined function to get the returned value? Thanks! :)
Just put the return statement at the point where you call the function.
<script>
var sample = function() {
return (function() { // The function returns when you call it
return "something"
})();
}
</script>
Related
Here is my code i want alert to give (some data) value. which is of inner function it give undefined value
alert(f1());
function f1(){
f2(function (){
return "some data";
});
}
function f2(f4){
// some code
}
visit : https://jsfiddle.net/dvdhyttr/8/
i want alert get value (some data).
but getting undefined.
Two main issues
Missing return within the function f1
Within function f2 you need to return the result of calling the function f4.
alert(f1());
function f1() {
return f2(function() {
return "some data";
});
}
function f2(f4) {
return f4()
}
You need to assign the return values of the functions to variables in the outer function and return them, all the way up to alert(f1());.
alert(f1());
function f1() {
const resultOfF2 = f2(function() {
return "some data";
});
return resultOfF2;
}
function f2(f4) {
return f4();
// some code
}
I'm trying to call a function inside a function outside the main one:
var myFunction = function myFunct(element) {
function tryIt(){
alert("hello");
};
return{
tryIt: tryIt
}
}
And I'm try to call "tryIt" function outside myFunct with
myFunction.tryIt
But it doesnt work.
how can I do this?
First, you need is to call the function and then call the function of the returned object of the property tryIt.
var myFunction = function myFunct(element) {
function tryIt(){
alert("hello");
}
return {
tryIt: tryIt
};
};
myFunction().tryIt();
// ^^ calls myFunction
// ^^^^^^^^^ returns object with function tryIt
// ^^ calls tryIt
function myFunct() {
function tryIt() {
console.log("hi");
}
return {
tryIt: tryIt
};
}
var foo = myFunct();
foo.tryIt();
I am trying to create a function that can then return many functions based on an input. Here is an example of the problem I am facing.
var giveFunction = function(text) {
return function(text) {
console.log(text)
}
}
var test = giveFunction('this is a test');
test()
Running test() at the end prints undefined instead of 'this is a test.' Is there any way around this problem?
The inner function should not contain any parameter,
var giveFunction = function(text) {
return function() {
console.log(text)
}
}
Let it create a closure. If it has a parameter then that would be read during execution and undefined would be printed as you are not calling that function with any arguments.
If you want your code to be working then you have to use bind for that,
var giveFunction = function(text) {
return function(text) {
console.log(text)
}.bind(null, text)
}
var test = giveFunction('this is a test');
test(); //'this is a test'
Lets go one step further and ask why?
var outerFunction = function(outerParameter) {
return innerFunction function(innerParameter) {
// in here we have access to anything in this function and the outer function
// all the way to out the the global scope
// therefore, we dont need to pass innerParameter in again as a parameter! ( in your case we 'next')
}
/// now over here, outside of innerFunction, we do NOT have access to innerParameter!
}
So applying the above principles to your code we have:
var giveFunction = function(text) {
return function() {
console.log(text)
}
}
var test = giveFunction('this is a test');
test()
which now works!
Finally, checkout the most upvoted post under the javascript tag:
How do JavaScript closures work?
Hello everyone hope you are well.
I am trying to call a function in my jQuery file and for some reason it doesn't get called if I call it from an if statement. However the same function gets called for when I call it using .click().
The if statement works fine.
Here is my jQuery code.
$(document).ready(function () {
var hello = 1;
if (hello == 1) {
console.log("True");
helloWorld();
} else {
console.log("False");
}
$('#en').click(helloWorld(pathname));
function helloWorld() {
return function () {
console.log("function called");
}
});
});
You're using
var hello==1;
instead of
var hello=1;
When assigning hello variable, you test it if it is equal to 1, not assigning 1 to variable. Just try with:
var hello = 1;
Your function return another function, if you want to call it, this is the correct way:
helloWorld()();
Or
var myFunc = helloWorld();
myFunc();
Your declaration in variable is wrong.
var hello=1;
I'm self-teaching myself JavaScript and out of curiosity I'm wondering what is the proper way of returning a value from one function to be used in another function. For example:
function firstFunction() {
// do something;
return somevalue
}
So how do I set up the second function to use somevalue? Thanks.
Call the function and save the return value of that very call.
function firstFunction() {
// do something
return "testing 123";
}
var test = firstFunction(); // this will grab you the return value from firstFunction();
alert(test);
You can make this call from another function too, as long as both functions have same scope.
For example:
function testCase() {
var test = firstFunction();
alert(test);
}
Demo
You could call firstFunction from secondFunction :
function secondFunction() {
alert(firstFunction());
}
Or use a global variable to host the result of firstFunction :
var v = firstFunction();
function secondFunction() { alert(v); }
Or pass the result of firstFunction as a parameter to secondFunction :
function secondFunction(v) { alert(v); }
secondFunction(firstFunction());
Or pass firstFunction as a parameter to secondFunction :
function secondFunction(fn) { alert(fn()); }
secondFunction(firstFunction);
Here is a demo : http://jsfiddle.net/wared/RK6X7/.
Call function within other function :
function abc(){
var a = firstFunction();
}
function firstFunction() {
Do something;
return somevalue
}
You can do this for sure. Have a look below
function fnOne(){
// do something
return value;
}
function fnTwo(){
var strVal= fnOne();
//use strValhere
alert(strVal);
}
var captcha = '';
//function name one
function one(captcha){
var captcha = captcha;
//call another function and pass variable data
var captcha = firstFunction(captcha);
};
// second function name
function firstFunction(captcha){
alert(captcha);
}
To copy the return value of any javascript(in chrome console), we can use inbuilt copy() method.
you can use any expression, function, etc
find some examples below
using expresseion
a = 245;
copy(a);
using function
a = function() {
return "Hello world!"
}
copy(a());
Official Doc for reference