Solved

listing all functions in JavaScript

Posted on 2016-10-28
  • Programming
  • Java App Servers
  • JavaScript
  • Java EE
  • JSP
  • +1
19
98 Views
Last Modified: 2016-11-19
We are using JavaScript environment inside of another product.

I would like some code that would list all of the functions that are available ?

Its simple to interrogate an object for all of its methods and properties.
But how do we list all the possible top level objects/Methods/Properties that java and/or the vendor may have exposed ?

Thanks
0
Comment
Question by:sidwelle
  • 8
  • 7
  • 3
  • +1
19 Comments
 
LVL 7

Assisted Solution

by:James Bilous
James Bilous earned 125 total points
Comment Utility
You can't do this with javascript itself, just look up a list of the Javascript API such as this one and hardcode it.
0
 

Author Comment

by:sidwelle
Comment Utility
Thank you, but what I am looking for is a technique to display objects that the vendor has exposed that would not be documented in an open format like the one you suggested.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
console.dir(variable)

run that in the dev tools of the browser and you'll see everything to do with the object
0
 

Author Comment

by:sidwelle
Comment Utility
Thank you for that, but this is not a browser environment.  This is an implementation of RHINO w/in another product.  All I get is the opportunity to write scripts in a window.

When the script executes a statement and the interpreter looks up that command either in a object list of some type,  how to I iterate through that list and show all the top level objects ?

Thank you.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
okay, the console aside, to loop through the top level objects, methods etc you can use a simple for loop.  I've used "console.log()" here so you would need to replace that with however you output in RHINO:

for (d in document) {
    console.log(d+": "+typeof(document[d]));
}

outputs:
captureEvents: function
releaseEvents: function
implementation: object
URL: string
documentURI: string

Open in new window

0
 

Author Comment

by:sidwelle
Comment Utility
That doesn't help because I don't have a document.  I am not evaluating a page.

I assume what the product does is take what little javaScript that I write and run it in an eval statement.

Thanks
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
The use of "document" was for this example, substitute it for the object you want to enumerate
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
In other words:

for (iterator in myobject) {
....
\\iterator will hold each property
}
0
 

Author Comment

by:sidwelle
Comment Utility
I am not looking to iterate a specific object,  I want a list of all top level objects so that I can iterate through each one.

If there is a cmd in the environment names "doSomething()", how do I learn what the parent object is or other cmds that I don't have documentation to  ?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
http://mozilla.github.io/rhino/javadoc/index.html outlines the available objects that you can access from JavaScript.

As for the vendor object, if there's no documentation then the only thing you can do is iterate over the vendor object and filter for function types
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
This is also a job for the debugger: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Debugger

It will allow you to graphically display the variables, objects and available functions
0
 

Author Comment

by:sidwelle
Comment Utility
Still looking for some cmds that will expose the vendor types ?

If I know that, iteration would be great !
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
Did you try the debugger?
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
The following work for jQuery which store function in the "fn" member object :
Test page : https://jsfiddle.net/0zour8fe/
for(var v in jQuery.fn) 
    if(typeof jQuery.fn[v]==="function") console.log(v);

Open in new window


or again :
for(var v in jQuery.fn) 
    if(typeof jQuery.fn[v]==="function") console.log(v + ": " + jQuery.fn[v]);

Open in new window

0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 250 total points
Comment Utility
@leakim - not sure there's a console to use here... RHINO is a javascript engine within a Java program

@sidwelle - did you use the RHINO Javascript debugger I posted previously?  I assume that showed you everything you need?
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 125 total points
Comment Utility
using RHINO, for debugging purpose, you need to replace :
"console.log" by "print"
in my previous code
0
 

Author Comment

by:sidwelle
Comment Utility
I am inside of another product, and that product supplies a text box for users to type small script-lets and then test them by hitting a test button.  No console or popups, smart syntax.  I do understand that the product is built on top of the Rhino engine, but thats all, and no other documentation is supplied.
By hunting around I have found that a nuber of objects do exist and are accessable and usable. Here are a few:
java.lang.string
java.io.File
java.io.FileReader
java.lang.system
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
look like java object... not javascript...
0
 

Author Closing Comment

by:sidwelle
Comment Utility
Its Java that I am calling form JavaScript.

Thank you for your posts, but I don't have a JScript object either.

I have had some success just working through the Rhino Java manual and testing to see what objects are available.

I posted another question about storing values in Java, maybe you could take a look at that question ?
(what is a "java.lang.System Property")

I am going to request this question be closed and give you guys the points.

Thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now