Jim said:
And yet certain features are difficult to detect...
For example what code would detect the "typeof" operator
(only available in 1.1 and above)?
How about the delete operator - which only works
correctly in 1.2 and above? The identity operator
was only introduced in 1.3 and above and so forth.
It is certainly true that language features that have been introduced
across versions are not necessarily easy to detect, especially compared
to browser features. On the other hand it is worth considering the
context in which we are working (i.e. August 2005). Netscape 4.06
introduced JavaScript 1.3, in 1998 as I recall, and as an implementation
of the second edition of ECMA 262. At this point the chances of
encountering a browser that does not support the features of JavaScript
1.3/JScript 3/ECMA 262 2nd edition or better are extremely slim, as such
ancient browsers are not capable of coping with the Internet as it now
is, and so are of little use to anyone.
Even tow-three years ago nobody was particularly worried about using the
typeof operator, though they would not use try-catch on the Internet.
These days we are getting to the point where even relatively recent
additions like try-catch are starting to seem practical, especially as
ECMA 262 3rd edition has existed since the end of 1999 and is the
standard to implement as the basis for a client-side scripting language.
That is, the current standard is now getting old enough to be relied
upon to provide a consistent basis for scripting across browsers.
Now we just have to hope that ECMA 262 4th edition never gets ratified,
and starts us off again on another round of language version
incompatibility.
I'm not saying that there are NOT ways to test for these,
however setting a baseline defining the "language" attribute
(which is supported by all major browsers and most, if not
all, minor ones) allows you to focus more on the code and
less on the checking.
That would be true if specifying versions actually worked. We have the
problem with IE, where JavaScript versions just don't map directly to
the capabilities of JScript versions. And then there are the browsers
that happily execute scripts specified as language="Javscritp1.6" (which
is a fictional version that may never exist), and so can be assumed to
execute any script with one single interpreter. These browsers (as
identified to date) are all ECMA 262 3rd edtion implementations so it
doesn't matter that they are not interested in the languae versions, but
if it is possible to find a pre-ECMA 262 3rd edition implementation that
disregards specified versions then specifying language versions would
already be a nonsense (and the fact that nobody has yet identified such
a browser is no guarantee that there is not one).
I think for many things you should still do some checking
- but but really, doing feature detection for language
_version_ is weak.
You would not be testing for the language version, in the same way as
you don't check browser versions. The point of feature testing is to
find out what you need to know in the context of the specific script, no
more and no less. The version doesn't matter much once you have
determined that.
And some features of the language are version specific
in how they work although they exist in all versions.
After 5 years of language standardisation there are not that many
problematic features lest. There are aspects of Regular Expressions that
are extensions beyond ECMA 262, and very difficult to test for, and
there are a few optional language features (such as function references
as the second argument to the String.replace function), but they are
much easier to test for.
We do have a standardised language with a core that is sufficient for
the vast majority of authoring tasks. The remaining issues are almost
entirely with the browser object models and not in the language.
Put simply you need to know (and respect) the rules before
you should ever consider breaking them.
Yes, that is the way to go.
Richard.