A.5. Functions and objects

A function is a segment of ECMAScript code that can be called from other parts of a ECMAScript program. Functions are declared using the function keyword, for example:

function square(x){
  return x*x;
}

This function, which returns the square of a number, can be called as follows:

var y = square(4);

If the caller passes a built-in type to a function, it is passed by value. If the caller passes an object, it is passed by reference. A function can be made to return an object as well. The members of the object can be dynamically declared before the object is returned, for example:

function Employee(firstName, lastName, age){
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  return this;
}

Get Definitive VoiceXML™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.