No, you can't disable strict mode per function.
It's important to understand that strict mode works lexically; meaning — it affects function declaration, not execution. Any function declared within strict code becomes a strict function itself. But not any function called from within strict code is necessarily strict:
(function(sloppy) {
"use strict";
function strict() {
// this function is strict, as it is _declared_ within strict code
}
strict();
sloppy();
})(sloppy);
function sloppy(){
// this function is not strict as it is _declared outside_ of strict code
}
Notice how we can define function outside of strict code and then pass it into the function that's strict.
You can do something similar in your example — have an object with "sloppy" functions, then pass that object to that strict immediately invoked function. Of course, that won't work if "sloppy" functions need to reference variables from within main wrapper function.
Also note that indirect eval — suggested by someone else — won't really help here. All it does is execute code in global context. If you try to call a function that's defined locally, indirect eval won't even find it:
(function(){
"use strict";
function whichDoesSomethingNaughty(){ /* ... */ }
// ReferenceError as function is not globally accessible
// and indirect eval obviously tries to "find" it in global scope
(1,eval)('whichDoesSomethingNaughty')();
})();
This confusion about global eval probably comes from the fact that global eval can be used to get access to global object from within strict mode (which isn't simply accessible via this
anymore):
(function(){
"use strict";
this; // undefined
(1,eval)('this'); // global object
})();
But back to the question...
You can kind of cheat and declare a new function via Function
constructor — which happens to not inherit strictness, but that would rely on (non-standard) function decompilation and you would lose ability to reference outer variables.
(function(){
"use strict";
function strict(){ /* ... */ }
// compile new function from the string representation of another one
var sneaky = Function('return (' + strict + ')()');
sneaky();
})();
Note that FF4+ seems to disagree with spec (from what I can tell) and incorrectly marks function created via Function
as strict. This doesn't happen in other strict-mode-supporting implementations (like Chrome 12+, IE10, WebKit).