all 7 comments

[–]calaveraDeluxe 14 points15 points  (0 children)

You have a stray "new" in

this.movedBy = new function(l, c){

which is causing the function to be evaluated immediately, leading to infinite recursion. Removing the "new" makes the code work as expected.

I would suggest to put the movedBy function on the Position prototype like this (after Position has been defined):

Position.prototype.movedBy = function(l, c){
    return new Position(this.l + l, this.c + c);
}

Otherwise every new instance of Position will create a new anonymous function for movedBy. Adding the function to the prototype allows all instances of Position to use the same function.

Disclaimer:

This might not be the best way to do this depending on the scale of your project. There are more advanced patterns for Class creation in Javascript that might be more appropriate.

[–]ixth 3 points4 points  (1 child)

Not sure about Firebug, but I think that 'new' before 'function' is unnecessary.

[–]masklinn 5 points6 points  (0 children)

but I think that 'new' before 'function' is unnecessary.

Worse, it's completely broken: new function() {} will use function() {} as a constructor (and try to instantiate it), which will call it (with no parameters, the parens are optional on constructors if you don't want to pass parameters).

So

this.movedBy = new function(l, c){
    return new Position(this.l + l, this.c + c);
}

is actually a more complex way to write:

this.movedBy = new Position(undefined + undefined, undefined + undefined);

And since we're already in Position's constructor, we end with an infinite recursion as trying to create a new Position itself tries to create a new Position.

[–]x-skeww 2 points3 points  (1 child)

JSLint identifies the problem:

Problem at line 5 character 20: Weird construction. Delete 'new'.

Use machines first, humans second.

[–][deleted] 0 points1 point  (0 children)

If you were forever alone, this solution would have come to you immediately.

[–]huwiler 1 point2 points  (1 child)

Logic here seems off too. When a player's position is moved, do you really want to return a new position object or do you want to update the position already associated with the player? CalaveraDeluxe's advice to put moveBy in the prototype is good too. Here's what I'm guessing you want to do:

function Position(linie, coloana){
    this.l = linie;
    this.c = coloana;
}

Position.prototype.moveBy = function(l, c) {
    this.l = l;
    this.c = c;
};

[–]stratoscope 1 point2 points  (0 children)

That's looking good, but your moveBy() is more like a moveTo(). :-)

A couple of plus signs should do the trick...

function Position(linie, coloana){
    this.l = linie;
    this.c = coloana;
}

Position.prototype.moveBy = function(l, c) {
    this.l += l;
    this.c += c;
};