为什么在内部函数中出现ReferenceError?

发布于 2024-12-14 06:24:52 字数 1686 浏览 2 评论 0原文

首先,我认为下面的大部分代码与问题无关,我只是认为它会更多,呃..准确地放置整个代码的一部分。
所以只要看看我首先标记的线:

window.onload = function() {

var WebGLSupported = isWebGLSupported();
var renderer = WebGLSupported ? new THREE.WebGLRenderer() : new THREE.CanvasRenderer();
renderer.setSize( window.innerWidth, 600 );
document.body.appendChild( renderer.domElement );
var scene = new THREE.Scene();
var camera = new THREE.Camera(35,window.innerWidth/600,.1,10000);
camera.position.set( -5, 5, 25 );
var light = new THREE.PointLight( 0xffffff, .4 );
light.position.set( 10, 10, 10 );
scene.addLight( light );
var ambientLight = new THREE.AmbientLight( 0xbbbbbb );
scene.addLight( ambientLight );
var materialClass = WebGLSupported ? THREE.MeshLambertMaterial : THREE.MeshBasicMaterial;
var materialWall = new materialClass( { color: 0xffffff, map: THREE.ImageUtils.loadTexture( 'posts/wholewall.png' ) } );
var i;
var planeWalls=new Array();
for (i=1;i<=10;i++){
    planeWalls[i]=new THREE.Mesh(new THREE.PlaneGeometry(18,11,1,1), materialWall);
    planeWalls[i].position.z=i*(-5);
    scene.addChild(planeWalls[i]);
}
camera.lookAt(planeWalls[1].position);


animate();
function animate() {
    renderer.render( scene, camera ); <<-------BREAK AT HERE
    requestAnimFrame( animate );
}   

}

当我在那条线处中断时, 并添加 i 的监视,然后调试器显示:

异常:ReferenceError:i 未定义

然后我添加了上面数组的监视,planeWalls,并且还得到了ReferenceError,

但是我还添加了“场景”的监视,并且调试器显示了它的值正确,

所以我想知道为什么,

我的意思是,内部函数显然可以使用 i 的变量,对吧?

然后,我

alert(i);

在内部函数的开头添加,

然后调试器正确显示 i

那么,这方面有什么提示吗?

First of all, most of the follow code I think is not related to the question, I just think it will be more, uh.. exactly to put whole part of the code.
So just look at the line I marked first:

window.onload = function() {

var WebGLSupported = isWebGLSupported();
var renderer = WebGLSupported ? new THREE.WebGLRenderer() : new THREE.CanvasRenderer();
renderer.setSize( window.innerWidth, 600 );
document.body.appendChild( renderer.domElement );
var scene = new THREE.Scene();
var camera = new THREE.Camera(35,window.innerWidth/600,.1,10000);
camera.position.set( -5, 5, 25 );
var light = new THREE.PointLight( 0xffffff, .4 );
light.position.set( 10, 10, 10 );
scene.addLight( light );
var ambientLight = new THREE.AmbientLight( 0xbbbbbb );
scene.addLight( ambientLight );
var materialClass = WebGLSupported ? THREE.MeshLambertMaterial : THREE.MeshBasicMaterial;
var materialWall = new materialClass( { color: 0xffffff, map: THREE.ImageUtils.loadTexture( 'posts/wholewall.png' ) } );
var i;
var planeWalls=new Array();
for (i=1;i<=10;i++){
    planeWalls[i]=new THREE.Mesh(new THREE.PlaneGeometry(18,11,1,1), materialWall);
    planeWalls[i].position.z=i*(-5);
    scene.addChild(planeWalls[i]);
}
camera.lookAt(planeWalls[1].position);


animate();
function animate() {
    renderer.render( scene, camera ); <<-------BREAK AT HERE
    requestAnimFrame( animate );
}   

}

When I break at that line,
and add a watch of i and then the debugger says:

Exception: ReferenceError: i is not defined

then I add a watch of the array above, planeWalls, and also get an ReferenceError,

BUT I also add a watch of "scene",and the debugger shows its value correctly,

so I'm wondering why,

I mean, an inner function obviously can use the variable of i, right?

and then, I add

alert(i);

in the beginning of the inner function,

and then the debugger shows i correctly.

So, are there any hints for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭了丶谁疼 2024-12-21 06:24:52

很可能是调试器问题,如果alert() 显示它,则意味着它是可访问的。

Most likely it's a debugger problem, if alert() displays it, means that it's accessible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文