Javascript 如何通过原型定义常量?

Javascript 如何通过原型定义常量?,javascript,Javascript,请帮忙解决这个问题 我制作脚本: Board = function(width, height, background){ this.width = width; this.height = height; this.background = background; this.Create(); } Board.prototype.Create = function(){ var board = $('<div class="bo

请帮忙解决这个问题

我制作脚本:

Board = function(width, height, background){ 
    this.width = width; 
    this.height = height;   
    this.background = background;   

    this.Create();
}

Board.prototype.Create = function(){
    var board = $('<div class="board" id="board"></div>').css({
        width: this.width + 'px',
        height: this.height + 'px',
        background: this.background
    });

    $('#game').append(board);
}


Tank = function(id){ 
  this.x_coord = helper.randomIntFromZero(481);
  this.y_coord = helper.randomIntFromZero(481);
  this.id = id; 
  this.arrow; 
  this.direction;
  this.DIRECTION = ['up', 'right', 'bottom', 'left'];

  this.Create();
}

Tank.prototype.Create = function(){
  this.direction = this.DIRECTION[helper.randomIntFromZero(4)];

  var tank = $('<div class="tank" id="' + this.id + '"></div>').css({
    left: this.x_coord + 'px',
    top: this.y_coord + 'px'
  });

  $('#board').append(tank);
}

Tank.prototype.checkArrowDirection = function(){
  switch (this.direction) {
   case 'up':
      this.arrow = '▲';
      break
   case 'right':
      this.arrow = '►';
      break
   case 'bottom':
      this.arrow = '▼';
      break
   case 'left':
      this.arrow = '◄';
      break         
   default:
      console.log('error arrow direction');
      break
  }
}

Tank.prototype.checkBorderCollision = function(){
  switch (this.direction) {
   case 'up':
      this.y_coord -= 10;
      if(this.y_coord <= 0) this.y_coord = 0; 
      break
   case 'right':
      this.x_coord += 10;
      if(this.x_coord >= 480) this.x_coord = 480;
      break
   case 'bottom':
      this.y_coord += 10;
      if(this.y_coord >= 480) this.y_coord = 480;
      break
   case 'left':
      this.x_coord -= 10;
      if(this.x_coord <= 0) this.x_coord = 0;
      break         
   default:
      console.log('error direction definition');
      break
  }
}

Tank.prototype.checkChangeDirection = function(){
  if(helper.randomIntFromZero(100) > 75){
    this.direction = this.DIRECTION[helper.randomIntFromZero(4)];
  }
}

Tank.prototype.Offset = function(){
  $('#' + this.id).css({
    left: this.x_coord + 'px',
    top: this.y_coord + 'px'
  }).html(this.arrow);
}

Tank.prototype.Move = function(){
  this.checkChangeDirection();
  this.checkBorderCollision();
  this.checkArrowDirection();
  this.Offset();
}


function Helper(){
    this.randomIntFromInterval = function(minInclusive, maxExclusive){
        return Math.floor(Math.random() * (maxExclusive - minInclusive + 1)) + minInclusive;
    };

    this.randomIntFromZero = function(maxExclusive){
        return Math.floor(Math.random() * (maxExclusive));
    };  
};


$(document).ready(function(){
    var board = new Board(500, 500, 'orange'),
            tanks = [];

    helper = new Helper();          

    var i = 0;
    while (i < 6) {
      tanks[i] = new Tank(i);
      i++;
    }

    setInterval(function(){
        var i = 0;
        while (i < 6) {
          tanks[i].Move();
          i++;
        }       
    }, 500);
});
但它也不起作用。

这是因为您在构造函数内部声明了方向,所以它在构造函数外部不可见

移动声明方向=[“向上”、“向右”、“向下”、“向左”];在文件的顶部,在任何函数之外,它都可以工作

这个代码就是问题所在

Tank.prototype.Create = function(){
  self.direction = DIRECTION[helper.randomIntFromZero(4)];

或者,如注释中所述,如果要将方向存储为对象属性,则应使用this.DIRECTIONS或Tank.DIRECTIONS(如果是静态字段)。

或将其设置为Tank.prototype.DIRECTION,以避免污染全局名称空间,然后在其他函数中使用this.DIRECTION。self也适用于此功能,只有这样才能隐藏window.self,这样您才能真正设置window.self.direction,而不是Tank.self。direction@linoleum谢谢但是您的变体不起作用@stackow101您在所有方法中都使用self,但是您没有声明它。记住self不是一个关键字,它只是一个变量,您应该在每个方法的顶部声明它,或者使用一个简单的方法。现在控制台中没有错误,这似乎与ES6无关。还是这样?
Tank.prototype.Create = function(){
  self.direction = DIRECTION[helper.randomIntFromZero(4)];