Skip to content

Instantly share code, notes, and snippets.

@drewlesueur
Created January 8, 2011 02:55
Show Gist options
  • Save drewlesueur/770489 to your computer and use it in GitHub Desktop.
Save drewlesueur/770489 to your computer and use it in GitHub Desktop.
Explicit scope in JavaScript

Explicit Scope in JavaScript

Wouldn't it be cool if JavaScript had explicit scope?

Every function would have an extra scope variable that represents the scope of that function.

Example

function update_scope(s) {
  s.name = "Bob"
}
function do_something() {
  update_scope(scope)
  alert(name) // alerts "Bob"
}

The same thing can be done with an extra variable

function update_scope(s) {
  s.name = "Bob" // alerts "Bob" 
}
function do_something() {
  var scope = {}
  update_scope(scope)
  alert(scope.name) // alerts "Bob"
}

But explicit scope is makes the syntax nicer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment