1

I had an object of the form obj[prop] and in the case where prop === 'name', using the syntax obj[prop] = 'xyz' was failing to set the property. Insteadm I had to delete obj[prop] first and then set it.

The code is far too much to put down here so this is a general question. Does anybody have an idea what might cause this problem? This was ONLY happening when prop === 'name'. Every other property I tried had no issues.

4
  • if u are using a obj, not an array type, you can make use of the function obj,hasOwnProperty(propertyname) this will return a true or false Commented Feb 18, 2016 at 8:41
  • What is obj? Where is it created? is it possible that name is defined with a getter/setter? To check this, place a breakpoint at the obj[prop] = 'xyz'; line, and trace into the code; it may take you inside a setter for this property. Commented Feb 18, 2016 at 9:02
  • I couldnt find any changes to the definition. Im going to guess that using JSON parse is causing some funkiness somewhere. Just going to use delete before setting the property. Commented Feb 18, 2016 at 9:57
  • Thanks for the help all. Commented Feb 18, 2016 at 9:57

2 Answers 2

2

I know of only one such circumstance: name being defined as not writable. It also has to be configurable, otherwise you wouldn't have been able to delete it.

var prince = {}
Object.defineProperty(prince, 'name', {
  writable: false,
  configurable: true,
  value: "Prince"
})
prince.name = 'The Artist Formerly Known as Prince';
console.log(prince.name);
// => Prince
Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't it also be possible that name is defined with a getter/setter?
1

You should check if the property "name" is writable :

Object.getOwnPropertyDescriptor(obj, 'name');
> Object {value: "xyz", writable: true, enumerable: true, configurable: true}

More information about property descriptors here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor#Description

4 Comments

Thankyou I was just looking for the dump for the property!
Everything is the basic setup. I cannot recreate the issue either in jsFiddle. Might just have to bite the bullet and ignore the error :(
This is strange... You can also look into the "freeze/isFrozen", "seal/isSealed" and "preventExtension/isExtensible" methods of the Object constructor. They can make an object immutable and things like that. I don't know these methods that well, so here is the documentation: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
If the object was frozen or sealed, he wouldn't be able to delete the property. preventExtensions would still let him change the property.

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.