本文介绍了Javascript:对象不支持方法'冻结'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Javascript中创建一个枚举。
使用的javascript代码是

I am trying to create an enummeration in Javascript.The javascript code used is

var FeatureName = {
"FEATURE1": 1,
"FEATURE2": 2,
"FEATURE3": 3,
"FEATURE4": 4,
"FEATURE5": 5
}
Object.freeze(FeatureName);

当方法 Object.freeze(FeatureName)被调用时,它可以工作适用于除IE7和IE8之外的所有浏览器。这有什么替代方案吗?

When the method Object.freeze(FeatureName), is called it works fine for all the browsers except IE7 and IE8. Is there any alternative to this?

推荐答案

您可以添加包含monkey-patch(只需要包含 es5-sham.min.js ),但是重要的是要明白这只会阻止错误出现。

You can add include the es5-sham "monkey-patch" (only need to include es5-sham.min.js) on your page, but it's important to understand that this will simply prevent the error from appearing.

尽管如此,它并不是真正的polyfill;来自项目的Github上的README:

It's not a true polyfill though; from the project's README on Github:

在所有传统引擎上静默失败。这应该没问题,除非你依赖于这种方法的安全和保障规定,这在传统引擎中是不可能的。

Silently fails on all legacy engines. This should be fine unless you are depending on the safety and security provisions of this method, which you cannot possibly obtain in legacy engines.

如果您想使用CDN,也可以从获取此库。

This library is also available from cdnjs if you want to use a CDN.

您可以使用此代码包含它(请注意将其限制为IE 8及更早版本的标签)

<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/es5-shim/4.0.5/es5-sham.min.js" type="text/javascript"></script>
<![endif]-->

这篇关于Javascript:对象不支持方法'冻结'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:33