loading...

My newest JavaScript library: HotTea

calin92540842 profile image PDS OWNER CALIN (Calin Baenen) ・2 min read

URL: https://github.com/CalinZBaenen/HotTeaJS
So I've talked about how I personally make classes in JavaScript, and how I make private variables work in an older post;


But I decided I want to simplify the process for myself, and anyone who wants to try my method, so I made HotTea, an emulator-like library that adds pseudo-classes to JavaScript.

So, how does it work? Let's take a look together!

Overview

HotTea should provide you with all the (basic) tools to make a class-like class object. It even comes with example classes!

Creating a basic class

When creating a class in a language like Java, or Python, we usually have the option between:

public or static

in the our case, to make a class, we need to give it a "description".
What this means is we need to give it an object value to convert to a class. This is the most basic class you can have:


Person = Class({
    public: {
        name: "Linda"
    }
});

let Linda = Person();

so, what exactly is happening?
Well, with the let me explain first, as I mentioned above, this one big object that contains sub-objects is the "class description". It quite literally describes the class to the program. this is equivalent to Java:


class Person {
    public String name = "Linda";
}

Person Linda = new Person();

where public: {} acts as the public field for Java, similarly, we can use the static: {} descriptor to define any static variables that belongs to the constructor and that will be inherited by the next instance of that class.

Constructors

Ok, ok, I don't want to spoil this part, because I mean, it's really easy how to guess what the constructor looks like, atleast it's descriptor-name, it's syntax looks like constructor: function(){}, but, unlike most constructors, it has to return something, here, you have to return an object called a "variable-declarator", it's exactly what it sounds like, it will create variables last minute. Here's how we can use this effectively:


Person = Class({
    constructor: function Person(name="Calin",age=13,sex="male") {
        return {name: name, age: age, sex: sex};
    }
});

Linda = Person("Linda",29,"female");

this can make things quite complex, especially depending on how you manage these classes. This code is equivalent to Java:


class Person {
    public String name = "Calin";
    public int age = 13;
    public String sex = "male";
    public Person() {}
    public Person(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
}

Person Linda = Person();



I hope you decide to try it out, play with it, and look at the code, I'd love to see feedback for anything you want to see added to HotTea, class-variables, finals, and "super-statics" are all planned for a new version of HotTea.
Anyways, I'm excited to hear your feedback!

Thanks for reading!
Cheers!

Posted on Jun 2 by:

calin92540842 profile

PDS OWNER CALIN (Calin Baenen)

@calin92540842

I am a 13 (as of Oct 30 of 2019) yr/o developer (I have been developing mini-projects for 4, years now, since I was 9), who makes projects in languages like: Java, HTML, Python 3, JS, CSS, and C#.

Discussion

markdown guide
 
 

That isn't the same thing... really... I wanted to make these more like classes from languages like Python, Java, etc... these classes don't give you static variables, class variables, or private variables, all things those other languages provide. That's why I'm making this.