如何访问旧值-JavaScript

如何访问旧值-JavaScript,javascript,Javascript,我很难弄清楚如何存储myRover.position的先前值。我需要访问它,以便在接近障碍物时进行备份 var myRover = { position: [0, 0], direction: 'N', obstacleX: [0], obstacleY: [2], marsGrid: [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0

我很难弄清楚如何存储myRover.position的先前值。我需要访问它,以便在接近障碍物时进行备份

var myRover = {
position: [0, 0],
direction: 'N',
obstacleX: [0],
obstacleY: [2],
marsGrid: [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
};

//prevent rover from falling off of planet Mars!
//my best attempt at wrapping a 2d array :-(
function preventSuddenDeath() {
if (myRover.position[0] > 9) {
myRover.position[0] = 0;
}
if (myRover.position[0] < 0) {
myRover.position[0] = 9;
}
if (myRover.position[1] > 9) {
myRover.position[1] = 0;
}
if (myRover.position[1] < 0) {
myRover.position[1] = 9;
}
}
我已经尝试将oldValue设置为与currentValue相同的值,正如我假设的那样,它只是将oldValue重写为当前值

function manueverObstacle() {
var oldPosition = myRover.position;
if (myRover.position[0] == myRover.obstacleX && myRover.position[1] ==   myRover.obstacleY) {
console.log("Display value of myRover.position: " + myRover.position);
console.log("Display value of oldPosition: " + oldPosition);
return myRover.position();
}
---->Rover is at 0,0 and heading N
//here I've given it the commands to turn right and go forward twice in order to test it
---->0,0
---->0,1
//breaks at 0,2 due to obstacle
----> Display value of myRover.position: 0,2
----> Display value of oldPosition: 0,2
如果有帮助的话,剩下的

function move_forward() {
if (myRover.direction == 'N') {
myRover.position[0] -= 1;
} else if (myRover.direction == 'E') {
myRover.position[1] += 1;
} else if (myRover.direction == 'S') {
myRover.position[0] += 1;
} else if (myRover.direction == 'W') {
myRover.position[1] -= 1;
} preventSuddenDeath();
}

function move_backward() {
if (myRover.direction == 'N') {
myRover.position[0] += 1;
} else if (myRover.direction == 'E') {
myRover.position[1] -= 1;
} else if (myRover.direction == 'S') {
myRover.position[0] -= 1;
} else if (myRover.direction == 'W') {
myRover.position[1] += 1;
} preventSuddenDeath();
}

function turn_left() {
if (myRover.direction == 'N') {
myRover.direction = 'W';
} else if (myRover.direction == 'E') {
myRover.direction = 'N';
} else if (myRover.direction == 'S') {
myRover.direction = 'E';
} else if (myRover.direction == 'W') {
myRover.direction = 'S';
}
}


function turn_right() {
if (myRover.direction == 'N') {
myRover.direction = 'E';
} else if (myRover.direction == 'E') {
myRover.direction = 'S';
} else if (myRover.direction == 'S') {
myRover.direction = 'W';
} else if (myRover.direction == 'W') {
myRover.direction = 'N';
}
}

function letsGoMrRover() {

//take user input from html prompt and convert the string to an array
var commandString = document.getElementById("command").value;
var output = document.getElementById("demo");
var csLength = commandString.length;
var commandArray = [];

//console.log the starting position
output.innerHTML = "Rover is at " + myRover.position + " and heading " +   myRover.direction + "<br />";

for (var i=0; i < csLength; i++) {
commandArray[i] = commandString[i];

switch (commandArray[i]) {
  case 'f':
    move_forward();
    break;
  case 'b':
    move_backward();
    break;
  case 'l':
    turn_left();
    break;
  case 'r':
    turn_right();
    break;
  }
maneuverObstacle();
console.log(myRover.position);
}
}
函数向前移动(){
如果(myRover.direction=='N'){
myRover.位置[0]-=1;
}else if(myRover.direction=='E'){
myRover.位置[1]+=1;
}else if(myRover.direction=='S'){
myRover.位置[0]+=1;
}否则如果(myRover.direction=='W'){
myRover.位置[1]-=1;
}防止突然死亡();
}
函数向后移动(){
如果(myRover.direction=='N'){
myRover.位置[0]+=1;
}else if(myRover.direction=='E'){
myRover.位置[1]-=1;
}else if(myRover.direction=='S'){
myRover.位置[0]-=1;
}否则如果(myRover.direction=='W'){
myRover.位置[1]+=1;
}防止突然死亡();
}
函数左转弯(){
如果(myRover.direction=='N'){
myRover.direction='W';
}else if(myRover.direction=='E'){
myRover.direction='N';
}else if(myRover.direction=='S'){
myRover.direction='E';
}否则如果(myRover.direction=='W'){
myRover.direction='S';
}
}
函数右转(){
如果(myRover.direction=='N'){
myRover.direction='E';
}else if(myRover.direction=='E'){
myRover.direction='S';
}else if(myRover.direction=='S'){
myRover.direction='W';
}否则如果(myRover.direction=='W'){
myRover.direction='N';
}
}
函数letsgomrover(){
//从html提示符获取用户输入,并将字符串转换为数组
var commandString=document.getElementById(“命令”).value;
var输出=document.getElementById(“演示”);
var csLength=commandString.length;
var commandArray=[];
//console.log记录起始位置
output.innerHTML=“月球车位于“+myRover.position+”和航向“+myRover.direction+”
”; 对于(变量i=0;i
一种方法是创建一个计算新位置的
newPosition
变量。然后在更新漫游者的位置之前,检查此新位置是否有障碍物


或者,您可以将
prevPosition
添加到
myRover
以存储以前的位置。

只需创建一个名为
prev
的变量或其他变量。我会在移动漫游车之前检查障碍物。检查障碍物功能,以机器人x,y为参数,并返回是否可以移动,这样在实际将机器人x更改为x+1之前,您可以通过x+1,y为什么障碍物和火星栅格是漫游者对象的一部分?这看起来像是三件截然不同的事情。“它只是将prevValue重写为当前值。”这是什么意思?你能给我一个完整的例子,我可以自己运行吗?我想这只是我最初尝试让位置[0,0]和marsGrid“同步”的一部分,所以我坚持使用它。作业几天后就要交了,虽然我现在知道了许多更好的方法来编写程序中的代码,但我担心改变一件大事会破坏许多其他事情@KH2017请编辑您的问题,以显示您的尝试。另外,一定要展示结果,并解释它与您想要的不同之处。
function move_forward() {
if (myRover.direction == 'N') {
myRover.position[0] -= 1;
} else if (myRover.direction == 'E') {
myRover.position[1] += 1;
} else if (myRover.direction == 'S') {
myRover.position[0] += 1;
} else if (myRover.direction == 'W') {
myRover.position[1] -= 1;
} preventSuddenDeath();
}

function move_backward() {
if (myRover.direction == 'N') {
myRover.position[0] += 1;
} else if (myRover.direction == 'E') {
myRover.position[1] -= 1;
} else if (myRover.direction == 'S') {
myRover.position[0] -= 1;
} else if (myRover.direction == 'W') {
myRover.position[1] += 1;
} preventSuddenDeath();
}

function turn_left() {
if (myRover.direction == 'N') {
myRover.direction = 'W';
} else if (myRover.direction == 'E') {
myRover.direction = 'N';
} else if (myRover.direction == 'S') {
myRover.direction = 'E';
} else if (myRover.direction == 'W') {
myRover.direction = 'S';
}
}


function turn_right() {
if (myRover.direction == 'N') {
myRover.direction = 'E';
} else if (myRover.direction == 'E') {
myRover.direction = 'S';
} else if (myRover.direction == 'S') {
myRover.direction = 'W';
} else if (myRover.direction == 'W') {
myRover.direction = 'N';
}
}

function letsGoMrRover() {

//take user input from html prompt and convert the string to an array
var commandString = document.getElementById("command").value;
var output = document.getElementById("demo");
var csLength = commandString.length;
var commandArray = [];

//console.log the starting position
output.innerHTML = "Rover is at " + myRover.position + " and heading " +   myRover.direction + "<br />";

for (var i=0; i < csLength; i++) {
commandArray[i] = commandString[i];

switch (commandArray[i]) {
  case 'f':
    move_forward();
    break;
  case 'b':
    move_backward();
    break;
  case 'l':
    turn_left();
    break;
  case 'r':
    turn_right();
    break;
  }
maneuverObstacle();
console.log(myRover.position);
}
}