[I'm not lecturing Neil, just happened to reply to his message in  
this thread as an entree in to discussing how we check for the  
existence of a property in Javascript.]

The 'correct' way to ask if a property exists is the `in` operator

   if ('bar' in foo)

which unfortunately is not currently supported in our compiler  
(http://www.openlaszlo.org/jira/browse/LPP-969).  If you know that  
the property is an immediate property (not inherited from the  
superclass) then you can ask:

   if (foo.hasOwnProperty('bar'))

Because we do not support the `in` operator, the compiler currently  
does not warn if you ask for a non-existent property using the index  
operator:

   if (foo['bar'])

but as Adam has said, that is a bad idiom that we ought to purge.   
The whole point of the 'undefined' warnings is to help you debug your  
code.  Someday we will need to turn on warnings for that (and have a  
whole raft more warnings to clean up). (http://www.openlaszlo.org/ 
jira/browse/LPP-2030)

We also should distinguish between a non-existent property and a  
property whose value is `undefined`.  In earlier swf runtimes, this  
was not possible, but we could do that now. (http:// 
www.openlaszlo.org/jira/browse/LPP-2031)

The compiler will also not warn for a non-existent property if you  
take `typeof` an non-existent property, so this test:

   if (typeof foo.bar != 'undefined')

is another way to check.  The intent is that you would not just check  
that the property was defined, but check that it is of the correct  
type.    Since you are checking, any warning would be superfluous.

By extension, the compiler ought not warn if you ask `foo.bar  
instanceof <class>`, or as you suggest `foo.bar == void 0`[1].  But  
that just creates more idioms we will eventually need to fix.  I  
suggest we stick with the two idioms we have for now and elevate the  
priority of LPP-969.

---
[1] See (http://pt.withy.org/ptalk/archives/2005/06/ 
dont_assume_undefined_is_undefined.html) for why I say `void 0`  
rather than `undefined`.


On 2006-05-08, at 00:58 EDT, Neil Mix wrote:

> Is contextual checking possible here?  I do a lot of this:
>
> if( foo.bar == null ) ...
>
> which of course can generate "WARNING: reference to undefined  
> property 'bar'".  Well duh, that's why I'm checking.  On the other  
> hand, once or twice I've been save by warnings from:
>
> var x = foo.bar + baz;
>
> I suspect that if the warnings are optional, I'll just get in the  
> habit of using a lot of the first example.  In turn, this means  
> that when I decide to use the optional warnings to track down and  
> instance of the second example I'll drown in a flood of red  
> herrings.  This is where a little bit of contextual smarts would be  
> nice.  If that's even reasonably possible...
>
> On May 7, 2006, at 7:47 PM, P T Withington wrote:
>
>> Looks good, but what is 'hasAttribute'?  Do you mean hasOwnProperty?
>> (Or 'in', but we don't permit 'in').
>>
>> The idiom we use for testing the existence of a property without
>> getting a warning is this['prop'] -- the debugger does not warn on  
>> that.
>>
>> But perhaps we should be working on ERROR's first, not WARNING's.  It
>> is not an error in Javascript to reference a non-existent property,
>> we only have a warning to help people debug.  Maybe I should make
>> that warning optional.
>>
>> On 2006-05-07, at 17:56 EDT, Benjamin Shine wrote:
>>
>>>
>>> Tucker, I have risen to approximately 1% of your challenge. If
>>> these fixes are in the right vein, I'll keep going.
>>>
>>> Change 42100 by [EMAIL PROTECTED] on 2006/05/07 14:49:36
>>> *pending*
>>>
>>>     Summary: Eliminating a few warnings in LaszloCanvas.as  
>>> instantiation.
>>>     
>>>     New Features:
>>>     
>>>     Bugs Fixed:
>>>     
>>>     Technical Reviewer: ptw (pending)
>>>     QA Reviewer:  frisco (pending)
>>>     Doc Reviewer:  (pending)
>>>     
>>>     Documentation:
>>>     
>>>     Release Notes:
>>>     
>>>     Details:
>>>        This change eliminates the following runtime errors and
>>> warnings, when I run http://localhost:8087/lps-legals/test/lztest/
>>> lztest-simple.lzx?debug=true
>>>        07 May 2006 14:21:26 (127.0.0.1 2) INFO
>>> responders.ResponderEVAL – CLIENT_LOG ERROR: views/LaszloCanvas.as:
>>> 256: undefined object does not have a property ‘sendEvent’
>>>        07 May 2006 14:21:26 (127.0.0.1 1) INFO
>>> responders.ResponderEVAL – CLIENT_LOG WARNING: views/
>>> LaszloCanvas.as:256: reference to undefined property
>>> ‘onpercentcreated’
>>>        07 May 2006 14:21:26 (127.0.0.1 3) INFO
>>> responders.ResponderEVAL – CLIENT_LOG WARNING: views/
>>> LaszloCanvas.as:258: reference to undefined property ‘initdelay’
>>>        07 May 2006 14:21:26 (127.0.0.1 4) INFO
>>> responders.ResponderEVAL – CLIENT_LOG WARNING: views/
>>> LaszloCanvas.as:308: reference to undefined property ’LZlateinit’
>>>     
>>>     Tests:
>>>
>>> Affected files ...
>>>
>>> ... //depot/lps-legals/WEB-INF/lps/lfc/core/LzNode.as#7 edit
>>> ... //depot/lps-legals/WEB-INF/lps/lfc/views/LaszloCanvas.as#5 edit
>>> ... //depot/lps-legals/lps/utils/lztestmanager.lzx#2 edit
>>>
>>>
>>> <changeset-42100.zip>
>>>
>>> benjamin shine
>>> software engineer
>>> [EMAIL PROTECTED]
>>>
>>>
>>>
>>
>>
>> _______________________________________________
>> Laszlo-dev mailing list
>> [email protected]
>> http://www.openlaszlo.org/mailman/listinfo/laszlo-dev
>


_______________________________________________
Laszlo-dev mailing list
[email protected]
http://www.openlaszlo.org/mailman/listinfo/laszlo-dev

Reply via email to