Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Last active May 25, 2022 02:00
Show Gist options
  • Save pesterhazy/2bd06fc6e12686e0705763988099b3c5 to your computer and use it in GitHub Desktop.
Save pesterhazy/2bd06fc6e12686e0705763988099b3c5 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