JavaScript:全局变量don';似乎在当地没有可参考性

JavaScript:全局变量don';似乎在当地没有可参考性,javascript,variables,global,Javascript,Variables,Global,我在函数中遇到了一些问题,因为一些变量之前已经定义过;他们被认为是全球性的,但他们的行为似乎不像这样 var global = "Summat."; function Function() { alert(global); //alerts "undefined" //some more code referencing similar variables alert("Hey."); //doesn't display. } 我不确定问题是否真的是

我在函数中遇到了一些问题,因为一些变量之前已经定义过;他们被认为是全球性的,但他们的行为似乎不像这样

var global = "Summat.";
function Function() {
    alert(global);    //alerts "undefined"
    //some more code referencing similar variables
    alert("Hey.");    //doesn't display.
    }
我不确定问题是否真的是我相信的全局变量的行为不像全局变量,但这就是我用有限的故障排除能力缩小范围的原因。如果有帮助的话,我也非常乐意发布并尝试解释完整/更多的代码

这是更扩展的版本:

<!DOCTYPE html>
<html>

<head>
    <title>divMan: Canvas</title>
</head>

<body>
    <canvas id="canvas" height="768px" width="1366px" style="position:fixed;top:0px;left:0px;"></canvas>
</body>

<script>
    window.onload = drawFrame();

//----------------------------------------Global Variables-----------------------------//
    var context = document.getElementById("canvas").getContext("2d");
//------------------------------------------------------------------------------------//

//-----------------------------------------------------------------------Constructors-----------------------------------------------------------------------------------------------//
    function Point(top,left) {this.top = top; this.left = left;}
    function Stone(top,left,height,width) {//a seemingly functional constructor}
    function Tree(top,left,trunkHeight,trunkWidth,crownHeight,crownWidth) {//a seemingly functional constructor}
    function DivMan(top,left,headHeight,headWidth,bodyHeight,bodyWidth) {//a seemingly functional constructor}
    function Sky(top,left,height,width) {//a seemingly functional constructor}
    function Ground() {//a seemingly functional constructor}
    function Grass(height,targetGround) {//a seemingly functional constructor}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//

//----------------------------------------------Objects------------------------------------------------//
    var sky = new Sky(0,0,538,1366);            //the sky.
    var ground = new Ground();              //the ground.
    var grass = new Grass(38,ground);           //the grass.
    var stone = new Stone(418,228,75,75);       //a stone.
    var pedestal = new Stone(508,630,30,200);       //a stone pedestal.
    var tree0 = new Tree(138,1000,150,60,250,300);  //a tree.
    var tree1 = new Tree(83,73,300,40,100,300); //another tree.
    var divMan = new DivMan(408,700,34,30,65,60);   //divMan!!
//----------------------------------------------------------------------------------------------------//

    function drawFrame() {
            sky.Draw();
            ground.Draw();
        grass.Draw();
        tree1.Draw();
        stone.Draw();
        pedestal.Draw();
        divMan.Draw();
        tree0.Draw();
        }

迪夫曼:帆布
window.onload=drawFrame();
//----------------------------------------全局变量-----------------------------//
var context=document.getElementById(“画布”).getContext(“2d”);
//------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------建设者-----------------------------------------------------------------------------------------------//
功能点(顶部,左侧){this.top=top;this.left=left;}
功能石(顶部、左侧、高度、宽度){//看似功能性的构造器}
函数树(顶部、左侧、树干高度、树干宽度、树冠高度、树冠宽度){//
函数DivMan(顶部、左侧、头高、头宽、体高、体宽){//a貌似函数构造函数}
函数天空(顶部、左侧、高度、宽度){//一个看似函数的构造函数}
函数Ground(){//一个看似函数的构造函数}
函数Grass(高度、目标地面){//一个看似函数的构造函数}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
//----------------------------------------------物体------------------------------------------------//
var sky=新天空(0,05381366)//天空。
var接地=新接地()//地面。
var grass=新草(38,地面)//草地。
var石材=新石材(418228,75,75)//一块石头。
var底座=新石材(508630,30200)//石头底座。
var tree0=新树(1381000150,60250300)//一棵树。
var tree1=新树(83,73300,4010300)//另一棵树。
var divMan=新divMan(408700,34,30,65,60)//迪夫曼!!
//----------------------------------------------------------------------------------------------------//
函数drawFrame(){
sky.Draw();
地。画();
草。画();
tree1.Draw();
石头。画();
图();
divMan.Draw();
tree0.Draw();
}
这就是现在整个事情的样子:

<!DOCTYPE html>
<html>

<head>
    <title>divMan: Canvas</title>
</head>

<style>
    * {-ms-touch-action:none;}
</style>

<body>
    <canvas id="canvas" height="768px" width="1366px" style="position:fixed;top:0px;left:0px;"></canvas>
</body>

<script>
    window.onload = drawFrame();
    //window.onclick = walk(event);

//----------------------------------------Global Variables-----------------------------//
    var context = document.getElementById("canvas").getContext("2d");
    //var _walk = false;
    /*var staticEquilibrium = true;
    //--Kinematic Variables--//
        var vSubX = 0;
        var aSubX = 0;
        var jSubX = 0;
        var vSubY = 0;
        var aSubY = 0;
        var jSubY = 0;*/
//  var deltaT = .001;      //standard time interval.
//  var interval;           //a timing loop variable.
//------------------------------------------------------------------------------------//

//-----------------------------------------------------------------------Constructors-----------------------------------------------------------------------------------------------//
    function Point(top,left) {this.top = top; this.left = left;}    //Parameters are numbers, to be used for pixel values.
    function Stone(top,left,height,width) {     //stones.
        this.origin = new Point(top,left);      //make sure to give this a curved top, eventually.
        this.height = height;
        this.width = width;
        this.color = "#a0a0a0";
        this.Draw = drawStone;
            function drawStone() {
                context.fillStyle = this.color;
                context.fillRect(this.origin.left,this.origin.top,this.width,this.height);
                }
        }
    function Tree(top,left,trunkHeight,trunkWidth,crownHeight,crownWidth) {     //trees.
        this.origin = new Point(top,left);
        this.trunkHeight = trunkHeight;
        this.trunkWidth = trunkWidth;
        this.trunkColor = "#702000";
        this.crownHeight = crownHeight;
        this.crownWidth = crownWidth;
        this.crownColor = "#40d030";
        this.Draw = drawTree;
            function drawTree() {
                context.fillStyle = this.crownColor;
                context.fillRect(this.origin.left,this.origin.top,this.crownWidth,this.crownHeight);
                context.fillStyle = this.trunkColor;
                context.fillRect(this.origin.left+(this.crownWidth-this.trunkWidth)/2,this.origin.top+this.crownHeight,this.trunkWidth,this.trunkHeight);
                }
        }
    function DivMan(top,left,headHeight,headWidth,bodyHeight,bodyWidth) {       //divMEN.
        this.origin = new Point(top,left);
        this.headHeight = headHeight;
        this.headWidth = headWidth;
        this.bodyHeight = bodyHeight;
        this.bodyWidth = bodyWidth;
        this.color = "#000000";
        this.Draw = drawDivMan;
            function drawDivMan() {
                context.fillStyle = this.color;
                context.fillRect(this.origin.left+(this.bodyWidth-this.headWidth)/2,this.origin.top,this.headWidth,this.headHeight);
                context.fillRect(this.origin.left,this.origin.top+this.headHeight+1,this.bodyWidth,this.bodyHeight);
                }
        }
    function Sky(top,left,height,width) {               //skies.
        this.origin = new Point(top,left);
        this.height = height;
        this.width = width;
        this.color = "#98e8ff";
        this.Draw = drawSky;
            function drawSky() {
                alert("Yorishomu.");
                context.fillStyle = this.color;
                context.fillRect(this.origin.left,this.origin.top,this.width,this.height);
                }
        }
    function Ground() {             //the ground.
        this.origin = new Point(538,0);
        this.hillStart = new Point(538,1);
        this.hillTop1 = new Point(488,114);
        this.hillTop2 = new Point(488,429);
        this.hillEnd = new Point(538,543);
        this.screenEnd = new Point(538,1366);
        this.bottomRight = new Point(768,1366);
        this.bottomLeft = new Point(768,0);
        this.color = "#401000";
        this.Draw = drawGround;
            function drawGround() {
                context.fillStyle = this.color;
                context.beginPath();
                context.moveTo(this.origin.left,this.origin.top);
                context.lineTo(this.hillStart.left,this.hillStart.top);
                context.lineTo(this.hillTop1.left,this.hillTop1.top);
                context.lineTo(this.hillTop2.left,this.hillTop2.top);
                context.lineTo(this.hillEnd.left,this.hillEnd.top);
                context.lineTo(this.screenEnd.left,this.screenEnd.top);
                context.lineTo(this.bottomRight.left,this.bottomRight.top);
                context.lineTo(this.bottomLeft.left,this.bottomLeft.top);
                context.closePath();
                context.fill();
                }
        }
    function Grass(height,targetGround) {           //the grass.
        this.color = "#10b030"
        this.height = height;
        this.targetGround = targetGround;
        this.Draw = drawGrass;
            function drawGrass() {
                context.strokeStyle = this.color;
                context.lineWidth = this.height;
                context.beginPath();
                context.moveTo(this.targetGround.origin.left,this.targetGround.origin.top);     //it follows the ground.
                context.lineTo(this.targetGround.hillStart.left,this.targetGround.hillStart.top);
                context.lineTo(this.targetGround.hillTop1.left,this.targetGround.hillTop1.top);
                context.lineTo(this.targetGround.hillTop2.left,this.targetGround.hillTop2.top);
                context.lineTo(this.targetGround.hillEnd.left,this.targetGround.hillEnd.top);
                context.lineTo(this.targetGround.screenEnd.left,this.targetGround.screenEnd.top);
                context.stroke();
                }
        }
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//

//----------------------------------------------Objects------------------------------------------------//
    var sky = new Sky(0,0,538,1366);            //the sky.
    var ground = new Ground();              //the ground.
    var grass = new Grass(38,ground);           //the grass.
    var stone = new Stone(418,228,75,75);       //a stone.
    var pedestal = new Stone(508,630,30,200);       //a stone pedestal.
    var tree0 = new Tree(138,1000,150,60,250,300);  //a tree.
    var tree1 = new Tree(83,73,300,40,100,300); //another tree.
    var divMan = new DivMan(408,700,34,30,65,60);   //divMan!!
//----------------------------------------------------------------------------------------------------//

    function drawFrame() {
        context.fillStyle = "#000000";
        context.fillRect(0,0,100,100);
        alert("Yo." + sky + context);
        sky.Draw();
        alert("Hi.");
        ground.Draw();
        grass.Draw();
        tree1.Draw();
        stone.Draw();
        pedestal.Draw();
        divMan.Draw();  // <-- Here's divMan.
        tree0.Draw();
        alert("Hey.");
        }

    /*function walk(click) {        //in future, walk in mini-hops.
        _walk = true;
        staticEquilibrium = false;
        if (click.screenX > rSubX+30) {vSubX = 5;} else {vSubX = -5;}
        update();
        }

    function update() {
        interval = setInterval(function() {
            for (i=0; i<1; i++) {
                divMan.origin.left += vSubX*deltaT+aSubX*deltaT*deltaT/2;
                divMan.origin.top += vSubY*deltaT+aSubY*deltaT*deltaT/2;
                }
            drawFrame();
            if (false) {clearInterval(interval);}
            }
        }*/
</script>

</html>

迪夫曼:帆布
*{-ms触摸操作:无;}
window.onload=drawFrame();
//window.onclick=walk(事件);
//----------------------------------------全局变量-----------------------------//
var context=document.getElementById(“画布”).getContext(“2d”);
//var_walk=false;
/*var静态平衡=真;
//--运动变量--//
var-vSubX=0;
var aSubX=0;
var-jSubX=0;
var-vSubY=0;
var aSubY=0;
var-jSubY=0*/
//var deltaT=.001//标准时间间隔。
//var区间//定时循环变量。
//------------------------------------------------------------------------------------//
//-----------------------------------------------------------------------建设者-----------------------------------------------------------------------------------------------//
函数点(顶部,左侧){this.top=top;this.left=left;}//参数是数字,用于像素值。
功能石材(顶部、左侧、高度、宽度){//石材。
this.origin=新点(顶部,左侧);//确保最终给它一个弯曲的顶部。
高度=高度;
这个。宽度=宽度;
this.color=“#a0”;
这个。Draw=drawStone;
函数drawStone(){
context.fillStyle=this.color;
context.fillRect(this.origin.left,this.origin.top,this.width,this.height);
}
}
功能树(顶部、左侧、树干高度、树干宽度、树冠高度、树冠宽度){//树。
this.origin=新点(顶部,左侧);
this.trunkheath=树干高度;
this.trunkWidth=trunkWidth;
this.trunkColor=“#702000”;
此.crownHeight=crownHeight;
this.crownWidth=crownWidth;
this.crownColor=“#40d030”;
this.Draw=drawTree;
函数drawTree(){
context.fillStyle=this.crownColor;
context.fillRect(this.origin.left,this.origin.top,this.crownWidth,this.crownHeight);
context.fillStyle=this.trunkColor;
context.fillRect(this.origin.left+(this.crownWidth this.trunkWidth)/2,this.origin.top+this.crownHeight,this.trunkWidth,this.trunkHeight);
}
}
功能分区(顶部、左侧、头高、头宽、体高、体宽){//divMEN。
this.origin=新点(顶部,左侧);
此项。头高=头高;
此参数。头宽=头宽;
这个.车身高度=车身高度;
this.bodyWidth=车身宽度;
this.color=“#000000”;
this.Draw=drawDivMan;
函数drawDivMan(){
context.fillStyle=this.color;
context.fillRect(this.origin.left+(this.bodyWidth this.headWidth)/2,this.origin.top,this.headWidth,this.headHeight);
context.fillRect(this.origin.left,this.origin.top+this.headHeight+1,this.bodyWidth,this.bodyHeight);
}
}
功能天空(顶部、左侧、高度、宽度){//天空。
this.origin=新点(顶部,le
window.onload = drawFrame();
// -------------- remove ^^
// the parentheses call function immediately
// and assign its return value to window.onload
window.onload = drawFrame;
// now you're actually assigning the function
// itself to window.onload, as it should be