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.
- 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:
Add a properties to the prototype property of the Function object.
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:
- Add a properties to the prototype property of the Function object.
© 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.