我遇到了一个插件问题,该插件在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
仅是您所需要的,并且仅使用它来纯粹设置原型链,则这是一个轻量级的poly-fill,它不支持null
作为第一个参数,也不支持第二个properties
参数。
这是规格:
15.2.3.5 Object.create(O [,属性])
create函数使用指定的原型创建一个新对象。调用create函数时,将执行以下步骤:
如果Type(O)不是Object或Null,则抛出TypeError异常。
令obj为创建新对象的结果,就像通过表达式new Object()一样,其中Object是具有该名称的标准内置构造函数
将obj的[[Prototype]]内部属性设置为O。
如果存在参数Properties且未定义参数,则将自身的属性添加到obj,就像通过调用带有参数obj和Properties的标准内置函数Object.defineProperties一样。
返回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();
};
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句