Reputation: 17
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
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
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