在Javascript中动态命名对象 [英] Dynamically naming an object in Javascript

查看:122
本文介绍了在Javascript中动态命名对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个二十一点游戏,并且在分手时遇到问题。最终我想创建一个关联数组,它具有每个拆分的总数和状态,将每个拆分视为自己的手对象。我遇到的问题之一是我正在尝试动态执行命名功能,所以每次调用 Split Function 时,它会根据编号创建名称手分裂的次数。

I am trying to create a Blackjack game and am having an issue when it comes to splitting the hands. Ultimately I want to create an associative array that has a total and a status for each split, treating each one as it's own hand object. One of the issues I'm having is with the naming functionality which I am trying to do dynamically so each time the Split Function is called, it creates the name according to the number of times the hand has been split.

关于我迄今为止所创造的内容的一些背景知识;卡片对象包含名称,套装和价值(即女王,俱乐部,10)。我有一个名为cardsInDeck的数组,它将所有卡片对象保存在一个洗牌的卡片中,这样每张卡片都可以随机拉出。从卡片中取出卡片时,会将值推入数组以计算值,并将名称和套装连接起来并添加到字符串中以填充HTML以显示页面上的卡片。

A bit of background on what I have created so far; A card object holds the name, suit and value (ie. Queen, Clubs, 10). I have an array called cardsInDeck that hold all the card objects in a deck which is shuffled so each card can be pulled randomly. When a card is pulled from the deck, the value is pushed into an array for calculating the value, and the name and suit are concatenated and added to a string to populate the HTML to show the cards on the page.

function drawOne() {
    let card = cardsInDeck.pop();
    return card;
}

var handNumber = 0;
var playerHand01 = [];
var p1, p2, d1;

function initialDealOut() {
    ++handNumber;
    p1 = drawOne();
    playerHand01.push(p1.value);
    ++playerCount;
    let p1n = p1.name + p1.suit;
    let p1c = "images/" + p1n + ".png";
    let p1Image = document.getElementById('player01');
    p1Image.src = p1c;
}

经销商第一张卡(d1)和玩家的第二张卡重复此操作(p2)并且运作良好。我想要做的是让 playerHand01 成为一个Hand对象的一部分,该对象位于 Player数组中所有的手,并将状态(动作,立场,胸围)与每个Hand对象相关联。

This is repeated for the dealers first card (d1) and the Player's second card (p2) and works well and good. What I would like to do is have the playerHand01 be part of a Hand object that is in a Player array that holds all the hands, and associate a status ("action", "stand", "bust") to each Hand object.

我试图用类似的东西来完成这个下面的代码:

I am trying to accomplish this with something like the code below:

var player = new Array();

function Hand(total, status) {
  this.total = total;
  this.status = status;
}

function Split() {
  ++handNumber;
  let handName = "playerHand0" + handNumber;
  let handTotal = "playerHandTotal0" + handNumber;
  handName = new Hand {
      handTotal: 0,
      status: "action"
    }
}

我是编程的新手,我知道可以做到,只要知道我接近它的方式就不太对了。

I am new to programming and I know it can be done, just know that the way I am approaching it isn't quite right.

任何建议都会受到赞赏。

Any recommendations would be appreciated.

推荐答案

我可能只是用数组存储手。他们的索引可以访问它们。如果你想给他们一个名字,那可能是手牌对象的一部分。

I would probably just use an array to store the hands. They'll be accessible by their index. If you want to give them a name, that could be part of the hand object.

let playerHands = [];
function Split() {
   ++handNumber;
   let newHand = new Hand ({
     handName: 'playerHand' + handNumber
     handTotal: 0, 
     status: 'action'
   });
   playerHands.push(newHand);
}

或者,如果你想使用一个对象(也许是因为我被误解了)您的要求),可以动态地为对象提供属性。有两种不同的语法可用于对象属性。以下两个都有相同的结果:

Alternatively, if you want to use an object (perhaps because i've misunderstood your requirements), it is possible to give an object a property dynamically. There are two different syntaxes that can be used for object properties. Both of the following have the same result:

let myObject = {};
myObject.test = 'hi';

let myObject = {};
myObject['test'] = 'hi';

您通常会使用前者,但如果您在运行时之前不知道名称,后者为您提供更大的灵活性,因为您可以使用任何字符串进行访例如,以下内容完全有效:

You'll usually use the former, but if you don't know the name until runtime, the latter gives you increased flexibility, because you can access using any string. So for example, the following is perfectly valid:

let player = {};
let handName = 'playerHand0' + handnumber;
player[handName] = new Hand();

这篇关于在Javascript中动态命名对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆