gpt4 book ai didi

javascript - ie8 不支持 Object.create

转载 作者:行者123 更新时间:2023-12-03 21:43:10 24 4
gpt4 key购买 nike

我遇到了一个插件问题,该插件在 jquery 中使用 object.create 来创建日期下拉列表。我刚刚注意到 IE 8 中抛出了一个错误:

SCRIPT438: Object doesn't support property or method 'create'

这是代码:

var dropdateobj = Object.create(dropdatefuncs);
dropdateobj.create(options, this);
$.data(this, 'dropdate', dropdateobj);

对于 IE8 或更高版本的跨浏览器兼容,有什么好的解决办法吗?

提前致谢!

最佳答案

如果您需要Object.create,那么您很可能还需要依赖其他 es5 功能。因此,在大多数情况下,适当的解决方案是使用 es5-shim .

但是,如果 Object.create 是您唯一需要的东西,并且您仅使用它来纯粹设置原型(prototype)链,那么这里有一个不支持 null< 的轻量级 Poly-fill 作为第一个参数,不支持第二个 properties 参数。

这是规范:

15.2.3.5 Object.create ( O [, Properties] )

The create function creates a new object with a specified prototype. When the create function is called, the following steps are taken:

If Type(O) is not Object or Null throw a TypeError exception.

Let obj be the result of creating a new object as if by the expression new Object() where Object is the standard built-in constructor with that name

Set the [[Prototype]] internal property of obj to O.

If the argument Properties is present and not undefined, add own properties to obj as if by calling the standard built-in function Object.defineProperties with arguments obj and Properties.

Return obj.

这是轻量级实现:

if (!Object.create) {
Object.create = function(o, properties) {
if (typeof o !== 'object' && typeof o !== 'function') throw new TypeError('Object prototype may only be an Object: ' + o);
else if (o === null) throw new Error("This browser's implementation of Object.create is a shim and doesn't support 'null' as the first argument.");

if (typeof properties != 'undefined') throw new Error("This browser's implementation of Object.create is a shim and doesn't support a second argument.");

function F() {}

F.prototype = o;

return new F();
};
}

关于javascript - ie8 不支持 Object.create,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18020265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com