12

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?

  • Multiple answers laud the ability to customize built-in functionality by deleting properties but this approach is only necessary because the functionality is hardwired in global variables instead of using DI. It seems customizing by deleting properties is a hack around a fundamentally bad design. For example, if you need to be able to change the JSON parser, you could write code that takes a JSON parser as input. – Reinstate Monica Oct 17 '17 at 15:32
2

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.

|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
1

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.

|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
0

..delete built-in functions in user's code is dangerous

Quite the contrary. Allowing customization is good because it allows website authors to have more flexibility.

If the website author needs to load 3rd party code within the same JS VM and wish to use the inbuilt JS parser to do so, he can always secure properties by setting them to nonconfigurable before loading the 3rd party code.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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