rxmnnxfpvg
rxmnnxfpvg

Reputation: 30983

Javascript object design

In Javascript I would like to create two classes: A node, and a node list. A node contains some trivial properties; a node list contains pointers to a node, and multiple node lists can contain the same nodes. Would the following be correct (simplistic) design?

function Node(name, x, y) {
    this.name = name;
    this.x = x;
    this.y = y;
}

Node.prototype.setX = function(x) {
    this.x = x;
};

Node.prototype.setY = function(y) {
    this.y = y;
};

function Nodelist() {
    this.list = [];
}

Nodelist.prototype.addNode = function(node) {
    this.list.push(node);
};

var a = new Node('stack', 0, 0);
var b = new Node('overflow', 0, 0);
var l = new Nodelist();
var m = new Nodelist();
l.addNode(a);
l.addNode(b);
m.addNode(a);

Do I even need these .prototype.set functions? Playing around in the console it seems I can just do a node.x = 10. Thanks.

Upvotes: 0

Views: 150

Answers (4)

z33m
z33m

Reputation: 6043

Javascript objects properties are accessible from anywhere ie. there are no real private variables so defining getter setter methods in this way is kind of pointless. If you want private variables or similar behaviour, read this http://javascript.crockford.com/private.html

Upvotes: 1

G-Wiz
G-Wiz

Reputation: 7426

not sure what your intention is (setters with no getters?), but you might be interested in private variables. to achieve the effect of private variables, you would start with the following:

function Guy(name) {
    var _name = name;

    this.getName = function(){ return _name; }
    this.setName = function(n) { _name = n; }
}

var g = new Guy("Bob");
alert(g.getName()); // works
alert(g._name); // doesn't work

(In fact in this simple example, you don't even need the variable _name; getName and setName can close over the function argument name);

Upvotes: 3

Dave
Dave

Reputation: 6179

It depends on if you're trying to enforce the encapsulation of x and y in an OOP manner. One way that javascript differs from - for example - Java is that it doesn't inherently enforce private variables. Usually, the common way to declare that some variable/method SHOULD be private is to name it with an underscore. So if you're actually trying to enforce OOP concepts here, then declare x and y like this:

function Node(name, x, y) {
  this.name = name;
  this._x = x;
  this._y = y;
}

And then keep your setters. If you aren't trying to enforce some kind of encapsulation of x and y to your Node, then go ahead and don't provide them and just use the node.x/node.y when you need to get/set x or y.

Just keep in mind that this is simply a naming convention and when this script is running, _x is just as visible as x. It will be up to you and any programmers you work with to enforce this.

Upvotes: 0

Andy E
Andy E

Reputation: 344497

No, you don't need those functions, unless you need some sort of callback-based system where a function should be executed when the value changes. You can access and assign to the properties directly, as you discovered.

Upvotes: 2

Related Questions