how to access to a function inside a funtion - javascript

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

Related

Javascript function inside a function access

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

Calling Function only once

Could anyone help me with this troubleshooting?
var something = (function() {
var executed = false;
return function() {
if (!executed) {
executed = true;
alert("Hi");
}
};
})(); //Removing the following calls the function does not execute with parenthesis
something(); //Happens
something(); //Nothing happens
My Approach:
var only = (function once() {
alert("Kaixo");
only = false;
})(); //That parenthesis calls the function
only(); //Calls the function
only(); //Nothing happens
If my example is run before the original, breaks.
Continuing of this topic: Function in javascript that can be called only once
You are setting the only to the value returnted by function i.e undefined.Even if you don't call it directly it will not work because false can't be called.
You can set the function to another dummy function which have nothing.
function only(){
alert("Kaixo");
only = function(){}
}
only(); //Calls the function
only();
Here only is a function, not a boolean. So, instead of overriding it to false, override it to an empty function. See:
var only = (function once() {
alert("Kaixo");
only = function () {};
}); // Don't call the function!
only(); // Calls the function
only(); // Nothing happens
var only = (function once() {
var executed = false; // Inits the flag;
return function() {
if (!executed) {
executed = true; //Set the flag
alert("Kaixo");
}
}
})(); //That parenthesis calls the function
only(); //Calls the function
only(); //Nothing happens

self-executing anonymous functions and call it again

I want to do something like this:
var build= (function(){
//my function body
})();
function test(){
//somthing then call build
build() //i want to call build function again in my code
}
How can I do this?
I tried this in angular:
var buildRoot = (() => {
$SubNode.get({
TypeID: vendorAdminService.NodeType.Category
}, function(data: vendorAdminService.IGetNodeParameters) {
$scope.ProductTree = data.TreeNodeModelItem;
$scope.AjaxLoading = false;
}, function(err) {
// alert(err)
})
})();
$mdDialog.show(confirm).then(function() {
$Category.Remove(node.ID)
buildRoot
}, function() {
});
but it does not work.
Anybody can guide me??
You need to return a function in your IIFE.
If you IIF is not trivial and has many functionalities you could also consider using Reveal Module Pattern.
var build = (function() {
var f = function() {
console.log('hello');
};
f();
return f;
})();
function test() {
build();
}
test();
Just use a named function.
Your IIFE needs to return a function, for later calling. But then is no need for an anonymous function.
function build() {
//my function body
}
or
var build = function () {
//my function body
};
var build = (function() {
var init = function() {
// magic code
};
return {
init: init
}
}());
function test() {
build.init()
}
test();
You include all your functionalities inside your build object, and you'll be able to call them as soon as you return them from inside that object. This effectively is called the revealing module pattern
For more information, read this
I see that there are missing semi-colons ";"
$mdDialog.show(confirm).then(function() {
$Category.Remove(node.ID);
buildRoot();
}, function() {
});

How to use a return value in another function in Javascript?

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

JavaScript - Return from anonymous function (varScope)

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

Categories