We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I've been reading about function expressions in Javascript. Like var greet = function greet(name, timeOfDay) {
return “Good “ + timeOfDay + “ “ + name + “!”;
}
Is it possible to do something like this in Processing? The way I am inclined to try this is setting the function to float and creating a variable to reference it in the body of my program. Just curious if anyone dealt with this much.
Answers
Processing got many flavors. And Java Mode is the main 1.
In Java we've gotta declare which type a function returns. Plus all the types of its parameters.
Thanks for reminding me one can switch modes! I was curious if you could have an expression like
float x = String greet(String name, String timeOfDay) { return "Good " + timeOfDay + " " + name + "!"; }
In Java, functions (also classes/interfaces) got their own namespace.
It means we can have a variable called greet at the same time we have a function called greet() too!
As you may be guessing by now, that's gonna fail miserably in JS! :-SS
JS stores references for anything in variables and properties, including functions. :-B
A tricky sample so you can understand that in JS functions share the same namespace as everything else: :-\" Hit F12 to open your browser's console and paste those 3 statements below: >-)
You're gonna get:
Uncaught TypeError: greet is not a function(…)
.It is instead the number
10
! :PIn contrast just check it out this confusing sketch using String everywhere: :ar!
It's mind-twisting but Java is smart enough to distinguish which "String"s are the variable, the parameter, the function and the class datatype! \m/
That is cool! Thanks for the info. Getting a better sense for JS and java differences