Bug 3537 - object.constructor returns incorrect object
Summary: object.constructor returns incorrect object
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 412
Hardware: Mac OS X 10.4
: P2 Normal
Assignee: Eric Seidel (no email)
URL:
Keywords:
: 4042 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-06-14 18:33 PDT by David Wheeler
Modified: 2005-11-02 08:48 PST (History)
2 users (show)

See Also:


Attachments
testcase (231 bytes, text/html)
2005-06-14 23:24 PDT, Joost de Valk (AlthA)
no flags Details
Test case showing broken nameless function support. (716 bytes, text/html)
2005-09-27 11:25 PDT, Eric Seidel (no email)
no flags Details
A small patch to fix this. (845 bytes, patch)
2005-09-27 14:26 PDT, Eric Seidel (no email)
mjs: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description David Wheeler 2005-06-14 18:33:03 PDT
A growing trend (IMO) in JavaScript is to use objects for namespaces. However, when an object 
constructor is an attribute of an object, the Function.toString() method thinks its an internal function. 
For example, this code:

  var Foo = {};
  Foo.Bar = function () {
    this.bar = 1;
  }; 
  var f = new Foo.Bar();
  document.write(Function.toString.call(f.constructor));

Outputs "(Internal Function)" in Safari and "function () { this.bar = 1; }" in Firefox.  Can Safari be updated 
to output the same string as Firefox?
Comment 1 David Wheeler 2005-06-14 18:40:43 PDT
Actually, looking more closly at it, I realize that the problem is not the stringification of the constructor 
of an object created in a class that uses objects for namespaces. The problem is that 
Function.constructor() returns the wrong function object!

  var Foo = {};
  Foo.Bar = function () {
    this.bar = 1;
  };
  var f = new Foo.Bar();
  document.write(Function.toString.call(f.constructor));

Again, this outputs "(Internal Function)" because the return value of f.constructor is not the actual 
constructor function. I have no idea what function it's returning.

Thanks!
Comment 2 David Wheeler 2005-06-14 18:41:41 PDT
> Function.constructor() returns the wrong function object!

Uh, make that the constructor attribute of the object has the wrong object.
Comment 3 David Wheeler 2005-06-14 18:53:36 PDT
Here's a simpler test case:

  var Foo = { Bar: function () {}};
  var f = new Foo.Bar();
  document.write(f.constructor == Foo.Bar);

This code outputs true in Firefox, but false in Safari. It should output true.
Comment 4 Joost de Valk (AlthA) 2005-06-14 23:23:06 PDT
I'll add the testcase in the comment below as a real testcase, please do so next time you post a bug :). And 
i will confirm it, since the behavior IS different from firefox and some will have to look in to it.
Comment 5 Joost de Valk (AlthA) 2005-06-14 23:24:32 PDT
Created attachment 2347 [details]
testcase
Comment 6 Eric Seidel (no email) 2005-09-27 11:25:58 PDT
Created attachment 4069 [details]
Test case showing broken nameless function support.
Comment 7 Eric Seidel (no email) 2005-09-27 12:41:47 PDT
*** Bug 4042 has been marked as a duplicate of this bug. ***
Comment 8 Eric Seidel (no email) 2005-09-27 14:26:08 PDT
Created attachment 4071 [details]
A small patch to fix this.
Comment 9 Maciej Stachowiak 2005-09-27 15:46:08 PDT
I suggested to Eric that he also test the case of the Function constructor.
Comment 10 Maciej Stachowiak 2005-09-27 15:57:44 PDT
Comment on attachment 4071 [details]
A small patch to fix this.

r=me

Make sure to make the test case also cover the new Function() case.
Comment 11 David Wheeler 2005-11-02 08:48:01 PST
Shouldn't the first example in the test case also evaluate to true?