Home arrow JavaScript arrow Page 2 - Nested JavaScript Functions as Objects
JAVASCRIPT

Nested JavaScript Functions as Objects


In this conclusion to a three-part series on nesting JavaScript functions, we see how nested functions can be used as object types. We shall make it simple. We shall consider only the case where there is only one level of nested functions.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
October 22, 2009
TABLE OF CONTENTS:
  1. · Nested JavaScript Functions as Objects
  2. · Constructor Function
  3. · Nested Function as Method
  4. · Position of Nested Function in the Constructor
  5. · My method of coding JavaScript Constructor Function

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Nested JavaScript Functions as Objects - Constructor Function
(Page 2 of 5 )

You create an object type using a constructor function.

Object Type and Object Example

The following code creates an object type and then creates an object from it. A method is then added to the object and called.

<html>

<head>

<script type="text/JavaScript">

function functionType1()

{

alert('This is function type one');

}


function objectFunction()

{

alert('This is object function');

}


function ObjectType()

{

this.propertyType1 = "Yes type one";

this.methodType1 = functionType1;

}


var theObject = new ObjectType();


theObject.method2 = objectFunction;


</script>

</head>

<body>

<button type="button" onclick="theObject.methodType1()">Call Object Type Method</button>

<button type="button" onclick="theObject.method2()">Call added Object Method</button>

</body>

</html>

Explanation of the Example

The first function, functionType1(), will become the method of the object type. The second function, objectFunction(), will be added as a method to the object (instance).

The next function of the code is actually the constructor function. This constructor function creates the object type. The name of the object type is ObjectType. The object type has a property and one method. The method is the first function in the code.

Next, you have the statement that creates the object (instance). The name of the object is theObject. This object inherits all the properties and methods of the object type. Next you have a statement that adds a method to the object (instance).

You have two HTML buttons in the code. The first one calls the object type method, using the object. This method displays the string "This is function type one." The second one calls the object method. This method displays the string "This is object function."


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials