Javascript Object.create Not Working In Firefox
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 w
Solution 1:
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 (typeofObject.create === 'undefined') {
Object.create = function (o) {
functionF() {}
F.prototype = o;
returnnewF();
};
}
Which I believe is from Crockford's Javascript: The Good Parts.
Solution 2:
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).
Solution 3:
I use this way(also working in ECMAScript 3):-
functioncustomCreateObject(p) {
if (p == null) throwTypeError(); // p must be a non-null objectif (Object.create) // If Object.create() is defined...returnObject.create(p); // then just use it.var t = typeof p; // Otherwise do some more type checkingif (t !== "object" && t !== "function") throwTypeError();
functionf() {}; // Define a dummy constructor function.
f.prototype = p; // Set its prototype property to p.returnnewf(); // Use f() to create an "heir" of p.
}
var obj = { eid: 1,name:'Xyz' };
customCreateObject(obj);
Post a Comment for "Javascript Object.create Not Working In Firefox"