1

正在乱搞,发现这对一些东西很方便,但如果你不得不问我现在到底是什么,我将无法告诉你,所以想知道社区是否可以帮助识别下列的...

 var MyStuff = {

    STATS: {
            SHOTS:0,
            TRIES:0,
            HIGHESTSCORE:0,
            LIVESLOST:0
    },

    defaultLevel: 0,

    players: [
            {
                    name: 'Chuck',
                    surname: 'Norris',
                    punchline: 'Chuck Norris can set ants on fire with a magnifying glass, At night.',
                    dateCreated: '10/03/2011'
            },
            {
                    name: 'Mr',
                    surname: 'T',
                    punchline: 'I pity the fool who drinks soy milk.',
                    dateCreated: '10/03/2011'
            }
    ],

    startGame: function() {

            alert("You shouldn't have come back, " + this.players[0].surname);
            alert("" + this.players[1].punchline);
            this.STATS.SHOTS = 0;
            this.STATS.LIVESLOST = 1000000000000;
            var smiles = this.STATS.LIVESLOST;
            //TODO - More stuff
    }
}

var KaPow = MyStuff;

用法:

    KaPow.startGame();
    alert("Starting Level: " + KaPow.defaultLevel);
    alert("Player 1: " + KaPow.players[0].name + " " + KaPow.players[0].surname);
    alert("Player 2: " + KaPow.players[1].name + " " + KaPow.players[1].surname);
    alert("Score: " + KaPow.STATS.LIVESLOST);
4

2 回答 2

2

{} 定义的 JavaScript 对象;[] 定义的 JavaScript 数组;

将对象和数组相互嵌套;

将函数定义为对象的一部分;

于 2011-03-10T14:11:08.657 回答
1

您已经创建了一个名为 MyStuff 的对象(您也已将其分配给 KaPow)。

它有一堆属性(STATS、dificultyLevel、Players)和一个函数(startGame)。其中一些属性本身就是对象(如 STATS 和 Players)等等。例如,STATS 有自己的属性(SHOTS、SCORE、TRIES、LIVESLOST)

函数 startgame 可以对对象的属性进行操作,因为它在对象的范围内(即 this.players[0])。

于 2011-03-10T14:23:21.820 回答