Bug 3537

Summary: object.constructor returns incorrect object
Product: WebKit Reporter: David Wheeler <david>
Component: JavaScriptCoreAssignee: Eric Seidel (no email) <eric>
Status: RESOLVED FIXED    
Severity: Normal CC: paul, vicki
Priority: P2    
Version: 412   
Hardware: Mac   
OS: OS X 10.4   
Attachments:
Description Flags
testcase
none
Test case showing broken nameless function support.
none
A small patch to fix this. mjs: review+

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?