W3C home > Mailing lists > Public > whatwg@whatwg.org > July 2006

[whatwg] Canvas 2d methods

From: Ian Hickson <ian@hixie.ch>
Date: Sun, 2 Jul 2006 19:56:58 +0000 (UTC)
Message-ID: <Pine.LNX.4.62.0607021939210.4826@dhalsim.dreamhost.com>
On Sun, 2 Jul 2006, Stefan G?ssner wrote:
>
> If I understand this correctly, in the following code
> 
> var a=1, f=function(x){}, X={b:2,c:3};
> with(X) {
>   a=4;
>   f(b);
>   c=5;
> }
> 
> *every* variable a,b,c,f in the with-block must be dynamically checked, if it
> *either* belongs to the global namespace *or/and* is a member of X. As all
> variables in the block are affected, this sounds inherently inefficient.

This is true whether or not you use "with". JavaScript is a dynamically 
bound language. All that "with" does is change the binding chain 
temporarily. It is not any more or less efficient.


> Despite of this fact, if every method of an object X
> 
> var X = {
>   a: function(){ return this;}
>   b: function(){ return this;}
>   c: function(){ return this;}
> };
> 
> returns that object, we can code
> 
> with (X) {
>  a(); b(); c();
> }
> 
> as well as more elegantly in my opinion
> 
> X.a().b().c();

The latter, IMHO, is semantically bogus. But that's just a matter of 
opinion, of course.


> So existing code -- previously using methods returning nothing -- 
> shouldn't be affected and I can see no other drawback when adding 
> 'return this' to every method. Modern javascript libraries (e.g. jQuery) 
> seem to follow the mindset:
> 
> if an object method has no explicit return value, then return the object 
> itself in order to allow call chains.

I understand what you are asking for, my concern is just that getting 
every implementation to change to this now is going to cause all kinds of 
bugs to be introduced. While it would have been an interesting feature in 
the API if we were building it from scratch, it doesn't seem to be a big 
enough improvement to be worth the pain at this point, especially 
considering that the API works well with "with".

If you can convince the implementors to change to support chaining, 
though, I will of course change the spec as well. Vlad, however, seemed 
reluctant, and he is one of the implementors.

-- 
Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'
Received on Sunday, 2 July 2006 12:56:58 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:58:47 UTC