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