henricoo
henricoo

Reputation: 17

Method added to ECMASscript 5th edition, why this conditional check?

if(typeof Object.create !== "function") {

   Object.create = function(o) {

      function F(){}
      F.prototype = 0;
      return new F();
};

Why, if(typeof Object.create !== "function") if the method is just being created and as a built in? Why the need to check if it is a function if that is as said being created?

Upvotes: 0

Views: 49

Answers (2)

codejockie
codejockie

Reputation: 10864

The Object.create() method creates a new object, using an existing object to provide the newly created object's ___proto____. -- MDN

That condition checks if Object.create() is available for use. In some JS engine, this is not the case so that's why that check is necessary.

Upvotes: 0

Quentin
Quentin

Reputation: 943560

This tests to see if the JS engine has a native Object.create method.

If it does not, then it adds one.

This is to avoid replacing the built-in method (which is likely to be heavily optimised) with one written in JS.

Upvotes: 3

Related Questions