2013-02-13 51 views
0

有没有办法让这个代码向后兼容IE6/7/8?旧的IE的JavaScript Object.create

function Foo() { 
    ... 
} 

function Bar() { 
    ... 
} 

Bar.prototype = Object.create(Foo.prototype); 

主要问题是Object.create错误,然后崩溃浏览器。

所以有一个功能,我可以放在模拟旧的IE浏览器的行为Object.create。或者我应该如何重新编码以上解决方法?

+7

[在MDN文档中有一个polyfill。](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create) – Pointy 2013-02-13 22:40:29

+0

@Pointy如果你想要一个答案代表。 – Petah 2013-02-13 22:57:17

+0

https://github.com/kriskowal/es5-shim – Bergi 2013-02-13 23:25:24

回答

1

一个答案尖尖的评论

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create#Polyfill

if (!Object.create) { 
    Object.create = function (o) { 
     if (arguments.length > 1) { 
      throw new Error('Object.create implementation only accepts the first parameter.'); 
     } 
     function F() {} 
     F.prototype = o; 
     return new F(); 
    }; 
} 

此填充工具覆盖被创建,其原型已被选定,但并不需要第二个参数为一个新的对象主要用例帐户。