Javascript Object.create 在 Firefox 中不起作用

发布于 2024-10-20 12:40:03 字数 456 浏览 3 评论 0原文

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

TypeError: Object.create is not a function

这很令人困惑,因为我很确定它是一个函数,并且代码在 Chrome 上按预期工作。

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

Object.create( Hand ).init( cardArr );
Object.create( Card ).init( value, suit );

如果有人想查看所有代码,则来自扑克库 gaga.js: https://github.com/SlexAxton/gaga.js

也许有人知道如何让它在 Firefox 中工作?

I always get the following exception in Firefox (3.6.14):

TypeError: Object.create is not a function

It is quite confusing because I am pretty sure it is a function and the code works as intended on Chrome.

The lines of code responsible for this behavior are the following:

Object.create( Hand ).init( cardArr );
Object.create( Card ).init( value, suit );

It is from a poker library gaga.js if someone wants to see all the code: https://github.com/SlexAxton/gaga.js

Maybe someone knows how to get it working in Firefox?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落花浅忆 2024-10-27 12:40:03

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

尽管您应该能够使用此代码片段添加非本机支持。

if (typeof Object.create === 'undefined') {
    Object.create = function (o) { 
        function F() {} 
        F.prototype = o; 
        return new F(); 
    };
}

我相信这是来自 Crockford 的 Javascript:好的部分

Object.create() is a new feature of EMCAScript5. Sadly it is not widely supported with native code.

Though you should be able to add non-native support with this snippet.

if (typeof Object.create === 'undefined') {
    Object.create = function (o) { 
        function F() {} 
        F.prototype = o; 
        return new F(); 
    };
}

Which I believe is from Crockford's Javascript: The Good Parts.

小…红帽 2024-10-27 12:40:03

Object.create 是一部分ES5 的版本,仅在 Firefox 4 中可用。

只要您没有为浏览器进行任何附加开发,您就不应该期望浏览器实现 ES5 功能(尤其是较旧的浏览器)。然后,您必须提供自己的实现(就像自己的实现一样由@Squeegy提供)。

Object.create is part of ES5 and only available in Firefox 4.

As long as you are not doing any add-on development for browsers, you should not expect browsers to implement ES5 features (especially older browsers). You'd have to provide your own implementation then (like the own provided by @Squeegy).

深陷 2024-10-27 12:40:03

我使用这种方式(也在 ECMAScript 3 中工作):-

function customCreateObject(p) {
   if (p == null) throw TypeError(); // p must be a non-null object
   if (Object.create)  // If Object.create() is defined...
     return Object.create(p);  // then just use it.
   var t = typeof p; // Otherwise do some more type checking
   if (t !== "object" && t !== "function") throw TypeError();
    function f() {}; // Define a dummy constructor function.
   f.prototype = p; // Set its prototype property to p.
   return new f(); // Use f() to create an "heir" of p.
}

var obj = { eid: 1,name:'Xyz' };
customCreateObject(obj);

I use this way(also working in ECMAScript 3):-

function customCreateObject(p) {
   if (p == null) throw TypeError(); // p must be a non-null object
   if (Object.create)  // If Object.create() is defined...
     return Object.create(p);  // then just use it.
   var t = typeof p; // Otherwise do some more type checking
   if (t !== "object" && t !== "function") throw TypeError();
    function f() {}; // Define a dummy constructor function.
   f.prototype = p; // Set its prototype property to p.
   return new f(); // Use f() to create an "heir" of p.
}

var obj = { eid: 1,name:'Xyz' };
customCreateObject(obj);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文