简体   繁体   English

Javascript关联数组到整数?

[英]Javascript Associative Array to Integer?

I'm working on a hunk of software for an internship and I've been tasked to add functionality to a system that builds a table of Award Levels based on existing Prizes and numbers of Cards. 我正在为实习工作开发大量软件,我的任务是向系统添加功能,该系统根据现有奖品和纸牌数量构建奖品级别表。

I'm getting into the function for adding rows and I find an extremely strange pair of declarations. 我正在进入添加行的功能,并且发现一对极其奇怪的声明。 I'm going to post the function itself lest lack of context inhibit answers. 我将发布函数本身,以免缺少上下文抑制答案。 I've searched for various combinations of "cast","array" and "number" but nothing seemed to be related to this practice. 我搜索了“ cast”,“ array”和“ number”的各种组合,但似乎与这种做法无关。

this.addNewTableRow = function(){
    var request = new XMLHttpRequest();
    awardLevel = this.length + 1;
    request.onreadystatechange = function(){
        if (request.readyState == 4 && request.status == 200){
            if(request.responseText != -1){
                var tableBody = document.getElementById('award-body');
                var sqlId = parseInt(JSON.parse(request.responseText));

                var prevSelector = document.getElementById('level-select-'+self.length);
                var prevLevel = 0;
                if(prevSelector != null){
                    prevLevel = parseInt(prevSelector.value);
                }

                var minCardQuantity = prevLevel + 1;
                var maxCardQuantity = minCardQuantity + 100;

                var awardLevel = {
                    id: sqlId,
                    awardId: 0,
                    certificateId: 0,
                    awardLvl: self.length + 1,
                    cardQuantity: minCardQuantity
                };

                self.changeLevelSelect(self.length + 1, minCardQuantity);

                var row = self.getRow(awardLevel, minCardQuantity, maxCardQuantity);

                tableBody.appendChild(row);
                self.awards[length] = awardLevel;
                self.length++;
            }
        }
    }
    request.open('GET', '_insert_award_lvl.php?level=' + awardLevel, true);
    request.send();
    location.reload();  
}

The behavior that has me puzzled is the treatment of AwardLevel 让我感到困惑的行为是对AwardLevel的处理

It's modified before it is declared, which even with a vague understanding of hoisting I don't think should work. 在声明之前已对其进行了修改,即使对提升的理解不够明确,我也不认为该方法可行。 Further the early assignment operator appears to be assigning the variable to an Int whereas it is assigned and used as an associative array later on. 此外,早期赋值运算符似乎是将变量赋给Int,而稍后它被赋值并用作关联数组。

The overall code has a lot of unexpected and confusing behavior and I'm already inexperienced with Javascript. 整个代码有很多意想不到的混乱行为,而我对Javascript并不熟悉。

The external awardLevel variable isn't "modified before it is declared" : it's never declared. 外部awardLevel变量不会“在声明之前进行修改”:从未声明过。 The other declaration is only valid for the function in which it is declared (and it shadows the external one). 另一个声明仅对其声明的功能有效(并且遮盖了外部声明)。

This means : 这意味着 :

  • you have two different variables with same name and different semantics (one is an integer, one is an object) 您有两个具有相同名称和语义的不同变量(一个是整数,一个是对象)
  • one of them is never declared (it's global) and is shadowed in the internal scope by the declared one 其中一个永远不会被声明(它是全局的),并且在内部范围中被声明的一个阴影
  • the fact awardLevel has a property named awardLvl doesn't make it better 事实上awardLevel有一个名为awardLvl的属性并不能使它变得更好
  • this is a bad careless code 这是一个不好的粗心代码

A way to make all this slightly less confusing would be this : 一种使所有这些稍微减少混乱的方法是:

// awardLevel = this.length + 1; remove that line
request.onreadystatechange = function(){
    // don't change which is here
}
request.open('GET', '_insert_award_lvl.php?level=' + (this.length + 1), true);

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

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