va***@pisem.net wrote:
function show() {
var s = '' ;
for (var i = 0; i<arguments.length; s += '\n'+arguments[i++]) ;
typeof(window) != 'undefined' ? window.alert(s) : WScript.Echo(s) ;
}
function f(){}
show('delete(f):',delete(f)) ; // false
g = function(){} ;
h = new g() ;
show('h:',h) ; // [object Object]
show('delete(g):',delete(g)) ; // true
show('h.constructor:',h.constructor) ; // function(){}
show('delete(h.constructor):',delete(h.constructor )) ; // true
show('h.constructor:',h.constructor) ; // function(){}
The delete operator yields true even if the object does not have a
property of that name e.g. try
var god = { name: 'Kibo' };
alert(delete god.power);
to see that.
And h does not have a property of the name 'constructor', only its
prototype has e.g.
function checkProperty (object, objectName, propertyName) {
return objectName + '.hasOwnProperty("' + propertyName + '"): ' +
object.hasOwnProperty(propertyName) + '\r\n';
}
function g () {}
var h = new g();
var result = '';
result += checkProperty(h, 'h', 'constructor');
result += checkProperty(g.prototype, 'g.prototype', 'constructor');
result += 'delete o.prototype.constructor: ' + (delete
g.prototype.constructor) + '\r\n';
result += checkProperty(g.prototype, 'g.prototype', 'constructor');
alert(result);
Of course then h.constructor still gives you a function as up in the
protpype chain there still is an object with the property name.
So you have to understand that property access in JavaScript (e.g.
object.propertyName
or
object['propertyName']
) does not only look at the object itself but walks the whole prototype
chain to find a property of that name while the delete operator only
deletes properties of the object itself.
--
Martin Honnen
http://JavaScript.FAQTs.com/