ES4 draft: Error classes

Lars Hansen lhansen at adobe.com
Fri Mar 21 08:03:30 PDT 2008


> -----Original Message-----
> From: Garrett Smith [mailto:dhtmlkitchen at gmail.com] 
> Sent: 20. mars 2008 21:40
> To: Lars Hansen
> Cc: es4-discuss Discuss
> Subject: Re: ES4 draft: Error classes
> 
> On Tue, Mar 18, 2008 at 12:05 PM, Lars Hansen 
> <lhansen at adobe.com> wrote:
> > > -----Original Message-----
> >  > From: Garrett Smith [mailto:dhtmlkitchen at gmail.com]  > Sent: 18. 
> > mars 2008 11:57  > To: Lars Hansen  > Cc: es4-discuss Discuss  > 
> > Subject: Re: ES4 draft: Error classes  >
> >
> >
> > > On Mon, Mar 17, 2008 at 12:11 PM, Lars Hansen
> >  > <lhansen at adobe.com> wrote:
> >  > > (Didn't know who to follow up to so lamely following up to 
> > myself.)
> 
> >  > What does this have to do with getting a stack trace?
> >
> I see.
> 
> var color = el.style.color
> 
> where el is null, a ReferenceError would be thrown. It would 
> be useful to have a backwards-compatible way to get that 
> stack trace.

How is the mechanism not backward compatible?  Either context and
backTrace
are available on the error object, or they are not, but this particular
aspect does not depend on whether 'assert' is available or not.

> How can - assert - be detected?

Since 'assert' is a new keyword it would only have special meaning in
scripts that are loaded with "application/ecmascript;version=4",
so detection would in that sense be implicit.

Since we're just hacking among friends then the following works in 
both ES3 and ES4, I believe, it makes sure that 'assert' is defined
globally and throws an error if the condition is false, and it
uses the ES4 assert if available:

  if (window.__ECMASCRIPT_VERSION__ >= 4)
    window.assert = global.eval("function (x) { assert(x) }", 4);
  else
    window.assert = function (x) { if (!x) throw new Error("ASSERTION
FAILED") }

ES3 code and ES4 code would both just do eg

  assert(limit >= 0);

Clearly the optional second parameter can't be accomodated with the same
semantics as in ES4.

> What about libraries that already have an assert() function?

They would presumably be loaded in the ES3 syntax compatibility
mode and there would not be a conflict.

> "assert"in window?

Ditto (counterexamples or more elaborate questions welcome).

--lars



More information about the Es4-discuss mailing list