There are many solutions here, the only one I think that will suit is to attach the function to the global object so it looks like a declared function. The only difference is that it won't be available until a runs:
function a() {
// Protect against ES5 strict mode, assume function is called as global code
if (typeof this !== undefined) {
this.b = function() { /* whatever */ };
}
}
or perhaps the following suits your coding style better:
function a() {
function b() { /* whatever */};
if (typeof this !== undefined) {
this.b = b;
}
}
Called simply as:
a();
then in ES3 or ES5 non-strict mode, it will work as expected. To overcome ES5 strict limitations where the above will result in a's this being undefined, call a as global code and set its this explicitly:
a.call(this);
or with some other suitable reference to the global object.
I have stayed away from using window since that is less reliable, not least because non–browser hosts will likely not have a window object.