Deleting an object constructor does nothing, but returns true

V

vakap

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(){}
 
M

Martin Honnen

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.
 
V

vak

Thanks, Martin.

Most valuable is your notice on that delete returns true on
non-existent property.

Surprisingly, one can totally delete all the constructors on the
prototype chain:

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() ;
// [object Object]
show('h:',h) ;

// true
show('delete(g):',delete(g)) ;

// function(){}
show('h.constructor:',h.hasOwnProperty('constructor'),h.constructor) ;

// true
show('delete(h.constructor.prototype.constructor):',
delete(h.constructor.prototype.constructor)) ;

// function Object(){[native code]}
show('h.constructor:',h.constructor) ;

// true
show('delete(h.constructor.prototype.constructor.prototype.constructor):',
delete(h.constructor.prototype.constructor.prototype.constructor)) ;

// undefined
show('h.constructor:',h.constructor) ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top