Javascript use strict error not catching

天大地大妈咪最大 提交于 2019-12-12 09:53:00

问题


I am creating a backbone.js app that uses require.js for AMD. In order to check for use strict support in the browser, I have included the following code. However, when the code is run, the error thrown by var o = {p:1, P:2} is not caught as I expect it to be, and instead kills the entire page.

Chrome console prints this error: Uncaught SyntaxError: Duplicate data property in object literal not allowed in strict mode

require([
    'jquery',
    'underscore',
    'backbone',
    'src/app'
], function(jQuery, _, Backbone, App){
    "use strict"

    var tooOld = true,
    isStrictMode = function () {
        try{
            var o = {p:1, p:2};
        } catch (e) {
            tooOld = false;
            new App;
        } finally {
            if (tooOld) {
                // Display some message
            }
        }
    }();
});

Why is the error crash my page instead of being caught? How can I fix this?


回答1:


If you want to check for strict mode support, consider:

function supportsStrict() {
  'use strict';
  return typeof function(){return this;}() == 'undefined';
}

console.log(supportsStrict()); // true if supports strict mode

That way you can test independently and run different code branches depending on the result.



来源:https://stackoverflow.com/questions/25479453/javascript-use-strict-error-not-catching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!