Home arrow JavaScript arrow Page 5 - 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 in a Constructor Function
(Page 5 of 5 )

The investigation here will say whether we can delete the property or method in the constructor function. This is the constructor function we have:

function ObjectType()

{

this.propertyType1 = "Yes type one";

this.methodType1 = functionType1;

}

We shall see if we can delete a property or method in a constructor function. We start with the property. Replace the statements in the investigate() function with the following:

answer = delete ObjectType.propertyType1;

alert('Boolean result is ' + answer );

alert(ObjectType.propertyType1); //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.

I tested the code with the five browsers; 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 see if the method can be deleted. Replace the statements in the investigate() function with the following:

answer = delete ObjectType.methodType1;

alert('Boolean result is ' + answer );

ObjectType.methodType1(); //try the property

When I tested the code with the five browsers, 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 properties and methods of the constructor function.

Note: when you are deleting a method, you do not need the attached () brackets. The following expression does not need the () brackets:

delete ObjectType.methodType1

You attach the () brackets when you are calling the method.

This is a summary of what we've discovered:

  • A property or method that belongs to an object can be deleted.

  • 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.

  • You can delete the property or the method we add to the object type when null is assigned to the property or method.

  • You can delete a property or a method of the constructor function.

Wow, we have gotten ourselves into web research; the results clarify what we can do.

Use of Deleting Properties

An area in programming where you will have to delete a property is when you have an object that behaves like an array (e.g. an array itself or a database query result).

Let us take a break here. In the next and last part of the series, we shall see how to add properties and methods at run time. We shall see how the user can indirectly give a property or method to an object. See you in the next part of the series.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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