473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How do you remove objects?

mrjohn
32
Hey!

I'm working on a javascript project which will run on a single html page, and I'm using lots of objects. The program will create new instances of these objects during runtime. However, for efficiency, I need a way to get rid of the old ones. Is there any way to do this?
May 13 '09 #1
8 2282
dlite922
1,584 Expert 1GB
by objects you mean elements?

any html element can be cleared with the innerHTML() method. Just set it to an empty string.
May 13 '09 #2
mrjohn
32
Ach, I'm sorry, I meant this sort of object
Expand|Select|Wrap|Line Numbers
  1. function spaceShip(hull, shields, weapons)
  2.             {
  3.                 this.hull = hull;
  4.                 this.shields = shields;
  5.                 this.weapons = weapons;
  6.                 this.crew = 5;
  7.             }
  8. var nightHawk = new spaceShip("Titanium", 16, phasers);
  9.  
I'm not sure what it is called, but I was wondering how to remove instances of such an object.
May 13 '09 #3
mrhoo
428 256MB
If you declare your objects as globals with the var keyword, as in your example, you cannot delete them. The nearest you can get is to set them to undefined.
May 14 '09 #4
mrjohn
32
Oh dear. That is a bit of a bummer. What if instead I put them in an array, like this?
Expand|Select|Wrap|Line Numbers
  1. var fleet = new Array(3);
  2.             fleet[0] = new spaceShip("Iron", 9, phasers);
  3.             fleet[1] = new spaceShip("Titanium", 13, phasers);
  4.             fleet[2] = new spaceShip("Aluminum", 2, phasers);
  5.             fleet.splice(1,1);
  6.  
When splice is run, would the object be removed then? Or would it just be floating around somewhere?
May 14 '09 #5
dmjpro
2,476 2GB
Object removing means .. you want to say your JS Engine to free up the memory(occupied by an instance) when the object no longer needed.
Did you mean that?
May 14 '09 #6
mrjohn
32
Yeah, thats what I meant.
May 14 '09 #7
acoder
16,027 Expert Mod 8TB
Where possible, use the delete operator.
May 14 '09 #8
mrjohn
32
Awesome! Thank you very much.
May 14 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
4
by: u7djo | last post by:
Hi, I'm currently building an application in Access and as part of this need to import forms and modules from another database. Some of the imports will be revisions of existing forms/modules so I...
7
by: Franck Diastein | last post by:
Hi, I'm trying to remove items from a collection this way: foreach(Object myO in ObjectCol){ if(myO != "xxx"){ myO.Remove(); } } But I'm having an error telling me that Collection was...
6
by: Jonathan | last post by:
Hi. I'm having trouble figuring out what I should be doing here. I'm trying to remove an object from a list. The function is: void Alive::FromRoom () { list<Alive>::iterator iter =...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
1
by: ken | last post by:
How to remove specified cookie (via a given name) in cookie jar? I have the following code, but how can I remove a specified cookie in the cookie jar? cj = cookielib.LWPCookieJar() if cj is...
11
by: Richard Maher | last post by:
Hi, I have read many of the copius entries on the subject of IE performance (or the lack thereof) when populating Select Lists. I don't mind the insert performance so much, (I get 100x120byte...
4
by: =?Utf-8?B?emhhbmdscg==?= | last post by:
Hi, Following code does not work. (it will crash.)-- I think the reason is that I cannot modify list (call remove) in foreach. But I don't know what I can do. ...
2
by: news | last post by:
Well here's a sample of the code: class someobject{}; ArrayList alist; alist = new ArrayList(); for(int i = 0; i<10;++i) alist.Add(new someobject());
3
by: Rainer Queck | last post by:
Hello NG, is it possible to use linq to remove certain objects from a generic list? Something more or less like: from obj in MyList where obj.Id 25 delete obj
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.