Javascript Object Oriented Design - Javascript Functions

Javascript functions are actually objects.

Javascript function has an internal property named [[Call]].

The internal properties are indicated by double-square-bracket notation.

Internal properties are not accessible via code. It is used to define the behavior of code.

Javascript defines several internal properties for objects in JavaScript.

The [[Call]] property is unique to functions and indicates that the object can be executed.

The typeof operator returns "function" for any object with a [[Call]] property.

declarations vs. expressions

There are two literal forms of functions.

The function declaration begins with the function keyword and continues with the name of the function.

The contents of the function are enclosed in braces.

The following code uses the function declaration literal syntax to create a function to add two number together.

function add(num1, num2) { 
   return num1 + num2; 

The code above shows how to use the function declaration.

A function expression doesn't require a name after function.

These functions are anonymous because the function object itself has no name.

Function expressions are typically referenced via a variable or property.

The following code creates a function expression to add two numbers together.

var add = function(num1, num2) { 
   return num1 + num2; 

The code above assigns a function value to the variable add.

Assignment expressions end with a semicolon.

Function declarations are hoisted to the top of the context.

With function declarations we can define a function after it is used in code without generating an error.

For example:

var result = add(5, 5); 

function add(num1, num2) { 
   return num1 + num2; 

JavaScript engine hoists the function declaration to the top and actually executes the code as if it were written like this:

function add(num1, num2) { 
   return num1 + num2; 

var result = add(5, 5); 


As long as we define functions before using them, we can use either function declarations or function expressions.

functions as values

JavaScript functions can be used as any other objects.

We can assign functions to variables, add them to objects, pass them to other functions as arguments, and return them from functions.

We can use a function anywhere we would use a reference value.

Consider the following example:

function myMethod() { 
myMethod();        // outputs "Hi!" 

var myMethod2 = myMethod; 

myMethod2();       // outputs "Hi!" 

In this code, there is a function declaration for myMethod.

A variable named myMethod2 is then created and assigned the value of myMethod.

Both myMethod and myMethod2 are pointing to the same function.

Javascript functions are objects.

We can pass a function into another function as an argument.


The following code shows how to pass a function as data to another function.

The sort() method on JavaScript arrays accepts a comparison function as an optional parameter.

The comparison function is called when two values are compared.

If the first value is smaller than the second, the comparison function returns a nega tive number.

If the first value is larger than the second, the function returns a positive number.

If the two values are equal, the function should return zero.

By default, Array.sort() converts every item in an array to a string and then does a comparison.

To sort an array of numbers, such as:

var numbers = [ 11, 15, 18, 41, 71, 110, 21, 16 ]; 
// ww w.j  ava2 s .c o m
var s = function(first, second) { 
   return first - second; 




The code above generates the following result.

In the code above, the comparison function s is passed into sort().