我正在学习 OOP,并且我相信我已经精通在尝试使用函数检索值时遇到错误。但这一次我真的被困住了。尝试调用函数时,我不断收到“意外的令牌)id()
”placeCards()
appendChilds()
function GUI(player)
{
for (var i = 0, j = arguments.length; i <= j; i++)
{
var display = document.createElement("div");
id = function()
{
idElem = document.createElement("p");
idElem.appendChild(document.createTextNode(
player.name == "Player" ? "You" : "Casino"));
return idElem;
}
placeCards = function()
{
for (var card = 0, totalCards = player.hand.length;
card <= totalCards;
card++)
var cardElem = document.createElement("img");
setAttributes(cardElem,
{
"id" : (player.name == "Player" ? "player" : "casino")
+ "Card" + card,
"class" : "card",
"src" : "images/Cards/Back.png",
"alt" : "Back of a card",
"title" : "Back of a card"
});
return cardElem;
}
appendChilds(display, {id(), placeCards()});
table.appendChild(display);
}
}
player = new Player("Player");
GUI(player);
我为 appendChilds 构建的辅助函数如下:
function setAttributes(element, attributes)
{
for (var key in attributes) {
element.setAttribute(key, attributes[key]);
}
}
任何与手头问题相关的帮助或学习 OOP 的可靠资源将不胜感激。