I came across an issue with a plugin that uses object.create in jquery to create a date dropdown. I just noticed in IE 8 that it is throwing an error of:
SCRIPT438: Object doesn't support property or method 'create'
Here is the code:
var dropdateobj = Object.create(dropdatefuncs);
dropdateobj.create(options, this);
$.data(this, 'dropdate', dropdateobj);
What is a good work around for IE8 or more cross browser compatible?
Thanks in advance!
If you need
Object.create
, there are good chances you may need to rely on other es5 features as well. Therefore, in most cases the appropriate solution would be to use es5-shim.However, if
Object.create
is the only thing you need and you only use it to purely setup the prototype chain, here's a lightweight poly-fill that doesn't supportnull
as the first argument and doesn't support the secondproperties
argument.Here's the spec:
Here's the lightweight implementation:
This will make Object.create() work in IE 8.
I tried the shim/sham but that didn't work for me.
There are several shims that provide this, including this one.
Note that
Object.create
can't be perfectly shimmed, though, because amongst other things it can create non-enumerable properties or properties with getters and setters, which you can't do on all pre-ES5 browsers. (You can do getters and setters on some pre-ES5 browsers using proprietary syntax, but not on IE8 I don't believe.) It can only be pseudo-shimmed.But a pseudo-shim will do for the use-case you've quoted.
Just for completeness, here's a simple version of the part that can be shimmed: