13

I am trying to delete a object property which is shallow copy of another object. But the problem arises when I try to delete it, it never goes off while original value throws expected output.

var obj = {
 name:"Tom"
};
var newObj = Object.create(obj);
delete newObj.name;//It never works!
console.log(newObj.name);//name is still there

manuell
7,6505 gold badges34 silver badges65 bronze badges
asked Apr 15, 2016 at 10:22
4
  • 3
    Both are completely different objects! Not referenced! Commented Apr 15, 2016 at 10:23
  • 7
    With var newObj = obj; It would work as you are expecting! Commented Apr 15, 2016 at 10:23
  • From docs, The Object.create() method creates a "new object" Commented Apr 15, 2016 at 10:25
  • 1
    Notice that newObj has no name property, so your delete is useless on it. It is newObj's prototype === obj, which has the name property... or not if you deleted it... Commented Apr 15, 2016 at 10:35

2 Answers 2

16

newObj inherits from obj.

You can delete the property by accessing the parent object:

delete Object.getPrototypeOf(newObj).name;

(which changes the parent object)

You can also shadow it, by setting the value to undefined (for example):

newObj.name = undefined;

But you can't remove the property on newObj without deleting it from the parent object as the prototype is looked up the prototype chain until it is found.

answered Apr 15, 2016 at 10:26
Sign up to request clarification or add additional context in comments.

3 Comments

But usage of _proto_ is not recommended. Isn't it?
I'm not sure there's really a notable difference when using it read only but yes it's the recommended practice now.
Thanks Denys for the quick reply, it is working now.
4

Basically Object.create will create an object , set its prototype as per the passed object and it will return it. So if you want to delete any property from the returned object of Object.create, you have to access its prototype.

var obj = { name:"Tom" };
var newObj = Object.create(obj);
delete Object.getPrototypeOf(newObj).name
console.log(newObj.name); //undefined.
answered Apr 15, 2016 at 10:32

5 Comments

In the Chrome console, the OP's code works fine: no need to use prototype.
@manuell I am using chrome and it is not working as intended.
You are right. I was running the delete obj.name; line, too. Without that one, name is still here in newObj (sort of), indeed.
The downvote is not mine. I just upvoted, and the downvote is still here.
@manuell Oh Thats's fine. keep encouraging our peers mate. :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.