The 'this' Keyword

## Introduction

This page talks about how to access functions, properties, and lists within the list without mentioning the name of the list.

## Examples

### Simple Example

Have a look at this example:

```
personHeight = {100-200}
personEyeColor = {brown^3|blue|grey^0.2|green^0.3}
personDescription = The person is [personHeight]cm tall and has [personEyeColor] eyes.

```

There's nothing wrong with this example, but we could use a hierarchy to clean it up a bit and make it more "modular":

```
person
	height = {100-200}
	eyeColor = {brown^3|blue|grey^0.2|green^0.3}
	description = The person is [person.height]cm tall and has [person.eyeColor] eyes.

```

That's much nicer because now all the person-related things are all nested under one label: `person`. Now, instead of writing `[personDescription]`, we'd write `[person.description]`. This organisational "nesting" would be especially helpful if we had a fairly complex generator with lots of things to keep track of. Writing neat code allows us to create more complex generators with less effort.

### Using `this`

#### Single Hierarchy

But we can actually make one final tweak which makes `person` just a little bit neater and more self-contained:

```
person
	height = {100-200}
	eyeColor = {brown^3|blue|grey^0.2|green^0.3}
	description = The person is [this.height]cm tall and has [this.eyeColor] eyes.

```

Notice how we've written `this.height` instead of `person.height`? That's because the special `this` keyword always refers to the "parent" of the item that it is written in. In this case the "parent" of `description` is `person`, so writing `this` is the same as writing `person`.

If we use `this`, instead of `person`, then our code is more "modular" or "self-contained", because, for example, the `description` doesn't care what its parent is called - we could rename `person` to `human` and it would still do its job correctly without worrying about looking for all the times we mentioned `person` and changing them to `human`.

#### Deep Hierarchy

The `this` keyword is also useful when you've got a very "deep" hierarchy:

```
race
	humanoid
		elf
			height = {10-20}0cm
			description = It's an Elf that's about [this.height]cm tall.
			...
		orc
			...
				...
		...
	...

```

See how we can use `this.height` instead of `race.humanoid.elf.height`? It's neater and more self-contained.

So the `this` keyword allows us to access the parent of the current item, but what if we want to access the *parent's parent*? This is where we use the `getParent` property. While the `selectOne` property allows us to move *down* the hierarchy by selecting a random child, the `getParent` property allows us to move *up* the hierarchy by getting an item's parent.

```
cake
	flavor = {strawberry|dark chocolate|vanilla}
	description
		A tasty [this.getParent.flavor] cake.
		A [this.getParent.flavor] cake that is tasty.

```

See how that works? The `this` keyword always refers to the item's parent - `description`, in this case. We add `.getParent` after `this` because we want to get the parent of `description` (which is `cake`). Finally, we add `.flavor` because we want to get the flavor property of `cake`.

Now that you understand `this` and `.getParent` you can go up and down the hierarchy as you please! 🎉

One final tip: To get the name of a list, you can use `.getName`, and this can be "chained" with `.getParent` like so:

```
output
  [animal.selectOne.selectOne.description]

animal
	mammal
    mouse
      description = a [this.getName] is a type of [this.getParent.getName]
    kangaroo
      description = a [this.getName] is a type of [this.getParent.getName]
	reptile
    lizard
      description = a [this.getName] is a type of [this.getParent.getName]
    turtle
      description = a [this.getName] is a type of [this.getParent.getName]

```

So that'll output something like "a turtle is a type of reptile" as you can see in this example generator. That's probably not the neatest way to achieve it (due to lots of repetition), but it gets the point across :)