Попытка передать числовые значения из одной функции в другую (значения, проверенные на числовое значение), но когда я получаю доступ к указанным значениям из другой функции, мне выдается ошибка «NaN». Попробовал следующий пример 6 (Advanced) из этого SO Какова область видимости переменных в JavaScript? , но он не работает для меня.
Как мне это исправить?
//global var accessed in other functions
var institution1Left;
var institution2Left;
var institution3Left;
var authorityLeft;
var inst1Left;
var inst2Left;
var inst3Left;
var authLeft;
function getOffset(el) {
var _x = 0;
var _y = 0;
while (el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) {
_x += el.offsetLeft - el.scrollLeft;
_y += el.offsetTop - el.scrollTop;
el = el.offsetParent;
}
return { top: _y, left: _x };
}
$(document).ready(function () {
calculateNode();
});
//val set here
function calculateNode() {
this.institution1Left = getOffset(document.getElementById('institution1')).left;
this.institution2Left = getOffset(document.getElementById('institution2')).left;
this.institution3Left = getOffset(document.getElementById('institution3')).left;
this.authorityLeft = getOffset(document.getElementById('authority')).left;
//change node Left positioning
this.inst1Left = institution1Left - 26;
this.inst2Left = institution2Left - 26;
this.inst3Left = institution3Left - 27;
this.authLeft = authorityLeft - 27;
}
function displayCipherIn1() {
//getting NaN error here
alert(
new calculateNode().inst1Left+
new calculateNode().inst2Left+
new calculateNode().inst3Left+
new calculateNode().authLeft)
}