Home arrow JavaScript arrow Page 2 - Adding and Deleting JavaScript Properties and Methods
JAVASCRIPT

Adding and Deleting JavaScript Properties and Methods


Did you know that after you have written the code for a JavaScript object, you could still add properties and methods to the object at run time? Did you know that the user can add properties and methods to a JavaScript object? In this three part series, I show you how to add JavaScript properties and methods at compile and at run time.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
September 25, 2009
TABLE OF CONTENTS:
  1. · Adding and Deleting JavaScript Properties and Methods
  2. · Adding without passing parameters
  3. · Explanation of the Script
  4. · Adding Properties and Methods to Object Type

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Adding and Deleting JavaScript Properties and Methods - Adding without passing parameters
(Page 2 of 4 )

For simplicity, we shall consider the case where the function creating the object does not have parameters.

Example

Let us consider an example where the object type has one property and one method. The name I give to the object is ObjectType. The value of the property of the object type is:

"Yes Type One"

The function to be used as the method of the object type is:

function functionType1()

{

alert('This is function type one');

}

To create a particular object (instance) from the object type you use the new operator. I call the particular object I created from the object type, theObject. So we have

var theObject = new ObjectType();

The object type itself is created using the constructor function. We have for the object type,

function ObjectType()

{

this.propertyType1 = "Yes Type One";

this.methodType1 = functionType1;

}

You can use the following sample to try out what we have described so far in this section.

<html>

<head>

<script type="text/JavaScript">

function functionType1()

{

alert('This is function type one');

}


function ObjectType()

{

this.propertyType1 = "Yes type one";

this.methodType1 = functionType1;

}


var theObject = new ObjectType();

</script>

</head>

<body>

<button type="button" onclick="alert(theObject.propertyType1)">Display property type 1</button>

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

</body>

</html>

This is a small web page. There are two buttons. When you click the first button, the value of the property for the type "object" is displayed. When you click the second button, the method for the type "object" is called.

We shall use this example throughout the series.


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 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials