markdown Screeps Javascript Basics

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Screeps Javascript Basics相关的知识,希望对你有一定的参考价值。

# Types of Things
#### String
Basically text.
```javascript
var screepName = 'screepalicious';
```

#### Array: `[ ]`
Basically a list or group of things, which can be of varying types.
```javascript
var myDumbArray = ['a', 'b', 'c', 1, 2, 3, screepName];
```
Each item in the array has an `index` number. Index numbers start at 0 (not 1), so if you wanted to reference the letter `a` (the first item in the array above), it would look like `myDumbArray[0]`. For example:
```js
console.log(myDumbArray[0]); // 'a'
/* or */
var someNewThing = myDumbArray[3]; // 1
/* or */
var someOtherNewThing = myDumbArray[6]; // 'screepalicious' (the value of screepName)
```
  
#### Object: `{ }`
It's...an object!
```js
var myDumbObject = {
  temperature: 68,
  humidity: 40,
  name: 'bob',
};
```
Objects are made up of key/value pairs (properties). In the object above, `temperature` would be the key, `68` would be the value.  You can access or reference properties like this:
```js
var howColdIsIt = myDumbObject.temperature; // 68
```

Objects can have many levels of nested values.
```javascript
var config = {
    roles: {
      harvesters: {
        population: 2,
        preferSource: 1,
      },
      upgraders: {
        population: 3,
        preferSource: 0,
      },
    }
};
```
If we wanted to get the value of population for the harvester role of the In the object above, it would be:
```js
var harvesterPop = config.roles.harvesters.population; // 2
```

# Logging to Console
Here's a few examples of ways you can use console log.
```js
var screepName = 'screepalicious';
var creep = {
  name: screepName,
  role: 'harvester',
};

console.log(screepName); // screepalicious
console.log('The creeps name is: ', screepName); // The creeps name is: screepalicious
console.log(`The creeps name is: ${screepName}`); // The creeps name is: screepalicious
console.log(JSON.stringify(creep)); // NOTE: objects need to be converted to strings first to log.
```

以上是关于markdown Screeps Javascript Basics的主要内容,如果未能解决你的问题,请参考以下文章

程序员专属游戏 Screeps:使用 JS/TS 代码控制自己的殖民地

牛x的JavaScript编辑器你知道几个

推荐几个编程游戏

12 个最佳的免费学习编程的游戏网站

分析 TypeScript Node.js 应用程序

一些适合程序员玩的游戏