Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm reading es5 these days and find that [[configurable]] attribute in some built-in properties of global object is set to true which means we can delete these properties.

For example:

the join method of Array.prototype object have attributes

{[[Writable]]:true, [[Enumerable]]: false, [[Configurable]]: true}

So we can easily delete the join method for Array like:

delete Array.prototype.join;
alert([1,2,3].join);

The alert will display undefined in my chromium 17,firefox 9 ,ie 10,even ie6;

In Chrome 15 & safari 5.1.1 the [[configurable]] attribute is set to true and delete result is also true but the final result is still function(){[native code]}. Seems like this is a bug and chromium fix it.

I haven't notice that before. In my opinion, delete built-in functions in user's code is dangerous, and will bring out so many bugs when working with others.So why ECMAScript make this decision?

share|improve this question

migrated from stackoverflow.com Nov 23 '11 at 23:54

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers

I would tend to agree with you, but on the other hand I just found a situation where I needed to delete JSON.stringify in certain circumstances because of a bug in Firefox 3.5. I certainly was glad of the ability to monkey-patch builtins there.

share|improve this answer
    
Why don't you just override it? –  demix Nov 24 '11 at 5:51
2  
Because the next thing that happens is JSON2.js gets loaded, which detects the presence of JSON.stringify and injects it if necessary. Apologies, I didn't explain that in my answer. –  N3dst4 Nov 24 '11 at 8:32
    
So you can modify the source code of JSON2.js too, lol –  demix Nov 24 '11 at 9:51
    
It's a bad idea to modify third-party libraries because then you can't upgrade them without copying over all your changes. –  N3dst4 Nov 24 '11 at 10:04
add comment

Configurable is not about deletion.

It's about the ability to replace a read only value.

It's a very powerful tool, and non-configurable values are frustrating if you cannot delete them.

I've had quite a few cases where I needed to fix an obscure bug or inject slightly different functionality (interception, logging). Doing that requires replacing the value.

Example:

Object.defineProperty(Object.prototype, "foo", {
  value: 42,
  configurable: true
});

var o = {};
o.foo = 50; // fails. foo is not writable
delete Object.prototype.foo;
o.foo = 50; // succeeds
/* optionally put Object.prototype.foo back */

The whole idea is that if you can delete properties you have more meta programming control. If you couldn't delete them then you would just get annoyed at the language.

There's no good reason to make properties non deletable other then to annoy people.

share|improve this answer
1  
The [[writable]] attribute control the ability to change the value.In ES5: [[Writable]] Boolean If false, attempts by ECMAScript code to change the property’s [[Value]] attribute using [[Put]] will not succeed. [[Configurable]] Boolean If false, attempts to delete the property, change the property to be an accessor property, or change its attributes (other than [[Value]]) will fail. –  demix Nov 24 '11 at 5:57
    
@demix Yes, that's correct ... –  Raynos Nov 24 '11 at 12:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.