W3C home > Mailing lists > Public > public-script-coord@w3.org > April to June 2011

whether [Callback=FunctionOnly] should be the default

From: Cameron McCormack <cam@mcc.id.au>
Date: Mon, 27 Jun 2011 15:06:41 +1200
To: public-script-coord@w3.org
Message-ID: <20110627030641.GA25806@wok.mcc.id.au>
In Web IDL currently, if you write [Callback] on an interface and the
interface consists of a single operation, then script can implement that
interface either by using a Function object, or by providing an object
with a property whose name is the identifier of the operation.

The case people would be familiar with is from DOM Events.  Both

  e.addEventListener("load", function(evt) { … }, false);

and

  var handler = { handleEvent: function(evt) { … } };
  e.addEventListener("load", handler, false);

work.  If you write [Callback=FunctionOnly], then the second style above
doesn’t work; you can only use a Function object.

Although I thought [Callback=FunctionOnly] would only be used for cases
where compatibility required the different handling from the default,
some spec authors seem to have taken a liking to it.  The result is that
some specs forbid the use of the object style while others allow it.  I
think we need to come to an agreement on whether we want to consistent
forbid or consistent allow this.

I made a change recently to the requestAnimationFrame spec to make it
[Callback=FunctionOnly], but it seems I’ve misread the general mood.
(Either way, it was a bad change: requestAnimationFrame is not special
enough to require behaviour different from the default.)

So I’d like to have a discussion here on whether [Callback=FunctionOnly]
should indeed be the default.  I prefer to leave the spec as it is, and
to discourage specs from using it unless it is needed for compatibility
reasons.

Olli gave a good example of why you might want to allow objects-with-a-
property to be used as callback objects, which is so that you can
encapsulate some state in that object.

  var handler = {
    _highlighted: false,
    handleEvent: function(evt) {
      this._highlighted = !this._highlighted;
      evt.target.style.color = this._highlighted ? "red" : "black";
    }
  };
  e.addEventListener("click", handler, false);

(EventListener is an example of an interface that needs to be able to
accept an object with a handleEvent property regardless of the default
Web IDL chooses, since content uses it.)

Thanks,

Cameron

-- 
Cameron McCormack ≝ http://mcc.id.au/
Received on Monday, 27 June 2011 03:07:09 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 8 May 2013 19:30:03 UTC