javascript ie8 不支持 Object.create
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18020265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object.create not supported in ie8
提问by klye_g
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:
我遇到了一个插件问题,该插件在 jquery 中使用 object.create 创建日期下拉列表。我刚刚在 IE 8 中注意到它抛出了一个错误:
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?
对于 IE8 或更多跨浏览器兼容,有什么好的解决方法?
Thanks in advance!
提前致谢!
回答by plalx
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
,您很有可能还需要依赖其他 es5 功能。因此,在大多数情况下,适当的解决方案是使用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 support null
as the first argument and doesn't support the second properties
argument.
但是,如果这Object.create
是您唯一需要的东西并且您只使用它来纯粹设置原型链,那么这里有一个不支持null
作为第一个参数并且不支持第二个properties
参数的轻量级 poly-fill 。
Here's the spec:
这是规格:
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.
15.2.3.5 Object.create ( O [, Properties] )
create 函数创建一个具有指定原型的新对象。调用create函数时,采取以下步骤:
如果 Type(O) 不是 Object 或 Null 则抛出 TypeError 异常。
让 obj 成为创建新对象的结果,就像通过表达式 new Object() 其中 Object 是具有该名称的标准内置构造函数
将 obj 的 [[Prototype]] 内部属性设置为 O。
如果参数 Properties 存在且未定义,则将自己的属性添加到 obj,就像通过使用参数 obj 和 Properties 调用标准内置函数 Object.defineProperties 一样。
返回对象。
Here's the lightweight implementation:
这是轻量级实现:
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();
};
}
回答by T.J. Crowder
There are several shims that provide this, including this one.
有几种垫片可以提供这一点,包括这个。
Note that Object.create
can'tbe 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.
但是请注意,这不能完美地填充,因为除其他外,它可以创建不可枚举的属性或具有 getter 和 setter 的属性,而这在所有 ES5 之前的浏览器上都无法做到。(您可以使用专有语法在某些 ES5 之前的浏览器上执行 getter 和 setter,但我不相信在 IE8 上不能。)它只能是伪填充。Object.create
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();
};
}
回答by Sorter
This will make Object.create() work in IE 8.
这将使 Object.create() 在 IE 8 中工作。
I tried the shim/sham but that didn't work for me.
我尝试了 shim/sham,但这对我不起作用。
if (typeof Object.create !== 'function') {
Object.create = function(o, props) {
function F() {}
F.prototype = o;
if (typeof(props) === "object") {
for (prop in props) {
if (props.hasOwnProperty((prop))) {
F[prop] = props[prop];
}
}
}
return new F();
};
}