简体   繁体   English

全局变量在事件监听器中变得未定义-javascript

[英]Global variable becoming undefined from within event listener - javascript

I have a function that creates a new image and gives it an event listener, which needs to access a global variable. 我有一个创建新图像并为其提供事件侦听器的函数,该函数需要访问全局变量。 The global variable (_currentSection) is defined until it gets to the declaration of the event listener, when it then becomes undefined. 定义全局变量(_currentSection),直到到达事件侦听器的声明为止,然后变为未定义状态。 Here is the function: 这是函数:

function relMouseCoords(event) {
    var totalOffsetX = 0;
    var totalOffsetY = 0;
    var canvasX = 0;
    var canvasY = 0;
    var currentElement = this;

    do {
        totalOffsetX += currentElement.offsetLeft - currentElement.scrollLeft;
        totalOffsetY += currentElement.offsetTop - currentElement.scrollTop;
    } while (currentElement = currentElement.offsetParent)

    canvasX = event.pageX - totalOffsetX;
    canvasY = event.pageY - totalOffsetY;

    //alert( 'x: ' + canvasX + '         y: ' + canvasY);

    for (var i = 0; i < _currentSection.allHotSpots.length; i++) {
        if (canvasX > _currentSection.allHotSpots[i].topLeft[0] &&
            canvasX < _currentSection.allHotSpots[i].bottomRight[0] &&
            canvasY > _currentSection.allHotSpots[i].topLeft[1] &&
            canvasY < _currentSection.allHotSpots[i].bottomRight[1]) {
            //alert( 'x: ' + canvasX + '         y: ' + canvasY);
            if (_currentSection.allHotSpots[i].isPicZoom === "false") {
                appendHotSpot(_currentSection.allHotSpots[i], _currentSection.thePicture, _context, true);
            } else {
                picZoomCode = _currentSection.allHotSpots[i].myPicZoom;
                var img = new Image();
                img.onload = function() {
                    _context.drawImage(img, 0, 0, _canvas.width, _canvas.height);
                }
                img.src = "data:image/jpeg;base64," + _currentSection.allHotSpots[i].picture;

                img.addEventListener('load', function() { 
                    if (_currentSection.allHotSpots[i].allHotSpots) {
                        for (var j = 0; j < _currentSection.allHotSpots[i].allHotSpots.length; j++) {
                            appendHotSpot(_currentSection.allHotSpots[i].allHotSpots[j], _currentSection.allHotSpots[i].thePicture, _context, false)
                        }
                    }
                }, false);
            }
        }
    }
}
HTMLCanvasElement.prototype.relMouseCoords = relMouseCoords;

So within img.addEventListener I need access to _currentSection, but it only just goes undefined in img.addEventListener. 因此,在img.addEventListener中,我需要访问_currentSection,但它只是在img.addEventListener中未定义。 How do i keep it as being defined? 我如何保持它的定义?

尝试将变量放在var currentElement = this之上;

Something similar to item 8 in this post is going on I suspect: 我怀疑与这篇文章中的第8项相似的事情正在发生:

What is the scope of variables in JavaScript? JavaScript中变量的范围是什么?

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

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