Functional JavaScript: Writing a Simple Command-line Game (Part 2)

Functional JavaScript: Writing a Simple Command-line Game (Part 2)

Functional JavaScript: Writing a Simple Command-line Game (Part 2) - In the first section, we defined a ‘person’ object. Using JavaScript and a functional object model we create the first part of a simple command-line adventure game in JavaScript (Part 2). Functional Patterns. Start with basic JS skills and learn a new way.

In the first section, we defined a ‘person’ object; we cheated a little by embedding the notion of a weapon in this object. We also sidestepped the question regarding whether the weapon was an item or not. In this part, we will derive the item and weapon using object composition.

Firstly, we define an item object in a classical object-oriented language; this would be the base class.

const item = function( n ) {
    const _name = n;
    function name() {
        return _name;
    }
    return { name, isItem : true };
}

This tiny object holds the name of the thing and also sets an isItem flag to true. We can then compose the item with the weapon :

const weapon = function( n, s ) {
    const _item = item( n );
    const _strength = s;

    function strength() {
        return _strength;
    }
    return { ..._item, strength, isWeapon : true }
}

Walking through this more slowly,

On the first line, we have an instance of the item object. Initializing the ‘base’ object is performed by passing the name to its constructor. We then declare a constant variable ‘strength’ for the weapon and a helper function that returns the strength. The penultimate line of the function composes the item with the new methods and sets a flag to indicate that this is a weapon. We can use this as follows.

const myItem = item( "MyItem");
console.log( "Item " + myItem.name() )

And

const myWeapon = weapon( "MyWeapon", 100 );

if( myWeapon.isWeapon ) {
    console.log("Weapon " + myWeapon.name())
}

programming learning-to-code javascript

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

The Most Important Programming Lesson I Ever Learned

The most important lesson in programming is learning to debug code. Here is a list of my favorite debugging tips and tricks in JavaScript, Python, and more.

Learning to Code: How to Boost Up the Process?

Learning to Code: How to Boost Up the Process? I also often recommend different online and offline resources to my students to make their learning process easier, more effective, and faster. And in this post, I will share a few tips with you.

Learn JavaScript - Full Course for Beginners. DO NOT MISS!!!

In this video tutorial, we'll learn Learn JavaScript - Full Course for Beginners. This complete 134-part JavaScript tutorial for beginners will teach you everything you need to know to get started with the JavaScript programming language. DO NOT MISS!!!

Learning JavaScript: for Loops

In this article I’m going to cover the different forms of the for loop that are available in JavaScript. There is the general for loop, the for..in loop, and the for..of loop. I will describe how each loop works and when is the right time to use each loop type. I am leaving out the Array.forEach loop as it is specialized for arrays and requires some knowledge of functions I haven’t covered yet.

13 Free/Low-Cost Sites to Supercharge Your Programming Self-Education

Although we still talk about programming as a standalone career, the dominance of technology in our lives makes it clear that coding is much more than a career path. In my opinion, computer science is more than a college major or a high-paid job; it’s a skill, essential for thriving in a modern-day economy. Whether you work in healthcare, marketing, business, or other fields, you will see more coding and have to deal with a growing number of technologies throughout your entire life.