Skip to content

Instantly share code, notes, and snippets.

@webappzero
Forked from pesterhazy/cljs-let-shadowing.md
Created October 26, 2017 21:26
Show Gist options
  • Select an option

  • Save webappzero/5bf2b302c358f6f14acef00bf91451cc to your computer and use it in GitHub Desktop.

Select an option

Save webappzero/5bf2b302c358f6f14acef00bf91451cc to your computer and use it in GitHub Desktop.
Clojurescript let global variable shadowing

Consider this ClojureScript code:

(defn foo []
  (let [location 12345] (prn js/location.hash)))

This won't work. js/ isn't a real namespace. This code will throw an exception at runtime.

The reason is that the code expands to this JS snippet:

cljs.user.foo = (function cljs$user$foo(){
  var location = (12345);
  return cljs.core.prn.call(null,location.hash);
});

As you can see, the location let binding is shadowing the js/location var.

The moral of the story is: be careful when using global vars like js/window and js/location. Don't use them in the context of let bindings or function arguments with common names like window or location.

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