Home arrow JavaScript arrow Page 3 - 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 - Properties and Methods Belonging Only to a Particular Object
(Page 3 of 5 )

In this section we shall see how to delete a property or a method that belongs to an object (instance). Add the following code at the bottom of the above script.

function investigate()

{

answer = delete theObject.property1;

alert('Boolean result is ' + answer );

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

}

Remove the HTML buttons and replace them with the following:

<button type="button" onclick="investigate()">Investigate</button>

You should have the following code:

<html>

<head>

<script type="text/JavaScript">

function functionType1()

{

alert('This is function type one');

}


function functionType2()

{

alert('This is function type two');

}


function function1()

{

alert('This is function one');

}


function ObjectType()

{

this.propertyType1 = "Yes type one";

this.methodType1 = functionType1;

}


ObjectType.prototype.propertyType2 = null;

ObjectType.prototype.methodType2 = null;


var theObject = new ObjectType();


theObject.propertyType2 = "Yes type two";

theObject.methodType2 = functionType2;


theObject.property1 = "Yes one";

theObject.method1 = function1;


function investigate()

{

answer = delete theObject.property1;

alert('Boolean result is ' + answer );

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

}


</script>

</head>

<body>

<button type="button" onclick="investigate()">Investigate</button>

</body>

</html>

For all the browsers, the operation returned true and displayed the value "undefined" for the property after deleting. So the first alert box displayed “Boolean result is true.” The second one displayed “undefined.” Go through the code to understand what happened if you have not already done so.

Let us check to see if the method can be deleted. Replace the statements in the investigate() function with the following:

answer = delete theObject.method1;

alert('Boolean result is ' + answer );

theObject.method1(); //try the method

The alert box has to display the returned Boolean value. If the method is still there after the delete process, we should see an alert box displaying “This is function one.”

I tested the code with all five browsers; the Boolean return value was true. When the method was called after the delete operation, nothing happened. This definitely means the method was deleted.

So a property or method that belongs to an object can be deleted.


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