You must be experiencing JavaScript's garbage collection here. When the immediately invoked closure is cleaned up, the variables created inside are cleaned up as well (and your window goes away, because it has no variable referencing it anymore). To keep the window around, you will need a persistent variable in the global scope (which is why the non-closure version worked for you). You can accomplish this by either having a window variable outside of the closure that you set to an NSWindow inside the closure or declaring the window variable without the var keyword inside the closure (which will declare the window variable in the global scope). Here is code demonstrating these two approaches:var globalWindow = null; (function() { ObjC.import('Cocoa'); // Helper function for window creation function windowWithTitle(title) { var window = $.NSWindow.alloc.initWithContentRectStyleMaskBackingDefer( $.NSMakeRect(0, 0, 800, 400), $.NSTitledWindowMask | $.NSClosableWindowMask | $.NSResizableWindowMask | $.NSMiniaturiz