you are viewing a single comment's thread.

view the rest of the comments →

[–]package 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 1 fun -  (8 children)

Get with the times, old man:

class Counter {
    #count;
    constructor(init = 0) {
        this.#count = init;
    }
    inc() {
        return ++this.#count;
    }
    dec() {
        return --this.#count;
    }
    get count() {
        return this.#count;
    }
}

[–]fschmidt[S] 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 1 fun -  (7 children)

I see it's gotten worse. Thankfully I am working with 11-year-old code that isn't pure modern crap. Here is your disgusting modern crap failing:

class Counter {
    count = 0;
    test() {
        function inner() {
            return ++this.count;
        }
        return inner();
    }
}
let c = new Counter();
console.log(c.test());
console.log(c.test());

Fails with: Uncaught TypeError: Cannot read properties of undefined (reading 'count')

[–]package 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 1 fun -  (6 children)

I honestly have no idea what you are trying to convey here; if you write incorrect code in any language it will not give you the results you expect.

[–]fschmidt[S] 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 1 fun -  (5 children)

Why is it incorrect? This works:

function newCounter() {
    let counter = {};
    counter.count = 0;
    counter.test = function() {
        function inner() {
            return ++counter.count;
        }
        return inner();
    };
    return counter;
}
let c = newCounter();
console.log(c.test());
console.log(c.test());

The disgusting modern code is only incorrect because of the moronic obscure rules of disgusting modern features. A good language is based on a few simple concepts. Thankfully if one sticks to a small subset of old Javascript, one actually has a reasonable language. Of course everything produced by modern scum is pure shit and should never be used.

[–]package 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 1 fun -  (4 children)

Are you getting your panties in a bunch over the scoping of "this"? And acting like it's a modern issue? That nonsense has been around since the beginning, and is fixed by using arrow functions instead of "function" declarations, as arrow functions do not replace "this" within their own scope:

class Counter {
    count = 0;
    test() {
        const inner = () => {
            ++this.count;
        }
        return inner();
    }
}
let c = new Counter();
console.log(c.test());
console.log(c.test());

[–]fschmidt[S] 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 1 fun -  (3 children)

You are obviously complete modern scum and I regret sharing the same planet with you. What you are saying is just fucked up beyond belief. Why should arrow functions behave differently from regular functions, that just makes things more confusing. Yes the "this" problem is old because Javascript should never have had object-oriented features like this in the first place. But at least the scum who added "class" could have fixed this issue within a class since this was new. But no, of course they didn't do this since they are depraved modern scum. The correct solution is mine, to reject all this disgusting crap and build objects strictly based on closures and not class-based nonsense.

[–]package 2 insightful - 2 fun2 insightful - 1 fun3 insightful - 2 fun -  (2 children)

Bro lmao why are you so attached to old js? Literally everyone hated old js when it was the norm, exactly because there were tons of weird quirks regarding scope, lifetimes, and mutability. Arrow functions specifically are made for callbacks and control inversion scenarios where the existing scope is more relevant than the function's own scope, which just so happens to be 99.999% of scenarios where you make use of anonymous functions. Word of advice if you're this far behind in your js skills and hate modern features, definitely avoid reading about async/await and the Promise monad; your brain will probably melt.

[–][deleted]  (1 child)

[removed]

    [–]Vulptex[A] 1 insightful - 1 fun1 insightful - 0 fun2 insightful - 1 fun -  (0 children)

    This comment is also removed for advocating violence.