ie8中不支持Object.create [英] Object.create not supported in ie8

查看:937
本文介绍了ie8中不支持Object.create的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

以下是代码:

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

IE8或更多跨浏览器兼容有什么好处?

What is a good work around for IE8 or more cross browser compatible?

提前致谢!

推荐答案

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

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.

但是,如果 Object.create 是你唯一需要的东西而你只用它来纯粹设置原型链,这里是一个轻量级的poly-fill,不支持 null 作为第一个参数,不支持第二个属性参数。

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 support null as the first argument and doesn't support the second properties argument.

这是规格:


15.2 .3.5 Object.create(O [,Properties])

15.2.3.5 Object.create ( O [, Properties] )

create函数使用指定的原型创建一个新对象。
调用create函数时,将执行以下步骤:

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

如果Type(O)不是Object或Null则抛出TypeError异常。

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

设obj是创建一个新对象的结果,好像通过表达式
new Object()其中Object是标准的内置构造函数,
name

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

将obj的[[Prototype]]内部属性设置为O。

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

如果参数属性为现在而不是未定义,将自己的
属性添加到obj,就好像通过调用带有参数obj和Properties的标准内置函数
Object.defineProperties一样。

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.

返回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();
    };
}

这篇关于ie8中不支持Object.create的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆