Javascript Object.create not working in Firefox

前端 未结 3 1836
自闭症患者
自闭症患者 2020-12-03 19:29

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 pre

3条回答
  •  日久生厌
    2020-12-03 20:21

    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);
    

提交回复
热议问题