Object.create not supported in ie8

前端 未结 3 459
北恋
北恋 2020-12-09 09:36

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:

SC         


        
3条回答
  •  余生分开走
    2020-12-09 09:40

    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:

    if (!Object.create) {
        Object.create = function(proto, props) {
            if (typeof props !== "undefined") {
                throw "The multiple-argument version of Object.create is not provided by this browser and cannot be shimmed.";
            }
            function ctor() { }
            ctor.prototype = proto;
            return new ctor();
        };
    }
    

提交回复
热议问题