Binding to a different variable name in js is how the cljs compiler 
emulates symbol-to-value binding using javascript vars.

It is equivalent to this es6 javascript using let:

{let a = 123, func = function() {return a;}; { let a = 456; }}

On Wednesday, March 13, 2019 at 12:26:15 AM UTC-5, [email protected] wrote:
>
> When I run the following CLJS code:
>
> (let [a 123
>       func (fn [] a)
>       a 456]
>   (func))
>
> the block returns 123, which I'm absolutely fine with and have gotten used 
> to since starting programming Clojure(Script).
>
> However, in JS if I run the following almost but not quite equivalent code:
>
> var a = 123,
>     func = function() {return a;}, 
>     a = 456;
>
> I of course get 456, which is also expected.
>
> So it appears, at least on the surface, that when func is created that it 
> evaluates the symbol a at the time of creation, such that func actually 
> becomes (fn [] 123). However, looking at the compiled JS code, what is 
> actually happening is:
>
> var a_32455 = (123);
> var func_32456 = ((function (a_32455) {
>    return (function () {
>       return a_32455;
>    });}) (a_32455) );
> var a_32457__$1 = (456);
> func_32456.call(null);
>
>
> which is fascinating, because it isn't actually what I intuitively 
> expected was happening. Instead, it appears as if each instance of the 
> symbol a as a binding in the let binding vector is converted to a different 
> variable in the compiled JS code. So the first a becomes a_32455, and the 
> second becomes a_32457__$1.
>
> This isn't actually causing me a problem at all, but I'm really interested 
> to know what exactly is going on, if anyone can explain it. I also wonder 
> if it is something which may trip newcomers from JavaScript and if an 
> explanation should be provided somewhere?
>
> Thanks, 
> Ali
>

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/clojurescript.

Reply via email to