Internally-used function in Blockly

19 Views Asked by At

If you go to the Blockly demo, take out the random colour block, and then click the JavaScript tab, you will see the following code generated:

function colourRandom() {
  var num = Math.floor(Math.random() * Math.pow(2, 24));
  return '#' + ('00000' + num.toString(16)).substr(-6);
}


colourRandom();

It creates its own function, colourRandom. Now if you go back to the Blocks tab and create a variable named colourRandom, the same name as the function, drag out a set colourRandom to block, and go back to the Javascript tab, the colourRandom function changes its name to colourRandom2.

var colourRandom;

function colourRandom2() {
  var num = Math.floor(Math.random() * Math.pow(2, 24));
  return '#' + ('00000' + num.toString(16)).substr(-6);
}


colourRandom2();

colourRandom = 0;

I would like to create my own function like this. How do you this? Here's what looks to be the code that creates the random colour block. (line 37)

Picture of Blocks

0

There are 0 best solutions below