Home arrow JavaScript arrow Page 4 - Deleting JavaScript Properties and Methods
JAVASCRIPT

Deleting JavaScript Properties and Methods


Welcome to the second part of a three-part series on adding and deleting JavaScript properties and methods. In this part of the series, we learn how to delete properties and methods. Before we get into that, let us see how to add properties and methods to objects (instances).

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
October 02, 2009
TABLE OF CONTENTS:
  1. · Deleting JavaScript Properties and Methods
  2. · Deleting Properties and Methods
  3. · Properties and Methods Belonging Only to a Particular Object
  4. · Object Type Properties and Methods with Particular Object Assignment
  5. · Object Type Properties and Methods in a Constructor Function

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Deleting JavaScript Properties and Methods - Object Type Properties and Methods with Particular Object Assignment
(Page 4 of 5 )

Here, we consider the case where you add a property or a method to an object type, but assign different values for the property or method of the particular objects. We saw this towards the end of the previous part of the series. We shall use the same code as above and try to remove what we added (the property and method).

Let us try to delete the property of the object (instance) first before we try to remove the method of the object (instance). Replace the statements in the investigate() function with the following:

answer = delete theObject.propertyType2;

alert('Boolean result is ' + answer );

alert(theObject.propertyType2); //try the property

When I tested the code, the Boolean result was true, meaning that the property was deleted. After deleting, the value of the property displayed was null. The program gave us the value we assigned to the property for the object type.

Let us now try to delete the method. Replace the statements in the investigate() function with the following:

answer = delete theObject.methodType2;

alert('Boolean result is ' + answer );

theObject.methodType2(); //try the method

I tested the code with the five browsers. The Boolean result was true. This means the method was deleted. When the deleted method was tried, nothing happened, confirming the fact that it was deleted.

So, when you add any property or method to the object type, you can delete the property or method with the assigned value of any of the derived objects (instances).

Let us now see if you can delete the property or the method we add to the object type when null is assigned to the property or method. We start with the property. Replace the statements in the investigate() function with the following:

answer = delete ObjectType.propertyType2;

alert('Boolean result is ' + answer );

alert(ObjectType.propertyType2); //try the property

Note that we have now used the ObjectType class and not the theObject derived object. Our aim is to delete the property from the object type and not from a derived object.

When I tested the code, the Boolean result was true, meaning that the property was deleted. After deleting, the value of the property displayed was undefined. This confirmed that the property was deleted.

Let us now see if you can delete the method we add to the object type when null is assigned to the method. Replace the statements in the investigate() function with the following:

answer = delete ObjectType.methodType2;

alert('Boolean result is ' + answer );

ObjectType.methodType2(); //try the method

Note that we have now used the ObjectType class and not the theObject derived object. Our aim is to delete the method from the object type and not from a derived object.

When I tested the code, the Boolean result was true, meaning that the method was deleted. After deleting, the method was tried and nothing happened. This confirmed that the method was deleted.

So, you can delete the property or the method we add to the object type when null is assigned to the property or method.


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