JavaScript for Automation (JXA): What's supported? and Workarounds

Continuing the discussion from How to Create JavaScript Multiline String Literals (Template Literals):

NOTE: I'm sure I'll get some terms wrong here, or at least not explicit enough, so I may come back and edit some of this, once my mistakes have been pointed out.

##The Issue:

One of the issues when working with JavaScript for Automation (JXA), is determining what features are available on which version of OS/X. And really, it's not which version of OS/X - it's which version of Safari and its supporting libraries (hereafter referred to collectively as "Safari") are installed.

##Example: Can I use "String.endsWith()"?

I think developer.mozilla.org is the place to go for this information. I would do a google search for:

javascript String.endsWith() mdn

("mdn" means Mozilla Developer Network), and I would end up at this page:

The first thing I do is go to the bottom of the page, to find this grid:

It says Safari supports String.endsWith() starting in version 9, which is generally OS/X 10.10 (add one to the Safari number).

So now we know that if we want to use String.endsWith(), it means if people are still on Yosemite, they can't use our script.

Or does it?

##Enter "Polyfill"

There's a really cool concept called "Polyfill". It's a way to add a prototype definition for a feature, in such a way as to add it only if it isn't already supported.

Back to the Mozilla page again. Somewhere on the page, you'll find something like this:

Just copy and paste that code at the top of you JXA script, and you can safely use String.endsWith() in your script, even on Yosemite.

##Conclusion

Only you can decide which features to use or not use. But in general, if you ever intend on sharing your script, you should always check MDN to see a particular feature is available or not.

Or, write it as you prefer, but share a version generated by pasting to and copying from Babel JS (compatible with any level of JXA).

Are you positive that what is compatible in JXA is tied to the Safari version installed, and not the macOS version?

I seem to remember some issues with JXA were fixed with a later update of the macOS, but I'm not sure if that was really just an update of Safari.

Irrespective of the Safari version, is it correct that JXA will not support ES6 in Yosemite?

If I have the lastest version of macOS 10.11 (El Capitan), which is 10.11.6, do we know that JXA will fully support ES6?

Are there any JavaScript features that require 10.12 (Sierra), or a specific update to 10.12? (for example, apparently 10.12.2 just fixed some issues for KM).

Thanks.

I think we know that it won't.

(As far as I know, only Sierra's JSC is fully updated to ES6 / SSF10. Before that it is SF9 JS, with a certain subset of the ES6 features but not all. Installing SF10 Safari on a pre-Sierra OS gives you ES6 in the browser, but not, I think, in Script Editor, osascript, or the Bash command line JSC).

(That, at least is what I seem to have found on a pre-Sierra system here – not sure if others have had differing experimental results)

Compare SF9 and SF10 in https://kangax.github.io/compat-table/es6/

If that be the case, then I'm definitely not going to use ES6 until it is fully supported by JXA. Too much trouble, for me and users of my scripts, to keep track of what is supported, and what is not. :frowning:

Just for the record, I was testing with OS 10.11.6, and Safari 10.0.2

On that platform, if I try to evaluate code like:

const id = x => x

(which works as a function definition in Sierra)

I just get an error in an El Capitan version of Script Editor.

@ComplexPoint, do you think that everything in ES6 will ever be fully supported in JXA? Aren't there always some things that not all vendors support?

Let's start with ES5. Is it fully supported by JXA?

Doesn’t look like it, from this chart:

https://kangax.github.io/compat-table/es5/

How can you tell?

I don't see any references to "JXA".
The only reference to Safari is indirect, a footnote.

What am I missing?

Also, since @ComplexPoint pointed out the ES6 chart was wrong, I don't have much faith in these GitHub tables.

Assuming that "SF 10" means Safari 10, looks like it is non-compliant in only two things:

Enumerable properties can be shadowed by non-enumerables
Array.prototype.sort: compareFn must be function or undefined

That site fails the basic requirement of all charts: Proper legend

Missing those two seems minor to me, since I don't even know what they mean. But then that is more about my JavaScript ignorance than anything else. :wink:

Well, I suppose that's a good question. I was assuming that what's supported in Safari is what's supported in JXA. I could be wrong.

In any case, I don't know if or when everything will be supported, but if I were you, I wouldn't hold my breath. That's really all I was trying to convey.

Point taken.

If I can find a source that is reliable reporting of compliance of JXA, then I'll be glad to step up to ES6 when JXA compliance is ≥ 95%.

LOL. Arbitrary, but valid. :wink:

Not exactly. All engineering is an approximation of pure science to solve a particular problem. So we often work with probabilities. I'm going to make an educated guess that the features that fall into the 5% non-compliance are not that important. IAC, I can more easily note them, and avoid their use.

So, can anyone give us a reliable % compliance of JXA (under macOS 10.11.6) with ES6?

And better yet, list the ES6 features that fail compliance?

I think SF10 and the Sierra JSC already have full ES6 compliance.

I'm more concerned about JXA. Does JXA under macOS 10.12.2 have full ES6 compliance?

I'm more concerned about JXA

Just a misunderstanding, I think – they're the same thing.

('JXA' is an instance of the system JSC with an Automation object added to its global context).

So yes, to reiterate, the Sierra JSC is fully E6S compatible, and that means you can make full use of ES6 in Script Editor and osascript (hence Atom, Keyboard Maestro etc) with the Automation object on Sierra.

The only things you need to keep apart in your mind are the system JSC and the JSContext embedded in Safari. It's possible for Safari to be running a later version of the JavaScript Core interpreter than the system is.

Thanks for clarifying. But I don't like to make assumptions -- like just because Safari is 100% compliant with ES6 then JXA is also.

So that leaves us with macOS 10.11.6 NOT being 100% compliant.
Any ideas which specific features are not compliant?

It’s the same as the SF9 vs SF10 differences in the compatibility table.