function foo(doBar) { function bar() { console.log( 'bar' ); } function baz() { console.log( 'baz' ); } window.baz = baz; if ( doBar ) bar(); }