为什么我的JavaScript在Safari上的严格模式下无法正常工作? [英] Why is my JavaScript not working correctly in strict mode on Safari?

查看:313
本文介绍了为什么我的JavaScript在Safari上的严格模式下无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个使用一些简单JavaScript的网站.经过一些测试,我发现我的JavaScript在iOS设备上的表现与我可以测试的所有其他设备相比都大不相同.

I have built a website that uses some simple JavaScript. After some testing, I have found that my JavaScript is behaving very differently on iOS devices as compared to all other devices that I could test with.

经过数小时的反复试验,我发现意外行为仅在严格模式下发生,但是很难进一步解决问题,因为我的软件/硬件仅限于Apple开发和测试.

After several hours of trial-and-error, I have discovered that the unexpected behaviour occurs only in strict mode, but it has been very hard to troubleshoot the problem further because I am limited in my software/hardware for Apple development and testing.

为什么我的代码不能在严格模式下工作,而只能在某些设备(尤其是Apple设备)上工作?

Why could my code not be working in strict mode, and only certain (Apple, in particular) devices?

推荐答案

可能的问题

您的问题可能是您在启用严格模式的情况下进行了const声明.

如果是这种情况,并且您想继续使用严格模式,则一种解决方案是使用var而不是const,并且要更加小心地将变量视为常量.

If that is the case, and you would like to continue using strict mode, one solution is to use var instead of const, and simply be more careful to treat the variable as a constant.

实际上,如果没有OS X计算机,则很难调试此问题,因为您无法使用iOS Safari的远程控制台"Web Inspector".

It is in fact difficult to debug this problem without an OS X computer, because you are unable to use iOS Safari's remote console "Web Inspector".

但是,对于Safari和iOS Safari的当前版本(9.1和9.1),根据caniuse 撰写时分别为9.3)const是:

However, according to caniuse, for the current versions of both Safari and iOS Safari (9.1 and 9.3, respectively, as of writing) const is:

仅在非严格模式下才能识别

Only recognized when NOT in strict mode

您可以使用以下示例(或此JSFiddle )自己进行测试.此代码可以在Ubuntu和Android上的Chrome和Firefox上正常使用,但在iOS或iOS Safari的Firefox上却无法使用.

You can test this for yourself using the following example (or this JSFiddle). This code worked as expected for me with Chrome and Firefox on both Ubuntu and Android, but not on Firefox for iOS or iOS Safari.

HTML

<pre></pre>
<form>
    <input type="submit">
</form>

JS

"use strict";

var form = document.querySelector("form");
var info = document.querySelector("pre");

// This const declaration in conjunction with strict mode will
// cause this script not to work on iOS devices
const _MEANINGLESS = 0;

form.addEventListener("submit", function(event) {
    event.preventDefault();
    info.textContent = "Submit event captured.\n";
});

这篇关于为什么我的JavaScript在Safari上的严格模式下无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆