簡體   English   中英

變量隱式聲明和原型

[英]Variable Implicitly Declared and Prototypes

試着保持這個簡短。 使用phpstorm查看我的代碼並得到一些錯誤。

它說我的函數命名位置有一個“隱式聲明的變量”

function towngate10() {
    updateDisplay(locations[10].description);
    if (!gate10) {
        score = score+5;
        gate10 = true;
    }
    playerLocation = 10;
    displayScore();
    document.getElementById("goNorth").disabled=true;
    document.getElementById("goSouth").disabled=false;
    document.getElementById("goEast").disabled=true;
    document.getElementById("goWest").disabled=true;
}

另外,我只是想確保我正確地研究了我的原型只是一個樣本

全局數組:

var locations = [10];
locations[0] = new Location(0,"Intersection","This is where you awoke.");
locations[1] = new Location(1,"Cornfield","The cornfields expand for miles.");
locations[2] = new Location(2,"Lake","A large lake that is formed by a river flowing from the East.", new Item(1,"Fish","An old rotting fish."));
locations[3] = new Location(3,"Outside Cave","Entrance to the dark cave.");

位置功能:

function Location(id, name, description, item) {
    this.id = id;
    this.name = name;
    this.description = description;
    this.item = item;
    this.toString = function() {
        return "Location: " + this.name + " - " + this.description; 
    }
}

關於隱式聲明的變量:

if (!gate10) {
    score = score+5;
    gate10 = true;
}

playerLocation = 10;

得分,門和玩家位置被創建為“全局”變量。 Phpstorm將提醒您這一點。 除非它們是全局可訪問的,否則使用var聲明變量。 這將使變量僅局限於創建它的范圍:

if (!gate10) {
    var score = score+5;
    var gate10 = true;
}

var playerLocation = 10;

我建議你閱讀更多有關變量范圍的內容 如果未正確處理,全局變量可能會在您的安全性中留下空白。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM