在这段 javascript 中发现了哪些元素/概念?
正在闲逛,发现这对某些东西很方便,但如果你必须问我现在到底是什么,我无法告诉你,所以想知道社区是否可以帮助识别元素和概念以下...
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);
Was messing around and found this to be handy for some stuff but if ya had to ask me what exactly is what right now, I wouldn't be able to tell you, so was wondering if the community could help identify elements and concepts in the following...
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;
Usage:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由 {} 定义的 JavaScript 对象;
JavaScript 数组由 [] 定义;
将对象和数组嵌套在彼此内部;
将函数定义为对象的一部分;
JavaScript objects as defined by the {};
JavaScript arrays as defined by the [];
Nesting objects and arrays inside each other;
Defining functions as part of an object;
您创建了一个名为 MyStuff 的对象(您也将其分配给 KaPow)。
它有一堆属性(STATS、difficultyLevel、Players)和一个函数(startGame)。其中一些属性本身就是对象(如统计数据和玩家)等等。例如,STATS 有自己的属性(SHOTS、SCORE、TRIES、LIVESLOST)
函数 startgame 可以对对象的属性进行操作,因为它在对象的范围内(即 this.players[0])。
You've made an object called MyStuff (which you've also assigned to KaPow).
It's got a bunch of properties (STATS, dificultyLevel, Players), and a function (startGame). Some of those properties are themselves objects (like STATS and Players) and so on and so forth. STATS, for example, has its own properties (SHOTS, SCORE, TRIES, LIVESLOST)
The function, startgame, can operate on the object's properties since it is within the scope of the object (i.e. this.players[0]).