How to retrieve all the functions name in a javascript file [duplicate] - javascript

This question already has answers here:
Return all of the functions that are defined in a Javascript file
(4 answers)
Closed 3 years ago.
I have a javascript file and in that there are some functions defined in it.
How can i get all the function name defined in that particular js file in an array of string
function getabc1() {
//block of code
}
function getabc2() {
//block of code
}
function getabc3() {
//block of code
}
function getabc4() {
//block of code
}
function getabc5() {
//block of code
}
function getabc6() {
//block of code
}
function getabc7() {
//block of code
}
I need all the 7 functions defined in this particular js file.
Only name of function
It will be great help if someone can help me on this.

You're just gonna inspect the code a little bit, answers in Stackoverflow aren't always meant to give you the exact thing you need. You should learn to derive from it. Looking at the link #AndroidNoobie said, all you had to do is push the key instead of the entire iterated value.
As stated on https://stackoverflow.com/a/11279959/7325182:
Declare it in a pseudo namespace
var MyNamespace = function(){
function getAllFunctions(){
var myfunctions = [];
for (var l in this){
if (this.hasOwnProperty(l) &&
this[l] instanceof Function &&
!/myfunctions/i.test(l)){
myfunctions.push(l);
}
}
return myfunctions;
}
function foo(){
//method body goes here
}
function bar(){
//method body goes here
}
function baz(){
//method body goes here
}
return { getAllFunctions: getAllFunctions
,foo: foo
,bar: bar
,baz: baz };
}();
//usage
var allfns = MyNamespace.getAllFunctions();
console.log(allfns)
//=> allfns is now an array of functions.
// You can run allfns[0]() for example

Related

What is this js syntax called [duplicate]

This question already has answers here:
What is 'Currying'?
(23 answers)
Closed 4 years ago.
I recently found JavaScript code like this:
someFunction()()
Function immediately invoked right after function without classic IIFE syntax.
What is that kind of syntax called? And how it works?
Its a function that returns another function.
Here is an example on how it works, this can help you to understand better.
function sum(x){
return function(y){
return x+y;
}
}
sum(3)(5); //8
For you code to work:
someFunction()();
You need to have code that looks like this:
function someFunction() {
console.log('someFunction()');
// do something before return
return function() {
console.log('someFunction()()');
// do something else here
}
}
someFunction()();
In this example you first call someFunction then you call the function that was returned by someFunction.
This can be good to do when you need to pass in a variable that will be used in the inner function but the inner function will be called in the future like this:
function someFunction(outerVal) {
console.log('Outer called.');
return function(innerVal) {
console.log('inner called.');
return outerVal * innerVal;
}
}
var inner = someFunction(12);
setTimeout(()=> {
console.log(inner(4));
}, 1000);
I use this often in Node.js for common middle-ware that needs a single value to be unique.
app.get('/dogs', myFn('woof'));
app.get('/cats', myFn('meow'));
app.get('/birds', myFn('tweet'));
function myFn(word) {
return function(req, res, next) {
res.write(word).end();
}
}
That is an over simplification, but can be vary powerful.

replace javascript function with a new function [duplicate]

This question already has answers here:
Can you alter a Javascript function after declaring it?
(12 answers)
Closed 6 years ago.
In our ordering system there is an embedded function I have no access to at all. Conveniently there is a spelling error in it so when a user clicks something a popup appears and has grammar issues in it.
Is there a way for me to replace that text or replace the function with a new function that has all the same code but correct spelling?
This is the function I need to edit.. note confirm says 'You selection' not 'Your selection'
I'd prefer to replace the whole thing because I may want to do some other edits but for now I'd like to fix that spelling error.
function showProof()
{
var blnSubmit = false;
var strHid='';
var strSave='';
if (aryTemplateChoices.length == 0)
{
blnSubmit = true;
}
else
{
for (var i=1; i<=aryTemplateChoices.length; i++)
{
strSave = aryTemplateChoices[i-1];
if (strSave=='')strSave='0';
if (document.getElementById('hidctrl'+ i))strHid = document.getElementById('hidctrl'+ i).value;
if (strHid=='')strHid='0';
if ((strSave != '0') && (strHid != strSave))
{
blnSubmit = true;
break;
}
}
}
if (blnSubmit)
{
if (confirm('Your selection has changed, do you want to save?'))
{
document.getElementById('subtype').value = 'proof';
document.getElementById('prevclick').value = '';
document.getElementById('frm1').submit();
}
}
canAddToCart();
//<!--WRITE-->
getQuantityPrice()
//<!--/WRITE-->
loadProof();
}
It doesn't really matter where the original function is and how you access it, as long as you just redefine the function (with the same name and corrected code) at a scope closer to where you want to use it.
Here's an example:
function foo(){
console.log("ORIGINAL foo says hello.");
}
function foo(){
console.log("NEW foo says hello.");
}
// The second declaration is in the same scope as the first, but it comes after the first
// so it overwrites that declaration and the second one is the one that is used.
foo();

How I can return a string from a javascript callback [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
How I can return a string from a javascript callback
I have two functions, main function is working on loaded.
and another function is used to calling web service.
I would like to know how can JS return the string value to main function.
thanks
function thisCallJSON(webServiceURL) {
var params = {};
params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
gadgets.io.makeRequest(webServiceURL, function(response)
{
if(response.data && response.text)
{
var jsondata = response.data;
for (var key in jsondata)
{
var value = jsondata[key];
//alert("Member Name : "+value["memNm"]);
}
}
else
{
//alert("Member Name : Not Found !");
}
}, params);
}; function main(){
var txt_string = "";
txt_string = thisCallJSON("http://192.100.1.59");
}
You can assign the value to the variable in the scope of the main function, but it won't happen before the main function is finished executing because of the event loop. Instead, you should put your code inside the callback, or better yet, look at how you would use javascript promises to accomplish this.

Run Function only once in Javascript [duplicate]

This question already has answers here:
Function in JavaScript that can be called only once
(32 answers)
Closed 6 years ago.
Execute function only one time in Javascript, no matter how many times it has been called.
I write the following code, but does not working.
var counter = 0;
if(n.data === YT.PlayerState.BUFFERING) {
setTimeout(function() {
if(counter===0) {
r.frontPlayer.seekTo(10);
counter++;
}}, 2000);
}
Try not to use timeouts, they invite misery and suffering. This is a simple example, I use jquery for attaching the events but the function is independent of jquery. The key thing is using the object, the anonymous function in this case, to track state.
<button id="testButton">
test
</button>
$("#testButton").click(function() {
if (null == this.ran) {
console.log("do something");
this.ran = true;
}
})
Take a look at underscore or lodash's _.once function:
var fn = _.once(function() {
console.log('this will only run once');
});
Or writing it yourself:
var fn = (function() {
var called = false;
var ret;
return function() {
if (called) return ret;
called = true;
// do stuff
// ..
ret = 'some return value';
return ret;
};
})();

JS/ Call function which define in other function [duplicate]

This question already has answers here:
Calling a function that's defined inside a function
(3 answers)
Closed 8 years ago.
I have the next in JS:
function doC() {
this.try = document.getElementById("try");
function tryC(){
//do something
}
}
Now, I want to call tryC function, when so I wrote the next:
<script type="text/javascript" src="myFile.js"></script>
<script type="text/javascript">tryC();</script>
But as I see, nothing happen. Ho I call tryC()?
You have defined C in the scope of doC. It is not accessible outside of doC.
If you want it to be accessible globally, then you have to explicitly assign it a global.
window.C = function () { /* etc */ };
Creating globals is usually a bad idea, more so when they aren't created at load time. There is probably a better way to achieve whatever problem you are trying to solve with this.
Your tryC is defined inside doC, it's not exposed (it's private), you can do:
function doC() {
this.try = document.getElementById("try");
return function(){
alert('Try C');
}
}
doC()(); // alerts
or
function doC() {
this.try = document.getElementById("try");
return {
tryC : function(){
alert('Try C');
}
}
}
doc().tryC(); //alerts
Or your way (globals all around)
function doC() {
this.try = document.getElementById("try");
this.tryC = function(){
alert('Try C');
}
}
doC(); // call first!
tryC(); // alerts

Categories