Hacker News new | ask | show | jobs
by cessor 2213 days ago
I like it. It gives you (f)actual private fields in python, as title and name can't be accessed via the instance variable of f.

That also works in JavaScript:

    function Fraction(a, b) {
        this.value = function () {
            return a / b; 
        }
    }

    const half = new Fraction(1, 2);
It feels weird to me, that classes were eventually introduced in JS.
2 comments

> It feels weird to me, that classes were eventually introduced in JS.

if you don't put value in the Fraction prototype, then value will be created each time Fraction is instanced.

But that's not why class where created. Class simplify writing classes, doing inheritance and introduce "super" static binding which wasn't possible before with functions.

Also, JS will now force you to use new when instanciating Fraction class as an object. You can't call it like a function.

It’s worth pointing out that classes in both of these languages implement another headline feature of OO: code reuse through prototypes and inheritance.