472,968 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,968 software developers and data experts.

Deleting an object constructor does nothing, but returns true

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
2 2170


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
6
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
4
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
4
by: Ben R. | last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing with value versus object types. If I'm correct, when I do: dim x as integer There's no need for "new" to be called...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
7
by: xllx.relient.xllx | last post by:
Q1: What happens when you apply parenthesis to the class type name? Is the constructor for the class called and returns an object?, or what? in main...: MyClass(); // end
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.