0

if I create an object in JavaScript like so:

var foo = {
create: function(){}
}

will that overwrite the Object.create function?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • No, it won't. And no, you definitely may not do that anyway. Why are you asking? – Bergi Feb 18 '15 at 00:17
  • related: [Why is it Object.defineProperty() rather than this.defineProperty()](http://stackoverflow.com/q/13239317/1048572) – Bergi Aug 05 '15 at 04:13

2 Answers2

4

No, it won't.

The object that you create will have a create method, i.e. foo.create, but the Object object still has its own create function.

Note that the Object.create function isn't in Object.prototype.create i.e. it's not a method that exists in any object that is created, it only exists as a property of the Object function object.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • It can also be accessed using the constructor of an object like `foo.constructor.create(foo)` – jungy Feb 18 '15 at 00:24
  • @jungy: That depends so much on `foo` that this shouldn't be used. – Bergi Feb 18 '15 at 00:28
  • @Bergi Yes, I totally agree it shouldn't used. I just don't want people to assume that a constructor and instance are the same thing. – jungy Feb 18 '15 at 00:38
1

No, because create is a property of foo.

For backward compatible Object.create:

if(!Object.create){
  Object.create = function(o){
    function F(){}; F.prototype = o;
    return new F;
  }
}
StackSlave
  • 10,613
  • 2
  • 18
  • 35