Parse javascript function name from js file using javascript [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Return all of the functions that are defined in a Javascript file
OK so i have a JS file in which i have a number of JavaScript functions defined.Is there any way to parse out the name of all function in that particular JS file using JavaScript.Any guidance or link to achieve this will be appreciated

If you are not generating the js file dynamically, the easiest and safest option is to keep a hard-coded list of function names.
All other parsing methods are risky because
String parsing of the code is not safe, since you will have to cater
too many cases
There are options to get all global functions. But they are browser
dependent. like looping through all window objects for objects with typeof window[x] === 'function'

If it's just for development purposes then use an IDE. The IDE will depend on your environment. For example, you might you IntelliJ if your server side code is Java or Visual Studio's if you are a .NET shop.
If you really need to use javascript to dynamically go through the list of functions I suggest rethinking why you need to do it. If it turns out usefull you could namespace your functions and then just iterate over the namespace functions. See this answer for how to namespace https://stackoverflow.com/a/5947280/695461. Then iterate over the "public" functions.
Again, if it's just for development ease of use, use an IDE. They have whole teams of people writing parsers and syntax highlighters and structure diagrams for you.

Related

How to launch shell command and generate JSON after Cplex OPL model runs?

I have an OPL model which solves an ILP.
Currently it writes the solution to a txt file.
I want to launch something on the completion of the model to nicely display the solution, so I need to run a shell command.
How can I launch an arbitrary shell command on completion of the OPL run?
Can I call a JavaScript file in the same project from the OPL run to process the data? (NOTE: I see I can do this with includeScript(...))
Could I launch an arbitrary shell command within a JavaScript file?
I want to format the results as JSON. However the usual "JSON" object is not available within the Javascript context. Can I create an object in Javascript then "stringify" it as JSON?
(NOTE on (4) - the documentation claims that the JavaScript implementation is compliant with ECMA-262:
https://www.ibm.com/support/knowledgecenter/SSSA5P_12.4.0/ilog.odms.ide.help/refjsopl/html/intro.html#1037020
However the ECMA-262 definition does include the JSON.Stringify function:
https://www.ecma-international.org/ecma-262/5.1/#sec-15.12
Why is it not then available in OPL?)
Just some personal experience, OPLScript is incomplete if you treat it as a JavaScript variant - very incomplete. Every time I try to use some old-style JavaScript code in OPLScript, I re-learn this lesson.
At the very basic level, many standard array and object methods are missing. For example, you can't even use the [] syntax to create a JavaScript array or access the property of an object. You can't list the keys of an object, etc. ...
It's probably better to use another language such as Java or Python for any complex data structure (without re-inventing all the common facilities).
This could be part of a new version if you re patient enough. But with 12.7.1 you could either use an external Java call from scripting or modify the example at CPLEX_Studio1271\opl\examples\opl_interfaces\java\oplrunsample in order to do what you need.
Yes through includes
Not in version 12.7.1
OPLscript is a subset of ECMA and not all functions are inside but you could manage with IloOplOutputFile See https://www.ibm.com/developerworks/community/forums/html/topic?id=a7738eaf-f053-4fa8-83dc-5886bc381244&ps=25

Is JavaScript compatible with strict Page Object Pattern?

I have built various Test Automation frameworks using the Page Object Pattern with Java (https://code.google.com/p/selenium/wiki/PageObjects).
Two of the big benefits I have found are:
1) You can see what methods are available when you have an instance of a page (e.g. typing homepage. will show me all the actions/methods you can call from the homepage)
2) Because navigation methods (e.g. goToHomepage()) return an instance of the subsequent page (e.g. homepage), you can navigate through your tests simply by writing the code and seeing where it takes you.
e.g.
WelcomePage welcomePage = loginPage.loginWithValidUser(validUser);
PaymentsPage paymentsPage = welcomePage.goToPaymentsPage();
These benefits work perfectly with Java since the type of object (or page in this case) is known by the IDE.
However, with JavaScript (dynamically typed language), the object type is not fixed at any point and is often ambiguous to the IDE. Therefore, I cannot see how you can realise these benefits on an automation suite built using JavaScript (e.g. by using Cucumber).
Can anyone show me how you would use JavaScript with the Page Object Pattern to gain these benefits?
From Gerrit0's comment above and investigating it further, it seems a great way to achieve this is to use TypeScript (which is a statically typed version of JavaScript):
https://en.wikipedia.org/wiki/TypeScript
I am not much about this patterns.but i will give some details maybe it helps to you.
http://www.guru99.com/page-object-model-pom-page-factory-in-selenium-ultimate-guide.html
http://www.assertselenium.com/automation-design-practices/page-object-pattern/
It seems a great way to achieve this is to use TypeScript (which is a statically typed version of JavaScript):
https://en.wikipedia.org/wiki/TypeScript
If you use Jetbrains products like IntelliJ IDEA it will do a code completion and ther proper navigation for you. In javascript world page object is a known pattern too. AngularJs offers it too in it's own e2e test framework (http://www.protractortest.org/#/page-objects). Personally I use IIFE for page objects and IntelliJ does the rest. If it doesn't fit to your needs you can still choose typescript and transpile it to javascript.

Can we instantiate Java Objects in Javascript?

I know we can do this via DWR library to work with Java objects in Javascript.
But I would like to know if we can actually instantiate Java Objects in Javascript, using plain Javascript objects?
I searched on the internet and found this link and this link which speaks about Packages object in Javascript. I even read that this object is a part of JS since JS 1.1, is that true?
But when I actually used var myClass = new Packages.myPackage.myClass(); , it says, Packages is not defined, obviously I am missing out something here.
For my use case I have to instantiate a Java Pojo in JS.
Any clue folks on how to achieve this?
This feature depends on the JavaScript engine (i.e. the interpreter which runs the JavaScript). I haven't tried to do this in a browser but it might be possible when the Java plugin is enabled (which you shouldn't do for security reasons, at least not unconditionally).
The special object Packages is a feature of the Rhino engine, for example, which is a JavaScript interpreter that runs in a Java VM. The Packages has overloaded the accessor methods so when you write Packages.com.pany.Foo, it will internally look up the class and return something that wired the Java and the JavaScript worlds in a useful way.
You can find a tutorial here: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scripting_Java
You can create a Java object and assign it to a variable in JavaScript with the help of the new
keyword. When you create an instance of a Java class, JavaScript automatically creates a JavaObject
object. For example, you can instantiate a Java String from JavaScript and assign it to a variable.
Then you can use the dot operator to access the object’s length()
var myString=new java.lang.String("Test String");
alert(myString.length()); //prints 11
You can follow this link for more information http://www.sitepoint.com/connect-java-to-javascript-with-liveconnect/

Javascript - What kind of javascript class that use in this code?

I have found a JavaScript gallery.
When I view the JavaScript code, I really don't understand how to write code in this style. What is the style called? Where can I find documents related to it?
Note: what i want to know is the way that define Class in this code
(function (){ ... })(window)
what is style ?
jQuery Mobile. http://jquerymobile.com/
The reason you can't read it is because they are compressing the code which changes around variable names and makes the code as small as possible. This helps the code load faster.
jQuery.com
Based on your question, it seems you're not familiar with jQuery. jQuery is a JavaScript framework written in JavaScript. It is most useful for it's selector/filter capabilities, being able to pull objects of objects using something like $('div'); (or jQuery('div');), which would pull all the divs from the page. Using dot notation, you can perform methods on those returned objects.
(function (){ ... })(window)...
is both an unnamed function definition and call, passing in the window object. It's sort of like function foo (){...} foo(window), only you don't store the definition in variable foo and you need to wrap the definition in parentheses for syntax recognition in order to call it with the trailing parentheses.
For more advanced JavaScript topics, google closures.
Developers typically minify their javascript before deployment. This makes it hard to understand. but typically you can go to their website if they are willing to share the code
Try to get a look at the github website of the app.

smart reversing of compressed javascript with obscured variable & function names?

I want to know if there exists a tool to help in reversing a compressed javascript that has obscure variable names. I am not looking for a pretty-printing beautifier, but for a tool that actually knows how to change & propagate variable name choices.
Let me be more specific :
- some of the functions belong to the 'public' API and i want to impose readable argument names in their prototypes
- there are intermediary variables for document, window and other browser idioms
I would like to give this knowledge to the tool and then let it create another javascript where the knowledge would have been correctly propagated.
thanks
Jerome Wagner
Sounds like maybe you need a javascript refactoring tool. Something that could refactor javascript, i.e take a javascript file and rename variables.
Here are some plugins for IDE's:
http://www.brics.dk/jsrefactor/index.html
http://www.jetbrains.com/editors/javascript_editor.jsp?ide=idea#JavaScript%5Frefactoring
If you are trying to do this programatically, then this may not be the best solution for you.

Categories