0

As i understood delete operator delete the corresponding reference, not object which located by this reference. Let we have a simple object and applied delete operator to one of this property, for instance

var o= {prop: "property", test: "sometext"};
delete o.prop;

Did we erased the "property" string itself or we erased reference to this only and "property" string will be erased by garbage collector as no-reference.

3 Answers 3

2

Technically, yes, you deleted the property and the value itself, because it's not referenced any more, will be separately garbaged.

You could check that by having an object referenced by two properties : one of them wouldn't be touched if you delete the other property.

Sign up to request clarification or add additional context in comments.

Comments

1

This code below will help you get a visual result using a test object and console.log, so you can see what happens to the object property.

Before the delete, when we console.log(); obj.prop1, it will say 'yo' in your console. after we deleted, this will become undefined.

You can use delete to clean up unused properties when you're either done with them or don't need them anymore, it's not a big speed increase but might be helpful when your website is viewed via a cheap machine.

<script type="text/javascript">

    var obj = {

        prop1: 'yo',
        prop2: 'yo'

    };

    console.log(obj.prop1);
    delete obj.prop1;
    console.log(obj.prop1);

    console.log(obj); //Will return only prop 2, prop1 is removed from the object.

</script>

PS:

After logging the entire object the property is gone entirely.

Comments

1

According to its repsective MDN article, the delete keyword in JavaScript "removes a property from an object." That said, if o.prop referenced an object (instead of a string), the reference to that object (that is, o.prop) would be deleted, but the object in question may remain in memory if there is some other variable/property pointing to it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.