[v8-users] Object.create deviating from Crockford's implementation

224 views
Skip to first unread message

Andrey Fedorov

unread,
Apr 27, 2010, 3:27:56 PM4/27/10
to v8-u...@googlegroups.com
When playing with some JS in Chrome, I noticed that the native implementation of Object.create throws a TypeError exception (Object prototype may only be an Object or null) if a function object is passed in, like:

    Object.create(function () {})

The error is both factually incorrect and inconsistent with Crockford's original implementation, which works fine, if you pass in a function (even in Chrome):

    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };

Oddly enough, I'm only seeing this on the Linux and OSX versions of Chrome, not on Windows. Any ideas why this could be?

- Andrey

--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users

Ondřej Žára

unread,
Apr 27, 2010, 3:55:59 PM4/27/10
to v8-u...@googlegroups.com
Hi Andrey,

I have no idea why this inconsistent behavior is present. I am, however, interested in your use case, beucase I do not see any good in passing a function to Object.create. Typically, one does

var Parent = function() {};
var Child = function() {};
Child.prototype = Object.create(Parent.prototype); /* inheritance without constructor execution */

So I wonder if your interest is based on curiosity, or some real-life implementation where it is desirable to call Object.create(function(){}). Note that weird things can happen:

var F = function() {};
F.prototype = function() {};
var f = new F();
"apply" in f; // true! you just "inherited" a function :)



Ondrej





2010/4/27 Andrey Fedorov <anfe...@gmail.com>

Rico Wind

unread,
May 7, 2010, 2:47:21 AM5/7/10
to v8-u...@googlegroups.com, anfe...@gmail.com
I have created a bug for this:
http://code.google.com/p/v8/issues/detail?id=697

I can, however, also reproduce this on windows.

Andrey, if possible, could you state which version of chrome this was
performed in on all platforms? Also, could you please state if it was
32 or 64 bit versions of chrome?

Rico

Andrey Fedorov

unread,
Jul 7, 2010, 11:50:33 PM7/7/10
to Rico Wind, v8-u...@googlegroups.com
Sorry for not following up earlier, it was the 32 bit version. The fix seems to have already shipped (at least in the dev channel).

Thanks for the quick turnaround, and the amazing browser! :)

Cheers,
Andrey

Andrey Fedorov

unread,
Jul 7, 2010, 11:53:14 PM7/7/10
to v8-u...@googlegroups.com, ondre...@gmail.com
On Tue, Apr 27, 2010 at 3:55 PM, Ondřej Žára <ondre...@gmail.com> wrote:
I have no idea why this inconsistent behavior is present. I am, however, interested in your use case, beucase I do not see any good in passing a function to Object.create.

I didn't get a chance to trace down the precise usage, but the bug showed up on this page.

- Andrey
Reply all
Reply to author
Forward
0 new messages