I have a Point
function Point(x, y) {
this.x = x;
this.y = y;
};
As you see, it's mutable. So I can change it properties, like
var p = new Point(2, 3);
p.x = 6;
I want to add clone method so that expected behavior would be
var p1 = new Point(2, 3);
var p2 = p1.clone();
p1.x = 6;
assert p1 != p2; //first assertion. pseudocode.
assert p2.x == 2; //second assertion. pseudocode.
For implementing clone()
I rewrite Point in next way
function Point(x, y) {
this.x = x;
this.y = y;
this.clone = function () {
function TrickyConstructor() {
}
TrickyConstructor.prototype = this;
return new TrickyConstructor();
};
};
But second assertion fails for my implementation. How should I reimplement it?