Javascript Object.create在Firefox中不工作
在Firefox(3.6.14)中,我总是遇到以下异常: 这是相当令人困惑的,因为我非常确定它是一个函数,并且代码在Chrome上可以正常工作 负责此行为的代码行如下所示:Javascript Object.create在Firefox中不工作,javascript,object-create,Javascript,Object Create,在Firefox(3.6.14)中,我总是遇到以下异常: 这是相当令人困惑的,因为我非常确定它是一个函数,并且代码在Chrome上可以正常工作 负责此行为的代码行如下所示: Object.create( Hand ).init( cardArr ); Object.create( Card ).init( value, suit ); 如果有人想查看所有代码,它来自扑克库gaga.js: 也许有人知道如何在Firefox中使用它?是ES5的一部分,仅在Firefox4中可用 只要您没有为浏览器
Object.create( Hand ).init( cardArr );
Object.create( Card ).init( value, suit );
如果有人想查看所有代码,它来自扑克库gaga.js:
也许有人知道如何在Firefox中使用它?是ES5的一部分,仅在Firefox4中可用
只要您没有为浏览器进行任何附加开发,就不应该期望浏览器实现ES5功能(尤其是较旧的浏览器)。然后,您必须提供自己的实现()。对象。create()
是EMCAScript5的一个新功能。遗憾的是,本机代码并不广泛支持它
尽管您应该能够使用此代码段添加非本机支持
if (typeof Object.create === 'undefined') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
我相信这是来自Crockford的Javascript:好的部分。我使用这种方式(也使用ECMAScript 3):-
Alex偶尔会打开,所以他可能会回答:-)我不推荐Crockford的
对象。创建垫片,因为ES5对象可以做一些事情。创建方法无法在ES3环境中进行模拟。。。使用这种垫片,最终会出现两种不一致的实现,即本机和预期的ES5方法,以及一种非标准的方法。
if (typeof Object.create === 'undefined') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
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);