Let's say I want to create an Object called 'Vertex'. Usually, in Java I would do this by:
public class Vertex {
// member variables
public data;
private id;
// member methods
public Vertex() { /* default constructor */ }
public getID() { return id; }
}
Now, how would I do that in JavaScript? I want to preserve private vs. public? This is what I have set up so far, but I don't think it is right, and I've never dealt with OOP in JavaScript.
/**
* Vertex constructor.
*/
function Vertex(object) {
data = object;
id = object.getCode(); // each Vertex will have a unique id which will be the city code
};
Vertex.prototype = (function() {
// Private Members here
// Public Members inside return
return {
constructor : Vertex,
getID : function() {
return (id);
}
};
I'm not familiar at all with prototypes, but I'm trying to learn. Is this the right way to do this? If it isn't, I'm basically trying to accomplish what the above Java code does, but by doing it in JavaScript.
Let me know if I need to clarify. You can view the full source code of the JavaScript Objects I'm trying to implement here.
Thanks, Hristo