0

I am trying to create a canvas game using the proccessing.js library.

I use an array to hold all my objects.

class blah{
    ...
    void delete(){
       // this.remove ???
    }
}

blah myArray = [];
myArray.push(new blah());

Is there a way I can create a delete function inside that class so that when the element is deleted it is removed from the array? I have thought of a workaround like passing as an argument the element's position in the array. Isn't there a direct way to delete it without passing any argument?

The workaround I told above:

void delete(int i){
  myArray.splice(i,1);
}
XCS
  • 27,244
  • 26
  • 101
  • 151
  • 1
    I'm confused: JavaScript doesn't currently support classes or static typing... Am I missing something? – Xophmeister Sep 28 '12 at 21:48
  • 1
    Your code snippet is confusing. In JavaScript you don't declare type of variable and you don't type the return value. Are you sure this isn't ActionScript or Java ? – HoLyVieR Sep 28 '12 at 21:48
  • 1
    I am using processing.js as I mentioned in the tags and at the begging of my question. This library allows C++-like declarations. http://processingjs.org/learning/ – XCS Sep 28 '12 at 21:49
  • I really enjoy how certain group of people try everything they have to follow the basic Java syntax and the inheritance but at the end you will still end up at prototypal inheritance, which is just not the usual class inheritance what Java, C# or C++ has... Dude, I recommend for you to learn th JavaScript good... At the end, it is going to be JavaScript. =) – benqus Sep 28 '12 at 21:49
  • @Cristy Fair enough: I retract my confusion! – Xophmeister Sep 28 '12 at 21:50
  • 1
    @Xophmeister just another library which ruins the basic idea of JavaScript. Flexibility, create your own 'stuff'... – benqus Sep 28 '12 at 21:51
  • 2
    @benqus Not really. This is a great library, and I am not a big fan of libraries. But as C++ was the first language I have learnt this really helps in reducing the code size. You can create a fully-functional canvas game in 100 lines, which would take like 300 in pure javascript. And the good part is that I don't have to learn any new library because with this one you can still write in pure javascript, but you can still use the advantages of more advanced OOP languages like C++. – XCS Sep 28 '12 at 21:54
  • n.b., Also `delete` is a reserved word in JS. It is implemented in, for example, node, where it removes a property from an object. – Xophmeister Sep 28 '12 at 21:56
  • Can't you people notice the processing.js tag? This isn't pure JS. – CMircea Sep 28 '12 at 21:56
  • @Xophmeister, that was only an example... It's not what I will use for that method name. – XCS Sep 28 '12 at 21:58
  • You should rename the question, I think processing.js should be in the headline. – Janus Troelsen Sep 28 '12 at 21:58
  • @Cristy Sorry pal, but I must disagree with you on 'advanced OOP' languages... If you just dive just a little deeper, and not just scratch to surface of the language, you will find, that the 'advanced OOP' you have mentioned is just unnecessary in Javascript. Just because JS doesn't allow class inheritance does not mean that JS is not 'advanced'. It's Object based, and if you digg a little deeper you will find that it is 'only' Object based. It's just different. Don't be lazy to learn something new as it is. ;) – benqus Sep 28 '12 at 21:59
  • @benqus Comments really aren't the right place to host such debates. Try http://chat.stackoverflow.com/ or any of many more appropriate places. – Jonathan Lonowski Sep 28 '12 at 22:03
  • @JanusTroelsen Well, won't there be the same problem if I was using pure javascript? A "pure-javascript" answer is wellcomed. Like if instead of class I was using javascript objects. How could I create a method so that if it is called the element is removed from the array too... – XCS Sep 28 '12 at 22:03
  • @JanusTroelsen: Right you are. Cristy: Have you heard of closures? Static scope? What you are looking for is a "destructor" function (if I'm not mistaken) which removes the instance from an array, which can be pushed into the static scope at initialization, Array.prototype.push method returns the index, which you store on the instance, and when you 'remove' the object, you just remove it from the array. At this point, I suggest you use a collection instead of the array. – benqus Sep 28 '12 at 22:08
  • @benqus Of course I know what a destructor is, but it's my personal oppinion that working with Arrays that store all elements in consecutive positions is more efficient than storing elements at some random keys in a collection. – XCS Sep 28 '12 at 22:13
  • Here you go: http://jsfiddle.net/benqus/6mFLN/ You can be consistent in a collection. In JavaScript, collection is actually an object. Nothing stops you to use the object's keys' (property names) as indicies. These indicies will be object properties and you assign the instance to the actual index in construction, therefore you can delete them later... (Check console after page loaded...) – benqus Sep 28 '12 at 22:31

1 Answers1

1

You should be able to find the instance with indexOf:

void delete() {
    int index = myArray.indexOf(this);

    if (index > -1) {
        myArray.splice(index, 1);
    }
}
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199