简体   繁体   English

Javascript-全局和局部变量之间的混淆

[英]Javascript - confusion between global & local variable

EDIT2 : Here is the console log. EDIT2 :这是控制台日志。 The image is being converted to canvas (HTML5), I need to 'return' this to the global variable somehow. 图片已转换为画布(HTML5),我需要以某种方式将其“返回”到全局变量。 Suggestions? 有什么建议吗?

<img id="mainIllustration" alt="main illustration" src="Img/Model01.png">
Illust...o_03.js (line 24)
<canvas id="mainIllustration" class="" width="278" height="659" style="" title="" tabindex="-1">
Illust...o_03.js (line 32)
<img id="mainIllustration" alt="main illustration" src="Img/Model01.png">
Illust...o_03.js (line 24)
<canvas id="mainIllustration" class="" width="278" height="659" style="" title="" tabindex="-1">
Illust...o_03.js (line 32)
<img id="mainIllustration" alt="main illustration" src="Img/Model01.png">
Illust...o_03.js (line 24)
<canvas id="mainIllustration" class="" width="278" height="659" style="" title="" tabindex="-1">

EDIT 1 : I am starting to wonder whether the issue is with Pixastic, the fact that its manipulating the image, which I need to return back to the global image variable (eMainIllustration). 编辑1 :我开始怀疑问题是否出在Pixastic上,即它在处理图像这一事实,我需要返回到全局图像变量(eMainIllustration)。 How would I achieve this? 我将如何实现?

ORIGINAL MESSAGE : 原始信息 :

All, 所有,

Im using pixastic library and am getting very confused by a seemingly very simple thing, global/ local variable. 我正在使用pixastic库,并且对看起来很简单的东西(全局/局部变量)感到非常困惑。

I have below two sets of code, one in which the variable is globally defined (ultimately what I want) & another in which the variable is locally defined. 我有以下两组代码,其中一组变量是全局定义的(最终是我想要的),另一组代码是局部定义的。

The first code below works fine, the variable (eMainIllustration) is locally defined and the function works well. 下面的第一个代码运行良好,变量(eMainIllustration)在本地定义,并且该函数运行良好。

    var eDisplayTableSlideState = false;
    function displayTableSlideIn()
    {
// eMainIllustration is locally defined
        var eMainIllustration = document.getElementById("mainIllustration");
        if (eDisplayTableSlideState === false)
        {
            $("#displayTable").animate ({top: "-=33px", left: "+=66px", width: "-=198px", height: "-=48px"}, 750, "swing",function()
            {
                Pixastic.process (eMainIllustration,"blurfast", {amount: 0.1});
            return (eDisplayTableSlideState = true);
        } else
        {
            $("#displayTable").animate ({top: "+=33px", left: "-=66px", width: "+=198px", height: "+=48px"}, 500, "swing",function()
            {
                Pixastic.revert(eMainIllustration);
            });
            return (eDisplayTableSlideState = false);
        }
    }

However, when I set the variable (eMainIllustration) globally, the first if statement of the function works, however the pixastic.revert code in the 'else' part of the function does not. 但是,当我全局设置变量(eMainIllustration)时,该函数的第一个if语句有效,但是该函数“ else”部分中的pixastic.revert代码无效。 Why? 为什么?

// eMainIllustration is locally defined
var eMainIllustration = document.getElementById("mainIllustration");
    var eDisplayTableSlideState = false;
    function displayTableSlideIn()
    {
        if (eDisplayTableSlideState === false)
        {
            $("#displayTable").animate ({top: "-=33px", left: "+=66px", width: "-=198px", height: "-=48px"}, 750, "swing",function()
            {
                Pixastic.process (eMainIllustration,"blurfast", {amount: 0.1});
            return (eDisplayTableSlideState = true);
        } else
        {
            $("#displayTable").animate ({top: "+=33px", left: "-=66px", width: "+=198px", height: "+=48px"}, 500, "swing",function()
            {
                Pixastic.revert(eMainIllustration);
            });
            return (eDisplayTableSlideState = false);
        }
    }

I wonder if this is because I am not returning the state of the changed (eMainIllustration) via return in the first half (if) of the function. 我想知道这是否是因为我没有通过函数的前半部分(if)返回来返回更改后的状态(eMainIllustration)。 Is that correct? 那是对的吗? And how would I achieve this? 我将如何实现呢?

Best, 最好,

What happens if you alert or console.log the variable out inside the function? 如果您在函数内部发出警报或console.log变量,会发生什么情况? Had my issues with defining global variables using functions. 我在使用函数定义全局变量时遇到了问题。

Where i am going with this is that the DOM might not the ready. 我要去的地方是DOM可能还没有准备好。 Try the use of: 尝试使用:

if(window.addEventListener) {
    window.addEventListener('DOMContentLoaded', function() {
        //DOM is now ready. Set your global variables value here.
    });
} else {
    //If internet explorer, use attachEvent method.
}

This is probably due to the location of your Javascript on the page. 这可能是由于Javascript在页面上的位置。

If you have it at the top of your page then the mainIllustration dom element may not have loaded before the javascript is loaded. 如果您将其放在页面顶部,则在加载JavaScript之前可能尚未加载mainIllustration dom元素。

If you move it to just above the body tag then the variable will be populated as the dom will be fully loaded by the time the javascript loads. 如果将其移到body标签上方,则该变量将被填充,因为javascript加载时dom将完全加载。

Thus it works when inside the function as the dom is loaded before you call the function, but does not work if it is outside the function. 因此,它在函数内部时起作用,因为在调用函数之前已加载dom,但在函数外部时不起作用。

var eMainIllustration = document.getElementById("mainIllustration");

Edit 编辑

<script type="text/javascript">
    $(document).ready(function() {
        load("path_to_javasript");
     });
</script>

I figured this out. 我想通了。

In case anyone has any confusion of how Pixastic works, here is what my understanding, along with a solution to my problem. 如果有人对Pixastic的工作方式有任何困惑,这是我的理解,以及对我的问题的解决方案。

The pixastic blurfast (& blur) plugin 'revert function' merely reverts the element back to its original state. pixastic blurfast(&blur)插件的“还原功能”仅将元素还原回其原始状态。 In other words, you are not adding another effect but telling pixastic to cancel all the changes & replace the element with its original state. 换句话说,您不是要添加其他效果,而是要让pixastic取消所有更改并将元素替换为其原始状态。

Keep in mind that when a pixastic effect is added, the image is no longer an image but a HTML5 canvas element, which in this case, is being canceled and the original 'image' being put back in place. 请记住,添加像素效果时,该图像不再是图像,而是HTML5 canvas元素,在这种情况下,该HTML5 canvas元素被取消,原始的“图像”被放回原处。

The global variable & local variable problem with resolved by using the 'return' at the end of the function. 全局变量和局部变量问题可以通过在函数末尾使用“返回”来解决。 Actually, this part was not the actual problem, the problem was with the revert implementation I (wrongly) was trying to implement. 实际上,这不是真正的问题,问题出在我(错误地)试图实现的还原实现上。

Hope this helps someone. 希望这对某人有帮助。

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

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