public
Last active

  • Download Gist
accessors.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13
class Vector
constructor: (@x, @y) ->
 
getX: () -> @[0]
getY: () -> @[1]
 
setX: (x) -> @[0] = x
setY: (y) -> @[1] = y
 
v = new Vector(3, 4)
 
ok v.x is v.getX()
ok v.y is v.getY()
accessors.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
var Vector, v;
Vector = function() {
function Vector(x, y) {
this.x = x;
this.y = y;
}
Vector.prototype.getX = function() {
return this[0];
};
Vector.prototype.__defineGetter__('x', Vector.prototype.getX);
Vector.prototype.getY = function() {
return this[1];
};
Vector.prototype.__defineGetter__('y', Vector.prototype.getY);
Vector.prototype.setX = function(x) {
return this[0] = x;
};
Vector.prototype.__defineSetter__('x', Vector.prototype.setX);
Vector.prototype.setY = function(y) {
return this[1] = y;
};
Vector.prototype.__defineSetter__('y', Vector.prototype.setY);
return Vector;
}();
v = new Vector(3, 4);
ok(v.x === v.getX());
ok(v.y === v.getY());

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.