Javascript Object.create无法在Firefox中运行

dom*_*nos 4 javascript object-create

我总是在Firefox(3.6.14)中得到以下异常:

TypeError: Object.create is not a function
Run Code Online (Sandbox Code Playgroud)

这非常令人困惑,因为我非常确定它是一个功能,并且代码在Chrome上按预期工作.

负责此行为的代码行如下:

Object.create( Hand ).init( cardArr );
Object.create( Card ).init( value, suit );
Run Code Online (Sandbox Code Playgroud)

如果有人想看到所有代码,它来自扑克图书馆gaga.js:https://github.com/SlexAxton/gaga.js

也许有人知道如何让它在Firefox中运行?

Ale*_*yne 13

Object.create()是EMCAScript5的新功能.遗憾的是,本机代码并没有广泛支持它.

虽然您应该能够使用此代码段添加非本机支持.

if (typeof Object.create === 'undefined') {
    Object.create = function (o) { 
        function F() {} 
        F.prototype = o; 
        return new F(); 
    };
}
Run Code Online (Sandbox Code Playgroud)

我相信这是来自Crockford的Javascript:The Good Parts.

  • 我不推荐使用Crockford的`Object.create`垫片,因为ES5`Object.create`方法可以做一些事情,并且**无法在ES3环境中进行模拟...有了这种垫片最终有两个不一致的实现,原生和预期的ES5方法,以及非标准的方法.[更多信息](http://stackoverflow.com/questions/3830800/#3844768) (5认同)