简体   繁体   English

实例化函数中的对象会给出“未捕获的TypeError:未定义不是函数”

[英]Instantiating object in a function gives “Uncaught TypeError: undefined is not a function”

I tried to look most of the suggested search results concerning the error message I receive, but unfortunately none is talking about a case that is similar to mine. 我尝试查看有关收到的错误消息的大多数建议搜索结果,但不幸的是,没有人在谈论与我的情况类似的情况。 So I believe this is not a duplicate. 因此,我相信这不是重复的。

(In particular I don't use jQuery, I don't want to use it. Besides, the accepted answer given is correct and it does not involve jQuery, it elaborates the understanding of hoisting in JavaScript.) (特别是我不使用jQuery,我不想使用它。此外,给出的可接受答案是正确的,并且不涉及jQuery,它阐明了对JavaScript吊装的理解。)

I wonder Why the code below (2nd code snippet) does not work? 我想知道为什么下面的代码(第二个代码段)不起作用? I can't figure that out. 我不知道这一点。


This code works. 此代码有效。 I instantiate Cell objects outside the drawGrid function. 我在drawGrid函数外部实例化Cell对象。

  'use strict'; var dim = 20; var side_length = 25; var canvas = document.getElementById('world'); canvas.width = 500; canvas.height = 500; document.body.appendChild(canvas); if (canvas.getContext) { var ctx = canvas.getContext('2d'); drawGrid(ctx); } var Cell = function(x, y, alive, context) { this.x = x; this.y = y; this.alive = alive; this.ctx = context; }; Cell.prototype.draw = function() { if (this.alive === true) { this.ctx.beginPath(); this.ctx.arc(this.x + side_length / 2, this.y + side_length / 2, 10, 0, 2 * Math.PI); this.ctx.fill(); } }; for (var i = 0; i < dim; i++) { for (var j = 0; j < dim; j++) { var x = i * canvas.width / dim, y = j * canvas.height / dim; new Cell(x, y, true, ctx).draw(); } } function drawGrid(ctx) { for (var i = 0; i < dim; i++) { for (var j = 0; j < dim; j++) { var x = i * canvas.width / dim, y = j * canvas.height / dim; ctx.strokeRect(x, y, side_length, side_length); } } } 
 <canvas id='world'></canvas> 


This code does not work. 此代码不起作用。 I instantiate the Cell objects inside the drawGrid function. 我实例化了drawGrid函数中的Cell对象。

  'use strict'; var dim = 20; var side_length = 25; var canvas = document.getElementById('world'); canvas.width = 500; canvas.height = 500; document.body.appendChild(canvas); if (canvas.getContext) { var ctx = canvas.getContext('2d'); drawGrid(ctx); } var Cell = function(x, y, alive, context) { this.x = x; this.y = y; this.alive = alive; this.ctx = context; }; Cell.prototype.draw = function() { if (this.alive === true) { this.ctx.beginPath(); this.ctx.arc(this.x + side_length / 2, this.y + side_length / 2, 10, 0, 2 * Math.PI); this.ctx.fill(); } }; function drawGrid(ctx) { for (var i = 0; i < dim; i++) { for (var j = 0; j < dim; j++) { var x = i * canvas.width / dim, y = j * canvas.height / dim; ctx.strokeRect(x, y, side_length, side_length); new Cell(x, y, true, ctx).draw(); } } } 
 <canvas id='world'></canvas> 

The problem is here: 问题在这里:

 if (canvas.getContext) {
   var ctx = canvas.getContext('2d');
   drawGrid(ctx);
 }

 var Cell = function(x, y, alive, context) {

You're assigning Cell after you call drawGrid . 您在调用drawGrid 之后分配Cell So, inside drawGrid , Cell is undefined . 因此,在drawGrid内部, Cellundefined

Simple fix 1, use a standard function declaration to have the assignment hoisted along the variable declaration : 简单修复1,使用标准函数声明使赋值沿变量声明悬挂

 function Cell(x, y, alive, context) {

Simple (more readable) fix 2: just move the call to drawGrid at the end. 简单(更具可读性)解决方案2:只需将调用移到drawGrid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM