how can I access a function inside a function? Here's an example:
function fun1() {
//some code here
var example = function (){
//some code here
}
return {
example: example
}
}
when i try to access it like this fun1.example() I get error fun1.example is not a function. What am I doing wrong?
Please look at comments for more info
function fun1() {
//some code here
var example = function () {
//some code here
};
return {
example: example,
};
}
// Fun1 is funtion, In funtion there is no property like example
// So when you call fun1.example , which undefined. Is not a function
fun1.example() // Error here
const obj = fun1() // call funtion to get obj
obj.example() // No error
You have to run the code as fun().example(), or store the result of fun() into a variable and then run it, like this:
var fun = ();
fun.example();
Related
I'm trying to create a function in a class that call another function. The code is
createElAdditive(){
...
this.cvs.addEventListener("mousedown", function () {
this.saveToHist();
...
});
}
saveToHist() {
this.historyNumber++;
if (this.historyNumber < this.saveHistory.length) {
this.saveHistory.length = this.historyNumber;
}
this.saveHistory.push(this.cvs.toDataURL());
}
The two function is on the same class. How can I access other function in same class? In my case I want to call saveToHist function inside cvs event in createElAdditive function.
The complete code is on codepen here and you can try draw something on canvas to see error in console.
The this inside the addEventListern() block refers to something other than the instance of the object. You need to capture it from outside, then use it
createElAdditive(){
...
var that = this; // captured here
this.cvs.addEventListener("mousedown", function () {
that.saveToHist(); // used here
...
});
}
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();
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
<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>