2. JS functions are first class

Created Sunday 01 December 2013

// A JS function is an object with executable code associated with it.
// This is the usual definition
function func (x) {

alert (x);
}

func ("blah");

// This is a definition by creating an anonymous function object, and assign it to variable func
var func = function (x) {

alert (x);
};

func ("blah2");

// Using the function constructor
var func = new Function ("x", "alert (x);");

func ("blah3");
// This method makes possible to construct arbitrary functions at run time

// It's possible to set or add properties to functions
function sayHi (x) {

alert ("Hi, " + x + "!");
}

sayHi.text = "Hello World!";

sayHi ["text2"] = "Hello World... again.";

alert (sayHi ["text"]); // out: "Hello World!"
alert (sayHi.text2); // out: "Hello World... again."

// Functions can be:
// * assigned to variables;
// * passed as arguments to other functions;
// * returned as the values of other functions;
// * stored as properties of objects;
// * stored as elements of arrays, and so on

// Assign an anonymous function to a variable
var greet = function (x) {

alert ("Hello, " + x);
};

greet ("MSDN readers");

// Passing a function as an argument to another
function square (x) {

return x * x;
}

function operateOn (num, func) {

return func (num);
}

alert (operateOn (16, square));

// Functions as return values
function makeIncrementer () {

return function (x) {return x + 1};
}

var inc = makeIncrementer ();

alert (inc (7)); // 8

// Functions stored as array elements
var arr = [];

arr [0] = function (x) {return x * x;};
arr [1] = arr [0] (2);
arr [2] = arr [0] (arr [1]);
arr [3] = arr [0] (arr [2]);

alert (arr [3]); //256

// Functions as object properties
var obj = {"toString" : function () {return "This is an object.";}};

alert (obj); // Calls obj.toString ();

// Therefore adding method to an object is like choosing a name and assigning a function to that name
var myDog = {

"name" : "Spot",
"bark" : function () {alert ("Woof!");},
"displayFullName" : function () {
alert (this.name + " The Alpha Dog");
},
"chaseMrPostman" : function () {
// implementation beyond the scope of the article beign learned
}
};

myDog.displayFullName ();
myDog.bark ();

// "this" changes as the object changes
function displayQuote () {

alert (this.memorableQuote);
}

var williamShakespeare = {

"memorableQuote" : "It is a wise father that knows his own child.",
"sayIt" : displayQuote
};

var markTwain = {

"memorableQuote" : "Golf is a good walk spoiled.",
"sayIt" : displayQuote
};

var oscarWilde = {

"memorableQuote" : "True friends stab you in the front."
};

williamShakespeare.sayIt ();
markTwain.sayIt ();
displayQuote.call (oscarWilde); // Each function object has a method call () so the function can be called as a method of the object passed to call () as an argument. Whichever object passed into call as its first argument will become the value of "this" in the function invocation. This is a useful technique for calling the base class constructor

// !!! Calling functions that contain "this" without an owning object will result in trampling over the global namespace — "this" will refer to the Global object, and that can wreak havoc in the application. The following example is not recommended
alert ("NaN is NaN: " + isNaN (NaN));

function x () {

this.isNaN = function () {
return "not anymore!";
};
}
// trampling the Global object
x ();

alert ("NaN is NaN: " + isNaN (NaN));



Backlinks: