No, there is nothing similar in Javascript functions, but there is a way to do something very similar, using this
to define all the properties of the object, and not as local variables:
function menu(){ return function(){ this.spam = 3; this.ham = 9; this.eggs = 5; // Do other stuff, you can reference `this` anywhere. console.log(this); // Object {'eggs': 5, 'ham': 9, 'spam': 3} return this; // this is your replacement for locals() }.call({}); } console.log(menu()); // Object {'eggs': 5, 'ham': 9, 'spam': 3}
source share