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

Re: whether [Callback=FunctionOnly] should be the default

From: Jorge <jorge@jorgechamorro.com>
Date: Wed, 29 Jun 2011 00:53:56 +0200
Cc: Cameron McCormack <cam@mcc.id.au>, public-script-coord@w3.org
Message-Id: <B0A0CB7E-5C5B-4CF0-B7B3-4FEC93308832@jorgechamorro.com>
To: Ian Hickson <ian@hixie.ch>
On 29/06/2011, at 00:23, Ian Hickson wrote:
> On Mon, 27 Jun 2011, Cameron McCormack wrote:
>> 
>> 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.
> 
> I've always assumed that the latter was a side-effect of non-JS languages 
> needing something more complicated here than just a function reference: if 
> the old DOM specs had never targetted anything but JS, would we support 
> the non-function case? Consider setTimeout(), which only takes a function 
> (or a string, for legacy reaons).
> 
> 
>> 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);
> 
> (function () {
>   var highlighted = false;
>   e.addEventListener("click", function (event) {
>     highlighted = !highlighted;
>     event.target.style.color = highlighted ? "red" : "black";
>   }, false);
> })();



Or you can simply attach 'highlighted' to the listener:

e.addEventListener("click", function  (event) {
    .highlighted = !.highlighted;
    event.target.style.color = .highlighted ? "red" : "black";
  }, false);

because functions are objects too.
-- 
Jorge.
Received on Tuesday, 28 June 2011 22:54:37 UTC

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