Yeah – the syntax is similar to C, C++, C#, Java. That part is fairly easy to understand. Where it is not similar, is the way the syntax is processed. It's actually a lot closer to…LISP.

If you are going to 'get' Javascript, you're going to have to get a grip on anonymous functions, delegates, closure, and prototypes.

They take a little getting used to – but they are the essence of what makes JScript so versatile. Hence popular.

JScript has the keyword function which allows you to define functions as follows:

function MyFunc(myArg){return "You gave: "+ myArg;}

Easy right. Same as any other language, just about.

But JScript allows a method to be anonymous as well, and stored in a var of your choosing:

var anyVarName = function(myArg){return "You gave: "+ myArg;}

The first time you created a function called MyFunc, that you can get back to anytime you want just by

var result = myFunc("apple");

and in the second you created a function that had no name – and therefore no way to refer back to it (and therefore would get lost in the bowels of the computer memory) if you didn't have a way to hold on to it. Which is why one assigns it to a variable (anyVarName).

The side effect of this is that one can use the variable as if it were a function:

anyVarName(“pear”);

It works…but what's the point?

The point is that in JScript functions are first class member variables, and you do not need to set up delegates to pass variables around. It's basically transparent to pass around either variables of value, or variables that are methods.

You can easily define:

var firstMethod = function(a){return "sliced " + a); 
var secondMethod = function(a){return "You asked for a " + a); 

and see that:

 
  var result = secondMethod(firstMethod("banana"));
  //result = "You asked for a sliced banana");
  • /home/skysigal/public_html/data/pages/it/ad/jscript/core.txt
  • Last modified: 2023/11/04 03:26
  • by 127.0.0.1