Jump to content

Removing object property?


jekillen

Recommended Posts

I have this code:

 

if(active[active.id].group.propertyIsEnumerable(selection)) { alert(selection) active[active.id].group[selection] = null; }

 

That is looking for a particular property (selection) and is supposed to

remove it from the object. In this case setting it to null, I thought, would

remove the specific property so it would no longer be defined and

therefore not be enumerable.

 

But it does not seem to, as demonstrated by

 

var test

for(var x in active[active.id].group)

{

test += "n"+x; // new line in DOM browsers

}

alert(test) // The selection property continues to be listed.

 

Am I doing something wrong, or is there another way to remove the

property altogether?

 

I have nit picked through the exact string construction for any differences and have not found any

and, in fact the alert in the first code snip does show up, so I know that selection is a valid property.

 

Thanks for time and attention

Link to comment
Share on other sites

In this case setting it to null, I thought, wouldremove the specific property

Null is a value like any other value, that's like saying that setting the property to true or false would remove it. It doesn't remove anything, it just sets the value to null. Use the delete operator.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
  • Like 1
Link to comment
Share on other sites

Thankyou Moderator:

 

I thought that null would remove the property because null is used to remove a

select element option. So, it appears that it is not just a value here, at least.

 

I am using O'Reilly's Javascript: the Definitive Guide, 6th ed for guidance where possible

but it does not mention delete.

Link to comment
Share on other sites

I thought that null would remove the property because null is used to remove aselect element option.

I'm not familiar with that, do you have an example? Select elements have a remove method for removing options:https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/removeIn general, null isn't used for much other than a special value to indicate that there is no value. It's different from the value undefined, because undefined means that the value hasn't even been defined, but null means that the value is defined but specifically has no value.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...