Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prove that my JavaScript files are in the scope of a specific JS or ECMA version?

Let's say you would get a bunch of .js files and now it is your job to sort them into groups like:

  • requires at least JavaScript 1.85
  • requires at least E4X (ECMAScript 4 EX)
  • requires at least ECMAScript 5

or something like this.

I am interested in any solution, but especially in those which work using JavaScript or PHP. This is used for creation of automated specifications, but it shouldn't matter - this is a nice task which should be easy to solve - however, I have no idea how and it is not easy for me. So, if this is easy to you, please share any hints.

I would expect something like this - http://kangax.github.com/es5-compat-table/# - just not for browsers, rather for a given file to be checked against different implementations of JavaScript.

My guess is, that each version must have some specifics, which can be tested for. However, all I can find is stuff about "what version does this browser support".


PS: Don't take "now it is your job" literally, I used it to demonstrate the task, not to imply that I expect work done for me; while in the progress of solving this, it would be just nice to have some help or direction.


EDIT: I took the easy way out, by recquiring ECMAScript 5 to be supported at least as good as by the current FireFox for my projekt to work as intendet and expected.

However, I am still intereseted in any solution-attemps or at least an definite answer of "is possible(, with XY)" or "is not possible, because ..."; XY can be just some Keyword, like FrameworkXY or DesignPatternXY or whatever or a more detailed solution of course.

like image 225
Jook Avatar asked Mar 11 '13 11:03

Jook


People also ask

How do I know if a JavaScript file is loaded or not in my browser?

Pass the URL of JavaScript file in a <script> tag. Set the onload parameter, Trigger alert if script loaded. If not then check for loaded variable, if it is equal to false, then script not loaded.

How do I know which version of JavaScript I'm using?

Visit the System information tool to see what version of JavaScript is detected. JavaScript is browser dependent, which means the version of JavaScript detected may be different in Firefox than the version detected by Internet Explorer.

Is JavaScript and ECMAScript same?

JavaScript is a general-purpose scripting language that conforms to the ECMAScript specification. The ECMAScript specification is a blueprint for creating a scripting language. JavaScript is an implementation of that blueprint. On the whole, JavaScript implements the ECMAScript specification as described in ECMA-262.

How do I include an external js file in another JavaScript file?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file.


1 Answers

Essentially you are looking to find the minimum requirements for some javascript file. I'd say that isn't possible until run time. JavaScript is a dynamic language. As such you don't have compile time errors. As a result, you can't tell until you are within some closure that something doesn't work, and even then it would be misleading. Your dependencies could in fact fix many compatibility issues.

Example:

  • JS File A uses some ES5 feature
  • JS File B provides a shim for ES5 deficient browsers or at least mimics it in some way.
  • JS File A and B are always loaded together, but independently A looks like it won't work.

Example2:

  • Object.create is what you want to test
  • Some guy named Crockford adds create to Object.prototype
  • Object.create now works in less compatible browsers, and nothing is broken.

Solution 1:

  1. Build or find a dependency map. You definitely already have a dependency map, either explicitly or you could generate it by iterating over you HTML files.
  2. Run all relevant code paths in environments with decreasing functionality (eg: ES5, then E4X, then JS 1.x, and so forth).
  3. Once a bundle of JS files fail for some code path you know their minimum requirement.
  4. Perhaps you could iterate over the public functions in your objects and use dependency injection to fill in constructors and methods. This sounds really hard though.

Solution 2:

  1. Use webdriver to visit your pages in various environments.
  2. Map window.onerror to a function that tells you if your current page broke while performing some actions.
  3. On error you will know that there is a problem with the bundle on the current page so save that data.

Both these solutions assume that you always write perfect JS that never has errors, which is something you should strive for but isn't realistic. This might; however, provide you with some basic "smoke testing" though.

like image 186
Parris Avatar answered Oct 13 '22 07:10

Parris