473,513 Members | 4,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2201


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
20276
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()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object...
9
1869
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. Here's the question: I want to be able to change the references (including deleting them). Is there any way to do that besides using pointers rather...
54
4492
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " + salaryMinLabel.value); } I also have an asp.net object:
16
25393
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
6
2651
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 the object and set default blank/null values), and then does all the Db access and number crunching to populate the new object. The factory returns...
4
1853
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 browsers) ? What about the new syntax of creating a object using { 'propName1':'propValue1', 'propName2':'propValue2', 'propName3':{ /* another object...
4
2614
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 since this "value" type is all set to go. Is this because value types are stored on the stack so the memory initialization is already taken care of? ...
15
1878
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 two? (1)
7
1655
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
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.