问题
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