Neither way for this particular problem. Furthermore, I've used many different ways, depending on the situation. The first question is, do you need to maintain state?
If not, then a simple function would suffice.
If you do, then there are several options available:
Traditional JavaScript OOP:
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype = {
above: function(point) {
return this.y > point.y;
},
below: function(point) {
this.y < point.y;
}
};
new Point(1, 2).above(new Point(3, 10));
Private variables using OOP:
function Point(x, y) {
var _x = x;
var _y = y;
this.above = function(point) {
return _y > point.getY();
}
this.below = function(point) {
return _y < point.getY();
}
this.getX = function() {
return _x;
}
this.getY = function() {
return _y;
}
}
Using the Module Pattern:
function Point(x, y) {
var _x = x;
var _y = y;
return {
above: function(point) {
return _y > point.getY();
},
below: function(point) {
return _y < point.getY();
},
getX: function() {
return _x;
},
getY: function() {
return _y;
}
};
}
p = Point(2, 3);
p2 = Point(4, 6);
p.above(p2);