简体   繁体   English

这个变量类型叫什么?

[英]What is this variable type called?

I was looking though the CivClicker game code and I found that he created variables with little "sub variables" inside it. 我在浏览CivClicker游戏代码时发现,他创建的变量中几乎没有“子变量”。 It doesn't look like an array to me. 在我看来,它看起来像不是一个数组。

An example of the variable initialization: 变量初始化的示例:

// Initialise Data
var food = {
    name:'food',
    total:0,
    increment:1,
    specialchance:0.1
},
wood = {
    name:'wood',
    total:0,
    increment:1,
    specialchance:0.1
},
stone = {
    name:'stone',
    total:0,
    increment:1,
    specialchance:0.1
},
skins = {
    name:'skins',
    total:0,
},

He later calls the variables using: 他随后使用以下命令调用变量:

food.total++;

And so on. 等等。 If anyone has any information on this type of variable then I will be very greatful :) 如果有人对这种类型的变量有任何了解,那么我将非常感激:)

These are JavaScript objects. 这些是JavaScript对象。 The { .. } syntax is known as literal notation and is one way of creating an object. { .. }语法称为文字表示法,是创建对象的一种方法。 The attributes between the braces are properties, or methods if the value is a function. 花括号之间的属性是属性,如果值是一个函数,则是方法。

The objects are created using the shorthand var syntax, which is the same as repeating var : 使用缩写var语法创建对象,这与重复var相同:

var a = {}, b = {};
// same as
var a = {};
var b = {};

There are other ways of creating objects and setting properties, such as: 还有其他创建对象和设置属性的方法,例如:

var food  = new Object();
food.total = 0;
food['name'] = 'food';

You can also instantiate a function to create an object, whereby the function acts like a class. 您还可以实例化一个函数来创建一个对象,由此该函数就像一个类。

function Food {
    this.total = 0;
    this.name = '';
}

var f = new Food();
f.total = 5;
f.name = 'abc';

MDN Working with objects is a good resource that covers this. MDN处理对象是涵盖此方面的好资源。

console.log(typeof {test: 0});

会给你“对象”

您发布的代码显示4个对象,在这种情况下,属性total是一个数字。

food here is 这里的食物是

{
    name:'food',
    total:0,
    increment:1,
    specialchance:0.1
}

wood is 木是

{
    name:'wood',
    total:0,
    increment:1,
    specialchance:0.1
}

In JavaScript multiple variables can be initialized in this way. 在JavaScript中,可以通过这种方式初始化多个变量。

More info here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#Examples 此处的更多信息-https: //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/var#Examples

Edit: The comments in this answer gives some benefits of doing so. 编辑:此答案中的注释提供了这样做的一些好处。

These are objects. 这些是对象。

In programming you can define your own objects to store information about a particular thing. 在编程中,您可以定义自己的对象来存储有关特定事物的信息。

ie. 即。 if you need to store information about a car using code you might create the following object containing a set of paramaters or variables which describe the car. 如果您需要使用代码存储有关汽车的信息,则可以创建以下对象,其中包含一组描述汽车的参数或变量。

car = {
 color = "red",
 type = "sports car"
 wheels = 4
}

The purpose of this is to make organizing information easier, ie you can access the paramaters of the car by using car.color or car.wheels . 这样做的目的是使组织信息更加容易,即,您可以使用car.colorcar.wheels访问汽车的参数。

You can read more here: http://www.w3schools.com/js/js_objects.asp 您可以在此处了解更多信息: http : //www.w3schools.com/js/js_objects.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM