[whatwg] HTML 5 : Misconceptions Documented

On Wed, Aug 6, 2008 at 7:06 PM, Maciej Stachowiak <mjs at apple.com> wrote:
>
> On Aug 6, 2008, at 7:17 AM, Thomas Broyer wrote:
>
>> On Wed, Aug 6, 2008 at 11:29 AM, Maciej Stachowiak wrote:
>>>


>>> I think Web IDL should provide a formalism to cater to this, because
>>> nearly
>>> all bindings with special dynamic properties work like this afaik. But I
>>> think it would have to involve a pseudo-method for the "hasOwnProperty"
>>> check (which "in" is based on).
>>
>> hasOwnProperty is ECMAScript-specific.
>

Yes it is.

> The Web IDL includes specific requirements for how to map an interface
> specified in Web IDL to ECMAScript. So, indeed, that would be the part of
> the Web IDL spec that would be the best place to define this.
>

Since Maciej is probably considered to be an EcmaScript expert and I
am not, and since he had stated that 'in' calls 'hasOwnProperty', and
even continued to argue the claim, it is necessary and appropriate to
demonstrate proof that the 'in' does not call 'hasOwnProperty'.

The following example shows that no implementation calls
hasOwnProperty with use of 'in' operator.

If an implementation were to do that, that would be a bug.

There should be an alert on the second button and no alert on the
first. After both buttons have been clicked once, there should be a
green PASS.

<!DOCTYPE HTML>
<html lang="en">
<head>
      <title>hasOwnProperty</title>
      <style type="text/css">
#fail { background: red; }
#pass { background: #0f0; }
.res { display: none; font-weight: 700; }
      </style>

</head>
<body>
<script type="text/javascript">
      var rec = {}, failed;
      var mo = {
        name : "Maciej",
        title : "unknown"
      };

      Object.prototype.hasOwnProperty = function(p) {
        if(p == "unknown") {
           document.getElementById('fail').style.display='inline-block';
           failed = true;
       }
        alert('calling hasOwnProperty: '+ p);
      };

   function testIn(){
       rec['name'] = true;
       checkPass();
       'name'in mo;
   }

   function testHasOwnProperty(p){
       rec['title'] = true;
       checkPass();
       mo.hasOwnProperty('name');
   }

   function checkPass(){
       if(rec['name'] && rec['title'] && !failed)
       document.getElementById('pass').style.display='inline-block';
   }
</script>
<button onclick="testIn()">testIn()</button>
<button onclick="testHasOwnProperty();"
>testHasOwnProperty;</button>

<div id="fail" class="res">FAIL</div>
<div id="pass" class="res">PASS</div>
</body>
</html>

=============================================

Result in IE8b1, FF3, Webkit, Op9, Saf2.04
* alert on second button,
* PASS

the 'in' operator does not call 'hasOwnProperty'.

'hasOwnProperty' is EcmaScript specific. Whether or not ordinally
indexed properties should be 'own' properties is not a place for IDL
to decide.

Garrett

> Regards,
> Maciej
>
>

Received on Thursday, 7 August 2008 13:51:58 UTC