[英]Can't understand why this is showing as undefined
我開始構建一個簡單的文本冒險游戲,根本無法解決這個問題。 我在文本框中輸入“檢查表”。 檢查被剝離,只留下表,然后將其傳遞給檢查 function。 當我嘗試將 innerHTML 更改為 input.description 時,我得到了未定義。 然而,當我使用 table.description 它工作正常。
const text = document.querySelector('#text');
const userForm = document.querySelector('#user-form');
let userInput = document.querySelector('#user-input');
const table = {
description: 'Plates and silverware are on the table',
items: ['plates', 'silverware'],
itemDescrips: ['rotting food sits on the plates.', 'just silverware']
};
let inspect = function(input) {
//Returns undefined
text.innerHTML = input.description;
//Oddly, this works fine
//text.innerHTML = table.description
};
userInput.addEventListener('keydown', (e) => {
if (e.keyCode == 13) {
let command = userInput.value.split(' ');
if (command.includes('inspect')) {
//I've checked the type of this argument and it is a string.
inspect(command[1]);
}
userForm.reset();**strong text**
e.preventDefault();
}
});
你們讓我思考,並幫助我意識到我可以在檢查 function 中使用條件邏輯來完成這項工作。
let inspect = function(input) {
if (input == 'table') {
text.innerHTML = table.description;
}
};
您將一個簡單的文本傳遞給您的檢查function 並且此文本沒有名為 description 的屬性。 這就是為什么你得到一個未定義的回報。
僅將參數輸入分配給您的text.innerHTML工作正常。
let inspect = function(input) {
text.innerHTML = input;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.