Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数组类型错误_Javascript_Arrays_Typeerror - Fatal编程技术网

Javascript 数组类型错误

Javascript 数组类型错误,javascript,arrays,typeerror,Javascript,Arrays,Typeerror,因此,我得到一个“未捕获的TypeError:无法读取未定义的属性“0” 第42行的“更新时”,即: init.ctx.moveTo(this.trail[0].x,this.trail[0].y); 从以下代码: window.onresize = function(){ init.canvas.width = window.innerWidth; init.canvas.height = window.innerHeight; //updateComponents(); }

因此,我得到一个“未捕获的TypeError:无法读取未定义的属性“0” 第42行的“更新时”,即:

init.ctx.moveTo(this.trail[0].x,this.trail[0].y); 
从以下代码:

window.onresize = function(){
  init.canvas.width = window.innerWidth;
  init.canvas.height = window.innerHeight;
  //updateComponents();
}
var init = {
  canvas: new Object(),
  ctx: new Object(),
  constructCanvas: function(){
    this.canvas = document.getElementById("canvas");
    this.ctx = this.canvas.getContext("2d");
    this.canvas.width = window.innerWidth;
    this.canvas.height = window.innerHeight;
  }
}
init.constructCanvas();

var background = {
  color: "green",
  refresh: function(){
    init.ctx.fillStyle = this.color;
    init.ctx.fillRect(0,0,init.canvas.width,init.canvas.height);
  }
}
//background.refresh();

function updateComponents(){
  background.refresh();
}

function WalkingLine(arguments){
  this.frames = 0;
  this.trail = [];
  this.walkLength = 5;
  this.position = arguments.position;
  this.color = "black";
  this.trail[0] = this.position;
}
WalkingLine.prototype = {
  update: function(){
    init.ctx.beginPath();
    init.ctx.moveTo(this.trail[0].x,this.trail[0].y);    
    var i;
    for(i=1;i<this.trail.length;i++)
      init.ctx.lineTo(this.trail[i].x,this.trail[i].y);
    init.ctx.stroke();
    this.generateNext();
    },
  genarateNext: function(){
    this.trail[this.trail.length] = {x:this.trail[this.trail.length-1].x+5,y:this.trail[this.trail.length-1].y};
    init.ctx.lineTo(this.trail[this.trail.length-1].x,this.trail[this.trail.length-1].y);
    inti.ctx.stroke();
}
};
var one = new WalkingLine({position:{x:100,y:100}});
setTimeout(one.update,1000);
window.onresize=function(){
init.canvas.width=window.innerWidth;
init.canvas.height=window.innerHeight;
//updateComponents();
}
var init={
画布:新对象(),
ctx:新对象(),
constructCanvas:function(){
this.canvas=document.getElementById(“canvas”);
this.ctx=this.canvas.getContext(“2d”);
this.canvas.width=window.innerWidth;
this.canvas.height=window.innerHeight;
}
}
init.constructCanvas();
变量背景={
颜色:“绿色”,
刷新:函数(){
init.ctx.fillStyle=this.color;
init.ctx.fillRect(0,0,init.canvas.width,init.canvas.height);
}
}
//background.refresh();
函数updateComponents(){
background.refresh();
}
函数WalkingLine(参数){
这是0.frames=0;
this.trail=[];
此值为0.walkLength=5;
this.position=arguments.position;
this.color=“black”;
this.trail[0]=this.position;
}
WalkingLine.prototype={
更新:函数(){
init.ctx.beginPath();
init.ctx.moveTo(this.trail[0].x,this.trail[0].y);
var i;
对于(i=1;i你失去了上下文:

setTimeout(one.update,1000);
这样,您只传递update而不传递update,因此当稍后调用它时,它不知道它属于何处。您需要直接调用它(在arrow函数中):

或者您需要将函数绑定到一个:

setTimeout(one.update.bind(one),1000);
你失去了背景:

setTimeout(one.update,1000);
这样,您只传递update而不传递update,因此当稍后调用它时,它不知道它属于何处。您需要直接调用它(在arrow函数中):

或者您需要将函数绑定到一个:

setTimeout(one.update.bind(one),1000);
你失去了上下文你失去了上下文