name variable gets changed to name2 in JavaScript

98 views
Skip to first unread message

Johnny Oshika

unread,
Apr 30, 2021, 12:28:55 PM4/30/21
to Blockly
I built an integration with Blockly that depends on the variables that are created in the Workspace. So for example, if a user creates a variable `foo`, my integration looks for the value of the variable `foo` after executing the Blockly code in JavaScript. I came across a strange unexpected behaviour with the variable name `name`. If a user uses `name` as the variable name, the JavaScript code generator turns that into `name2`. Here's the XML that's generated:

  <variables>
    <variable id="t]mo%u-X${06yZGs#?pA">name</variable>
  </variables>
  <block type="variables_set" id="MDHJt|uyV_=Z.Xi%9Nkm" x="230" y="218">
    <field name="VAR" id="t]mo%u-X${06yZGs#?pA">name</field>
    <value name="VALUE">
      <block type="text" id="LQ?CEN;K/JX$9`zIHS:I">
        <field name="TEXT">Bob</field>
      </block>
    </value>
  </block>
</xml>

Notice the variable name `name` above. But the generated JavaScript code is:

```
var name2;
name2 = 'Bob';
```

This seems to only happen in JavaScript and doesn't impact the other languages. Do you know why this is happening? Is there a list of variable names that are susceptible to this same behaviour?

Thanks


Coda Highland

unread,
Apr 30, 2021, 12:39:17 PM4/30/21
to blo...@googlegroups.com
On Fri, Apr 30, 2021 at 11:28 AM Johnny Oshika <joh...@examind.io> wrote:
This seems to only happen in JavaScript and doesn't impact the other languages. Do you know why this is happening? Is there a list of variable names that are susceptible to this same behaviour?

This is an intentional feature to prevent collisions with certain reserved names. Each language has a list of reserved names that the variableDB avoids using by adding a number to the end.

It's nominally a private variable, but each generator has a RESERVED_WORDS_ property you can consult and an addReservedWords() method you can use to add more.

/s/ Adam

Johnny Oshika

unread,
Apr 30, 2021, 2:30:54 PM4/30/21
to Blockly
Thank you, that makes sense.

Neil Fraser

unread,
Apr 30, 2021, 3:48:33 PM4/30/21
to blo...@googlegroups.com
This is intended behaviour.  'name' and 'length' are properties of the browser's window object, which is also the global object.  So if the generated code is to be executed in the environment of a browser, we don't want to clober those two properties by masking them with global variables.  Thus they (and many other variable names) are renamed.




--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/beebc8ad-bc11-4ef0-94ce-fd87c34a083fn%40googlegroups.com.


--

Beka Westberg

unread,
Apr 30, 2021, 4:10:32 PM4/30/21
to blo...@googlegroups.com
I think the others did a good job of explaining why this happens =) But if your integration still needs to know the generated variable names I recommend checking out this forum thread which needed to do a similar thing,

Best wishes,
Beka

Johnny Oshika

unread,
Apr 30, 2021, 6:19:08 PM4/30/21
to Blockly
This all makes sense. Thank you very much everyone!

Beka, thank you for the link to the forum thread. That was going to be my next question. 🙂
Reply all
Reply to author
Forward
0 new messages