Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

This is how we write and call a function in Javascript. You can copy paste this directly into the Javascript console:

function APPhelloWorld(Input){
   return "Hello " + Input;
}

var Result = APPhelloWorld("World");
console.log(Result);

You should see something like this:

We can assign Javascript functions into variables like this:

var X = APPhelloWorld;
X("Fred");

Another thing we can do is make what is called a “closure”. This is a Javascript function which is able to bind to and reference an external variable on the fly. Like this:

var myEnclosedVariable = "Fred";
setTimeout(function(){
   console.log("Call " + myEnclosedVariable);
}, 1000);

Under the hood Javascript is creating a closure which effectively is like creating an object on the fly which has the enclosed variable “myEnclosedVariable” as a member. The beauty of closures is they are a much more light weight way of creating an object that one would traditionally get in languages like C++. See how this runs in the console:

Closures are important in Javascript since it allows us to receive callbacks from core APIs in the browser like the setTimeout function.

Lua also has closures as a core part of the language.

See Wikipedia for more information on what a closure is.

  • No labels