Javascript built-in Function object

Details about the Function object.

Working with the built-in Function object.

  • The Function object have a prototype property which is a reference to an object that contains common properties for all created function object instances.
  • All created function object instances has a hidden reference to this prototype object, and you can access the common properties directly through the function object instance variable.
  • Unlike all other objects, a instantiated object from the Function object have its own prototype property inherited from the Function object.
  • Javascript has given us the opportunity to create new objects out of function objects. A prototype property is of great importance in this case (look at the next session for this).
  • As we have seen before, we can create a function with a literal notation syntax, which is the normal way to create a function.
  • On the other hand, you can create a function object instance using the built-in "Function" object, and the "new" keyword.
    // Create an function object with the new keyword
    var add1= new Function("p1","p2","return p1+p2;")
    // p1 and p2 is the argument to the new function object
    // the last argument is the body of the new function object
  • As you can see this is a cumbersome way to create a function and is also not recommended.
  • You can add new common properties for all created functions.

Create common properties for all functions.

  • There are two main ways to add new common properties for all function instances:
    1. Add a properties to the prototype property of the Function object.

      Example:
      <script type="text/javascript">
      // first we define a function, func1, using a literal notation syntax
      function func1() {} // a function that contains nothing
      // warning is a property that refer to a new function
        Function.prototype.warning=function() {
          return "This property can be used by all function object instances.";
        }
        document.write("function 1 : "+func1.warning()+"<br>");
        var func2 = new Function(); // a function that contains nothing
        document.write("function 2 : "+func2.warning()+"<br>");
      </script>
    2. Add a property to the prototype property using a created function object instance. A created function object has a property, constructor, that refer back to the constructor name of the object.

      To do it this way we use the property, prototype, on that constructor:

      Example:
      <script type="text/javascript">
        function func1() {} // a function that contains nothing
      // func1.constructor is a reference to the Function constructor in this case
        func1.constructor.prototype.warning=function() {
          return "This property can be used by all object instances.";
        }
        document.write("function 1 : "+func1.warning()+"<br>");
        var func2 = new Function(); // a function that contains nothing
        document.write("function 2 : "+func2.warning()+"<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.