Code Monkey home page Code Monkey logo

Comments (3)

basarat avatar basarat commented on May 10, 2024

Stateful functions as described are totally unnecessary in JS (and TS by extension). Since it has first-class functions it can easily be achieved with a closure

Thanks 🌹. However you are saying "not needed" followed by "here is how to do them".

Also I do mention using various abstractions that wrap over a local variable. I didn't want to use the term closure to scare newbies 🌹. The pattern I present uses a closure too.

So comparison is between

A.)

const {called} = new class {
    count = 0;
    called = () => {
        this.count++;
        console.log(`Called : ${this.count}`);
    }
};

B.)

const counter = (() => {
  let count = 0;
  return () => {
    count += 1;
    console.log(`Called : ${count }`);
  };
})();

I prefer A 🌹 Also one could use namespace.

from typescript-book.

si13b avatar si13b commented on May 10, 2024

Yes, sorry, I didn't word that very well. They were two separate statements.

What I meant was the introduction of a "static" keyword would be unnecessary, as there's already a built-in feature capable of achieving the same thing and more.

I can understand that classes are easy to grasp for people coming from a OOP background. However, at run time they are actually functions, and they're an important concept to understand when working in a functional language.

As a suggestion, maybe mention them in passing if you want to avoid confusing the reader with a full example?

edit: spelling

from typescript-book.

basarat avatar basarat commented on May 10, 2024

and they're an important concept to understand when working in a functional language.

Agreed 🌹 I probably need to add a complete section on JS Fundamentals. But didn't want to add that to this book in particular.

from typescript-book.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.