Javascript Function reference

Javascript build-in Function object reference.

Using Function:

To create an object: new Function(p1,p2,... ,pn, body)
You uses the new keyword to create an function object.
To use as a function: Function(p1,p2,... ,pn, body)
When the Function object is used as a function the return will be an function object as well.
p1,p2,... ,pn: You can have 0 -> n arguments supplied to the function. Any arguments are normally intended to be used by the body-statements that should be defined for the function.
body: Body-statements that will be executed when the function is invoked.
An example of using the new Function(p1, p2, ... , pn, body):
<script type="text/javascript">
  // Create an object with the new keyword
  var add1= new Function("p1","p2","return p1+p2;")
  document.write(add1(2,6)+"<br>");
  // Using Function as a function
  var add2= Function("p1","p2","p3","return p1+p2+p3;")
  document.write(add2(2,6,90)+"<br>");
</script>

General information about the object member types:

  1. Prototype created methods, properties or constants can only be used on instances of an object or on a primitive datatype.
  2. Constructor created methods, properties or constants can NOT be used on instances of an object or on a primitive datatype.

Prototype Methods:

Syntax: apply(thisArg,argArray)
Make a call to the function specified as if there was a method in thisArg object and use any arguments (given as an array) that follow thisArg in the argument list.

The thisArg object is the value of "this" keyword in the body of the function

If thisArg is null or undefined, the called function is passed the global object as the this value.

A 'TypeError' will be thrown if the method is invoked on an object that is not a function.

Return value: The value returned by the called function.
thisArg: The object that should be the this keyword for the method to be invoked.
argArray: An optional number of arguments, which will be delivered as an array of arguments to the function.
An example of using the apply() :
<script type="text/javascript">
  var orderList ={Milk:1.55, Bread:1.81, Butter:1.23, Cheese:1.67};
  function makeSortedList(obj,minPrice){
    this.selected= new Array();
    for (var item in obj) {
      if (obj[item]>minPrice){
        this.selected.push(item+" ....  "+obj[item]);
      }
    }
    this.selected.sort();
  }
  function List(minPrice){
    makeSortedList.apply(this,[orderList,minPrice]);
  }
  var list = new List(1.5);
  document.write(list.selected+"<br>");
</script>

Prototype Properties:

Syntax: Function.prototype.constructor
This property of the Object.prototype holds the reference back to the Function object.

It is possible to provide the constructor a refererence to another created object.

Constructor (Function) Properties:

Syntax: Function.constructor
The constructor property is a reference to the function that will be invoked to create a Function object.

Inside Function Object:

Syntax: arguments
The arguments object exists only inside a function, and contains all of the arguments passed to the function or method.

It has numbered properties that function as array elements.

Each property contains a argument value.

length: Specifies the number of array elements. It is how many arguments that is passed to the function or method.
callee: A reference to the function that is currently executing.
An example of using the arguments :
<script type="text/javascript">
  function Sum() {
    if (arguments.length != arguments.callee.length){
      document.write("Actual   arguments: "+arguments.length+"<br>");
      document.write("Expected arguments: "+arguments.callee.length+"<br>");
    }
    var sum=0;
    for (var i=0 ; i< arguments.length; i++ ){
      sum+=arguments[i];
    }
    return sum;
  }
  document.write("Sum: "+Sum(1,2,3,4,5)+"<br>");
</script>

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.