[whatwg] Canvas 2d methods

Ian Hickson wrote:

>On Sun, 2 Jul 2006, Stefan G?ssner wrote:
>  
>
>>Ian Hickson wrote:
>>    
>>
>>>On Sat, 1 Jul 2006, Andrew Fedoniouk wrote:
>>> 
>>>      
>>>
>>>>In prototype based languages it is almost impossible to implement 
>>>>'with' effectively in the given notation.
>>>>
>>>>ctx.moveTo(0,0).lineTo(10,10);
>>>>
>>>>is more effective. In some circumstances - in times.
>>>>        
>>>>
>>>Why is it more effective for JS?
>>>      
>>>
>>see 
>>http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/
>>    
>>
>
>I'm familiar with this, but it doesn't seem to be relevant for this case 
>since all the method names are predefined and well-known.
>
>  
>
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.

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();

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.

Received on Sunday, 2 July 2006 07:01:46 UTC