with and scope chain augmentation

Garrett Smith dhtmlkitchen at gmail.com
Wed Jun 24 14:42:37 PDT 2009


On Wed, Jun 24, 2009 at 12:02 PM, Brendan Eich<brendan at mozilla.com> wrote:
> On Jun 24, 2009, at 11:38 AM, Mike Wilson wrote:
>
>> Jason Orendorff wrote:
>>>
>>> Eeuuurgh.  In that case, what David-Sarah said.
>>
>> What did he say?
>
> He said "don't do that", to paraphrase. Full quote:
>
> "The 'with' can be in non-strict code, which is perfectly
> adequate for implementing a backward-compatible misfeature
> (the peculiar scope chain of a DOM0 event handler)."
>

Isn't it funny how something that seems small can have such a large
effect? This misfeature doesn't seem to have a good role or use cases.
It seems a bit like the callable collections that MSIE started.

If HTML 5 author(s) only had the hindsight to see the problems with
their inventions. I don't expect much at this point.

> The nested DOM object scopes for event handlers was something I did to allow
> brevity in inline event handler code. On balance it was a mistake, but it
> was so popular that the VXML spec actually goes nuts and reifies nested XML
> tags as objects on the scope chain for its ES scripting model!
>

Chrome actually shows the source code for the onclick using "with".

Taking that document linked,
javascript:document.write("<xmp
onclick="foo()">",(Function.prototype.toString.call(document.getElementsByTagName("xmp")[0].onclick),"<\/xmp>");

results:

function onclick(evt) {
  with (this.ownerDocument ? this.ownerDocument : {}) {
    with (this.form ? this.form : {}) {
      with (this) {
        return (function(evt){
self.alert([window.tagName, document.tagName])
}).call(this, evt);
      }
    }
  }
}

> This makes for a confusing tower of scopes. You can't always see the
> bindings, since an XML or HTML doc can be large. With the DOM, pieces of the
> namespace can be bound implicitly or in obscure ways. And "unobtrusive
> JavaScript" favors no event handler code bodies at all.
>
> So yeah: "don't do that" (don't use strict mode for any with hacks that
> emulate bad old web APIs such as event handler scope).
>

Not using bad old web APIs is also an alternative.

Garrett


More information about the es-discuss mailing list