Javascript uses strict error not catching

I am creating a backbone.js app that uses require.js for AMD. To check use strict

browser support , I have included the following code. However, when the code runs, the error thrown var o = {p:1, P:2}

is not caught as I expect and instead kills the entire page.

Chrome console prints this error: Inactive SyntaxError: Duplication of data property in an object literal is 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 crashing into my page and not a trap? How can I fix this?

+3


source to share


1 answer


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

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

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

      



This way you can test on your own and run different branches of code depending on the result.

+2


source