The funniest example of strict mode hatred is in the RiverTrail code base, where NBody.js starts as follows:
/*
* ...
*/
"DO NOT use strict";
var NBody = {
// ...
But if you look at what strict mode takes away then you find out that it only serves to make JavaScript a cleaner language.
with (obj.foo) {
bar = ...;
}
you can use an IIFE [3] and write
(function (o) {
o.bar = ...;
}(obj.foo));
var MyMath = {
fac: function (n) {
if (n <= 0) {
return 1;
} else {
return n * arguments.callee(n-1);
}
}
};
A named function expression looks like a function declaration (a statement), but is in fact an expression. It gives the function a name that is only accessible from within the function. That allows you to rewrite the above code as follows:
var MyMath = {
fac: function me(n) {
if (n <= 0) {
return 1;
} else {
return n * me(n-1);
}
}
};
(function () {
// private data
...
// public data
this.myModule = { // avoid!
...
};
}());
Strict mode does not allow the above – this is undefined in non-method functions. You can use the following work-around:
(function (global) {
// private data
...
// public data
global.myModule = {
...
};
}(this)); // top-level |this| points to global object
But you might not even need to access the global object inside the IIFE:
this.myModule = function () {
// private data
...
// public data
return {
...
};
}();