4

I always get the following exception in Firefox (3.6.14):

TypeError: Object.create is not a function

It is quite confusing because I am pretty sure it is a function and the code works as intended on Chrome.

The lines of code responsible for this behavior are the following:

Object.create( Hand ).init( cardArr );
Object.create( Card ).init( value, suit );

It is from a poker library gaga.js if someone wants to see all the code: https://github.com/SlexAxton/gaga.js

Maybe someone knows how to get it working in Firefox?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
dominos
  • 412
  • 1
  • 11
  • 21

3 Answers3

13

Object.create() is a new feature of EMCAScript5. Sadly it is not widely supported with native code.

Though you should be able to add non-native support with this snippet.

if (typeof Object.create === 'undefined') {
    Object.create = function (o) { 
        function F() {} 
        F.prototype = o; 
        return new F(); 
    };
}

Which I believe is from Crockford's Javascript: The Good Parts.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • 5
    I wouldn't recommend the Crockford's `Object.create` shim, since there are some things that the ES5 `Object.create` method can do and there's **no way** to emulate on an ES3 environment... With this kind of shims one ends up having two inconsistent implementations, the native and expected ES5 method, and a non-standard one. [More info](http://stackoverflow.com/questions/3830800/#3844768) – Christian C. Salvadó Mar 04 '11 at 21:04
0

I use this way(also working in ECMAScript 3):-

function customCreateObject(p) {
   if (p == null) throw TypeError(); // p must be a non-null object
   if (Object.create)  // If Object.create() is defined...
     return Object.create(p);  // then just use it.
   var t = typeof p; // Otherwise do some more type checking
   if (t !== "object" && t !== "function") throw TypeError();
    function f() {}; // Define a dummy constructor function.
   f.prototype = p; // Set its prototype property to p.
   return new f(); // Use f() to create an "heir" of p.
}

var obj = { eid: 1,name:'Xyz' };
customCreateObject(obj);
vineet
  • 13,832
  • 10
  • 56
  • 76
0

Object.create is part of ES5 and only available in Firefox 4.

As long as you are not doing any add-on development for browsers, you should not expect browsers to implement ES5 features (especially older browsers). You'd have to provide your own implementation then (like the own provided by @Squeegy).

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143