<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>19967</bug_id>
          
          <creation_ts>2008-07-09 15:01:49 -0700</creation_ts>
          <short_desc>Variables named &quot;name&quot; become strings</short_desc>
          <delta_ts>2022-06-03 05:46:40 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebCore JavaScript</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>Mac (Intel)</rep_platform>
          <op_sys>OS X 10.5</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>CONFIGURATION CHANGED</resolution>
          
          
          <bug_file_loc>http://www.tip.it/runescape/index.php?page=smith_smelt_calc.htm</bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P4</priority>
          <bug_severity>Minor</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Tamal White">tamal</reporter>
          <assigned_to name="Cameron Zwarich (cpst)">zwarich</assigned_to>
          <cc>ahmad.saleem792</cc>
    
    <cc>annevk</cc>
    
    <cc>ap</cc>
    
    <cc>cdumez</cc>
    
    <cc>joepeck</cc>
    
    <cc>jond</cc>
    
    <cc>zwarich</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>85573</commentid>
    <comment_count>0</comment_count>
    <who name="Tamal White">tamal</who>
    <bug_when>2008-07-09 15:01:49 -0700</bug_when>
    <thetext>Run this bit of code in Safari 3.1 or Webkit nightly:

javascript:name=names=new Array(&quot;foo&quot;,&quot;bar&quot;,&quot;bang&quot;);alert(typeof(name)+&quot;/&quot;+typeof(names));

or

javascript:name=names=function() { return &quot;foo&quot; };alert(typeof(name)+&quot;/&quot;+typeof(names));

It appears that if the variable name is &quot;name&quot;, it is automatically typed as a string. In the URL provided, this is causing a problem when using name.length with a for loop. The loop is iterating over every character in the string instead of elements in an array.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>85581</commentid>
    <comment_count>1</comment_count>
    <who name="Cameron Zwarich (cpst)">zwarich</who>
    <bug_when>2008-07-09 16:31:06 -0700</bug_when>
    <thetext>It seems that either the getter or the setter for the window.name property is converting the value to a string. This is an interesting bug, but it shouldn&apos;t be too hard to fix.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>222445</commentid>
    <comment_count>2</comment_count>
    <who name="Joseph Pecoraro">joepeck</who>
    <bug_when>2010-05-07 09:57:21 -0700</bug_when>
    <thetext>I think this is expected behavior. window.name is an actual property to set the (string) name of the browsing context:
http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#apis-for-creating-and-navigating-browsing-contexts-by-name</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>222804</commentid>
    <comment_count>3</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2010-05-07 22:39:33 -0700</bug_when>
    <thetext>Compare to:

javascript:window.name=new Array(&quot;foo&quot;,&quot;bar&quot;,&quot;bang&quot;);alert(typeof(window.name));

In Firefox, this says &quot;string&quot;, but the original test case says &quot;object&quot;. So, this is more about what &quot;name&quot; refers to in different contexts, not about window.name behavior.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>222822</commentid>
    <comment_count>4</comment_count>
    <who name="Joseph Pecoraro">joepeck</who>
    <bug_when>2010-05-07 23:19:37 -0700</bug_when>
    <thetext>(In reply to comment #3)
&gt; So, this is more about what &quot;name&quot; refers to in different contexts,
&gt; not about window.name behavior.

I disagree. The three cases seen here all show window.name is type string, and that the same value in a different variable shows a different type. I believe this was the original way that the author was trying to show that typeof window.name is always string.

&gt; javascript:name=names=new Array(&quot;foo&quot;,&quot;bar&quot;,&quot;bang&quot;);alert(typeof(name)+&quot;/&quot;+typeof(names));

  typeof name =&gt; &quot;string&quot;
  typeof names =&gt; &quot;object&quot;

&gt; javascript:name=names=function() { return &quot;foo&quot; };alert(typeof(name)+&quot;/&quot;+typeof(names));

  typeof name =&gt; &quot;string&quot;
  typeof names =&gt; &quot;function&quot;

Your example is just a simplification of the first.


&gt; In Firefox, this says &quot;string&quot;, but the original test case says &quot;object&quot;.

It was `names`, not `name` that was object =)


I think you are on the right track about contexts, but that just leads me back to the `window.name` special case. Any time you use the variable with name `name` in the global scope it will be `window.name` and have the special properties. However, if you make a new scope (which I believe can only be down with functions in JavaScript) you get expected results:

&gt; javascript:(function() { var name = 1;  alert(typeof name); })()

  typeof name =&gt; &quot;number&quot;

The special case of `window.name` can be recreated in JavaScript using a getter on the global variable. Lets do the same thing with a property named `goose`. A much jollier and less confusing variable name ;)

  (function() {
    var private = &quot;&quot;; // Keep the real data private
    window.__defineGetter__(&apos;goose&apos;, function() {
      return private;
    });
    window.__defineSetter__(&apos;goose&apos;, function(x) {
      private = String(x);
      return x; // Preserve chain behavior
    });
  })()

Now usage appears the same as `window.name`

  var goose = [1,2,3];
  typeof goose;         // &quot;string&quot;
  goose;                // &quot;1,2,3&quot;
  
  var name = [1,2,3];
  typeof name;          // &quot;string&quot;
  name;                 // &quot;1,2,3&quot;

Its worth noting that using var multiple times is not a syntax error, and does nothing if the variable is already in initialized.

  var x = 10;
  var x = 5; // harmless, but frowned upon</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>222828</commentid>
    <comment_count>5</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2010-05-07 23:36:54 -0700</bug_when>
    <thetext>&gt;&gt; In Firefox, this says &quot;string&quot;, but the original test case says &quot;object&quot;.
&gt; It was `names`, not `name` that was object =)

In fact, both are objects. For the first case in bug description, Firefox 3.5 says object/object.

Are you running each test in a new window or tab?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>222845</commentid>
    <comment_count>6</comment_count>
    <who name="Joseph Pecoraro">joepeck</who>
    <bug_when>2010-05-08 00:30:43 -0700</bug_when>
    <thetext>(In reply to comment #5)
&gt; &gt;&gt; In Firefox, this says &quot;string&quot;, but the original test case says &quot;object&quot;.
&gt; &gt; It was `names`, not `name` that was object =)
&gt; 
&gt; In fact, both are objects. For the first case in bug description, Firefox 3.5
&gt; says object/object.

This is very fascinating to me. Thanks for pointing it out.

It looks like Firefox special cases a number of the properties on Window. A glance at their IDL files shows a comment that these variables are &quot;replaceable&quot;. So you end up with the following behavior:

  window.name;            // &quot;&quot;
  window.name = 1;        // (the result is 1, expected chainable assignment)
  window.name;            // &quot;1&quot;
  window.name = [1,2,3];  // (same thing, chainable)
  window.name;            // &quot;1,2,3&quot;
  
  // First time the user accesses the global &quot;name&quot; things go wild!
  name = 1;
  window.name;            // 1 (what!?!?!)
  window.name = [1,2,3];
  window.name;            // [1,2,3]

My guess is that Firefox was trying to save developers from the weirdness that is window.name. I don&apos;t really know which behavior I would recommend. So here is a test case:

  (function() {
    var arr = [];
    arr.push( typeof window.name ); 
    window.name = 1;
    arr.push( typeof window.name );
    name = 1;
    arr.push( typeof window.name );
    alert( arr );
  })();

Produces the following values in the browsers I have on my system:

  Safari 4.0.5 / WebKit Nightly and Chrome 5
  &quot;string,string,string&quot;

  Firefox 3.6.3 and Opera 10.5.3
  &quot;string,string,number&quot;

My interpretation of the HTML5 spec sides with Safari / Chrome. However, that doesn&apos;t mean its right. I did a quick search through the WHATWG archives and didn&apos;t see any discussion on the lists. There was something interested, and I would say &quot;related&quot; but not the same on public-html:
http://lists.w3.org/Archives/Public/public-html/2009Nov/0302.html</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>222898</commentid>
    <comment_count>7</comment_count>
    <who name="Joseph Pecoraro">joepeck</who>
    <bug_when>2010-05-08 10:26:51 -0700</bug_when>
    <thetext>I dug a bit more. That public-html link pointed to an HTML5 bug:
http://www.w3.org/Bugs/Public/show_bug.cgi?id=8241

It describes new WebIDL properties [ReplaceableNamedProperties] and [Replaceable]. Only the latter seems to be defined in WebIDL, and it matches the weirdness I saw above:
http://dev.w3.org/2006/webapi/WebIDL/#Replaceable

However, the HTML5 spec doesn&apos;t make window.name replaceable. Just a bunch of other properties!
http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#the-window-object</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>222899</commentid>
    <comment_count>8</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2010-05-08 10:36:36 -0700</bug_when>
    <thetext>WebKit also has the notion of replaceable properties, see e.g. DOMWindow.idl.

It would seem that adding [Replaceable] to the name attribute in our IDL could solve this, but I actually suspect that this has been discussed and investigated before, and could have been an intentional choice. Maybe you could try this change, and see which tests break?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>242189</commentid>
    <comment_count>9</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2010-06-23 17:49:50 -0700</bug_when>
    <thetext>See also: bug 41003.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>246486</commentid>
    <comment_count>10</comment_count>
      <attachid>60491</attachid>
    <who name="Joseph Pecoraro">joepeck</who>
    <bug_when>2010-07-04 20:42:27 -0700</bug_when>
    <thetext>Created attachment 60491
[TEST]: Test to Compare Browsers</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1872624</commentid>
    <comment_count>11</comment_count>
    <who name="Ahmad Saleem">ahmad.saleem792</who>
    <bug_when>2022-05-29 08:44:24 -0700</bug_when>
    <thetext>Outputs from all browsers:

Safari 15.5 on macOS 12.4 - string,string,string
Firefox Nightly 102 - string,string,string
Chrome Canary 104 - string,string,string

Since all browsers are consistent and have same output to attached test case. I think this bug should be closed and if there is spec issue then it should be raised separately. Thanks!</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1872996</commentid>
    <comment_count>12</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2022-05-31 09:51:41 -0700</bug_when>
    <thetext>Thank you for checking! Seems like the spec hasn&apos;t changed in this regard, so follow-up may be necessary indeed.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1873867</commentid>
    <comment_count>13</comment_count>
    <who name="Anne van Kesteren">annevk</who>
    <bug_when>2022-06-03 05:46:40 -0700</bug_when>
    <thetext>The specification always reflected WebKit here I think. So Firefox must have changed at some point to no longer have this property as `[Replaceable]`. All seems good. \o/</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>60491</attachid>
            <date>2010-07-04 20:42:27 -0700</date>
            <delta_ts>2010-07-04 20:42:27 -0700</delta_ts>
            <desc>[TEST]: Test to Compare Browsers</desc>
            <filename>test.html</filename>
            <type>text/html</type>
            <size>205</size>
            <attacher name="Joseph Pecoraro">joepeck</attacher>
            
              <data encoding="base64">PHNjcmlwdD4KKGZ1bmN0aW9uKCkgewogIHZhciBhcnIgPSBbXTsKICBhcnIucHVzaCggdHlwZW9m
IHdpbmRvdy5uYW1lICk7IAogIHdpbmRvdy5uYW1lID0gMTsKICBhcnIucHVzaCggdHlwZW9mIHdp
bmRvdy5uYW1lICk7CiAgbmFtZSA9IDE7CiAgYXJyLnB1c2goIHR5cGVvZiB3aW5kb3cubmFtZSAp
OwogIGFsZXJ0KCBhcnIgKTsKfSkoKTsKPC9zY3JpcHQ+Cg==
</data>

          </attachment>
      

    </bug>

</bugzilla>