I am trying to test Map class. My class has a method getCenter() that returns {x:0,y:0}. Also, I want to get {x:1,y:0} calling getRight() and I still want that getCenter() returns {x:0,y:0}.
var assert = require('assert')
, Map = require('../lib/map')
describe('Map()', function () {
describe('#getCenter()', function () {
it('should return [0,0] by default', function () {
var map = new Map()
assert.deepEqual({x: 0, y: 0}, map.getCenter())
})
})
describe('#getRight()', function () {
it('should return [1,0] by default', function () {
var map = new Map()
assert.equal(1, map.getRight().x)
assert.deepEqual({x: 0, y: 0}, map.getCenter())
})
})
})
But I am doing something wrong:
var Map = function () {
this.center = {x: 0, y: 0}
}
module.exports = Map
Map.prototype.getCenter = function () {
return this.center
}
Map.prototype.getRight = function () {
var new_position = this.center
new_position.x += 1
return new_position
}
I dont want to alter this.center. How can I create new variable? I dont understand the scope of "this.center". I am not changing that variable.