RESOLVED FIXED 3537
object.constructor returns incorrect object
https://bugs.webkit.org/show_bug.cgi?id=3537
Summary object.constructor returns incorrect object
David Wheeler
Reported 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?
Attachments
testcase (231 bytes, text/html)
2005-06-14 23:24 PDT, Joost de Valk (AlthA)
no flags
Test case showing broken nameless function support. (716 bytes, text/html)
2005-09-27 11:25 PDT, Eric Seidel (no email)
no flags
A small patch to fix this. (845 bytes, patch)
2005-09-27 14:26 PDT, Eric Seidel (no email)
mjs: review+
David Wheeler
Comment 1 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!
David Wheeler
Comment 2 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.
David Wheeler
Comment 3 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.
Joost de Valk (AlthA)
Comment 4 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.
Joost de Valk (AlthA)
Comment 5 2005-06-14 23:24:32 PDT
Created attachment 2347 [details] testcase
Eric Seidel (no email)
Comment 6 2005-09-27 11:25:58 PDT
Created attachment 4069 [details] Test case showing broken nameless function support.
Eric Seidel (no email)
Comment 7 2005-09-27 12:41:47 PDT
*** Bug 4042 has been marked as a duplicate of this bug. ***
Eric Seidel (no email)
Comment 8 2005-09-27 14:26:08 PDT
Created attachment 4071 [details] A small patch to fix this.
Maciej Stachowiak
Comment 9 2005-09-27 15:46:08 PDT
I suggested to Eric that he also test the case of the Function constructor.
Maciej Stachowiak
Comment 10 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.
David Wheeler
Comment 11 2005-11-02 08:48:01 PST
Shouldn't the first example in the test case also evaluate to true?
Note You need to log in before you can comment on or make changes to this bug.