On Fri, 04 Feb 2011 20:10:05 +0100, jemptymethod <[email protected]>
wrote:
I want to delete a couple of object properties, but first save those
properties, then add them back after I'm done processing the object
*without* those properties.
Seems reasonable.
But I can't do the following, when I log the object to the console,
it's apparent the properties didn't get deleted, presumably because to
do so would leave dangling references, i.e. the two vars:
var title = '' + this.args.title;
var mode = '' + this.args.mode;
Here you convert the current values to strings. I don't see why that should
be necessary in order to save the values. And if you want to restore the
original
values, it will only work if the values were strings to begin with ... and
in that
case there is still no need to convert them to string here.
delete this.args['title'];
delete this.args['mode'];
Have you checked the value of the delete expressions. If they don't
evaluate to true,
the deletion didn't work, and you'll have to figure out why (e.g.,
properties are not
configurable or the properties are inherited from a prototype).
The dangling references bit makes no sense. I'm not sure what you think is
happening
behind the scene when you remove a property, but it's just removing a
name-value binding
from the object. Dangling references doesn't happen in a garbage collected
language.
You say that you log the object to console. However, if that's the Firefox
console, it's
a live view of the object. If you remove the properties, log the object,
and then re-add
the properties, then the properties will be there when you expand the
object in the console.
(Or so I have been told, I don't use Firefox much).
One way to work around this use some of Javascript's awful parts
according to Crockford is:
var title = new String(this.args.title);
var mode = new String(this.args.mode);
That's completely unnecessary. You create two new objects here, after
converting the
current property values to strings, but you don't need those objects for
anything.
with (this.args) {
delete title;
delete mode;
}
If this makes any difference, there's something wrong.
Again, check the value of the delete expressions. If this.args doesn't
have a title or
mode property, you'll try to delete the variables declared above (and
fail, luckily).
Between this and my use of "new Number(0)" earlier, I'm breaking all
the rules today, as I presume is bound to happen when you've been
tinkering with Javascript 12 years or so ;)
I'll have to find that new Number(0) message to see what it's about. I'll
keep insisting
that there is never any good reason to create a wrapper object for a
primitive, short
of a bad language implementation.
Finally I settled on this:
var title = '' + this.args.title;
var mode = '' + this.args.mode;
delete this.args['title'];
delete this.args['mode'];
That's identical to the first thing you wrote. Is either of them showing
the wrong code?
Feedback appreciated....George
I'd just do:
var args = this.args;
var title = args.title;
var mode = args.mode;
delete args.title;
delete args.mode;
// do something with this.args
args.title = title;
args.mode = mode;
and if that didn't work, I'd start debugging, but that needs more
information.
If in ES5, you'll also have to take care of restoring getters/setters if
the properties
are not simple value properties, and consider whether the properties might
be non-configurable
(aka ES3's DontDelete).
/L
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]