By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,110 Members | 1,645 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,110 IT Pros & Developers. It's quick & easy.

Deleting an object constructor does nothing, but returns true

P: n/a
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(){}

Aug 8 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


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/
Aug 8 '05 #2

P: n/a
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('constructo r'),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.p rototype.constructor):',
delete(h.constructor.prototype.constructor.prototy pe.constructor)) ;

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

--
Vic

Aug 9 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.