Does delete walk the prototype chain?

This is a quick post relating to a Rhino bug I've just fallen foul of. This probably isn't very interesting for most folk, but if you've come here from Google then hopefully I can help you out!

Let's start with a definition (from the excellent MDN) of how the delete operator should behave in relation to properties found on the prototype

"You cannot delete a property on an object that it inherits from a prototype (although you can delete it directly on the prototype)."

It even goes on to give an example -

function Foo(){}
Foo.prototype.bar = 42;
var foo = new Foo();
delete foo.bar; // but doesn't do anything
alert(foo.bar); // alerts 42, property inherited
delete Foo.prototype.bar; // deletes property on prototype
alert(foo.bar); // alerts "undefined", property no longer inherited

Unfortunately, after lots of head scratching, it turns out this is not how things work in Rhino (<=1.7R2). The above code, modified to print rather than alert, prints "undefined", "undefined". Rhino is walking the prototype chain to find the property and then deleting it from the prototype. This is because of the following bug Bug 510504 - delete foo.prop should not walk the property chain to find prop.

The bug was fixed and released in 1.7R3 which can be found at a slightly different location from 1.7R2 -

<dependency>
	<groupId>org.mozilla</groupId>
	<artifactId>rhino</artifactId>
	<version>1.7R3</version>
</dependency>

Upgrading to that version prints "42", "undefined" - success!

MORE BY CHRIS

Efficient SVG layout with d3

blog comments powered by Disqus