Live data from Hacker News

ES6 Classes

javascriptjanuary.com

21–30 of 51 posts

Re: ES6 Classes

#21
post #16

I'm happy about the class syntax additions that came with ES6, especially the method definitions. It's increased my productivity (and happiness) when working with js.

My only complaint is having to manually bind methods in the constructor. I understand why it's necessary but it is tedious. I suppose the answer is to just use TypeScript but we are talking about ES6 here.

Re: ES6 Classes

#22
post #11
post #9

I think ES6 classes are the worst of all worlds. I know people from a Java background really want classes in their Javascript but this is not that. ES6 classes look like kind of sort of like a class but if you poke it at all you see lots of sharp edges and weird behaviours that make no sense unless you know what it's sugar for.

I think the point was to simplify the JS Prototype syntax to be more "normal-looking" and less confusing to devs, not some rich OOP model compared to true OOP languages.

> I think the point was to simplify the JS Prototype syntax to be more "normal-looking" and less confusing to devs

So they bolted on an incompatible thing that hides prototypes and is confusing to devs:

   class C {
      y = 10;

      // this fine. "methods" defined as class properties
      // have access to proper `this` due to how class properties work
      // and scoping rules
      x = () => if(this.y > 10) this.y = 10; 

      constructor() {
          // if you don't bind `this`, `method()` will not have access
          // to `this`
          this.method = this.method.bind(this)
      }

      method() {
         // will refer to a wrong `y` if `this` isn't bound
         // in the constructor
         this.y++; 
      }
   }

Re: ES6 Classes

#23
No-

    static isDog (animal) {
        return Object.getPrototypeOf(animal).constructor.name === this.name;
    }
Yes-

    static isDog (animal) {
        return animal instanceof Dog;
    }

Re: ES6 Classes

#24
post #12

Earlier quoted context omitted.

Unfortunately in the JS community you cannot say these things without being punished. If you do anything else than strict eslint with "airbnb settings", or default prettier settings, your head goes off! Don't ever say this to your JS team mate. It's a very rigid community if it comes to these futilities.

This is... puzzling. Where do you see these attitudes? Both ways are perfectly cromulent and it's fine for people to have opinions. In my years of JS dev the main thing I've seen from the community is "pick a style and stick to it. Be consistent." And that's pretty typical and sage advice of all programming. The reason to downvote parent is that it's bringing up a tired debate. We may as well discuss tabs vs. spaces…

> The reason to downvote parent is that it's bringing up a tired debate.

Do you mean that even when it is scientifically proved that your code is more readable when using more white space like in: console.log( "Hello world!" ); we are not even allowed to mention that because some random dudes decided we have to adhere to the "airbnb" settings?

I bring this up because I think it's pathetic, like many other things in the JS community. Tired debate or not, we should have freedom of thinking and speech please! Programming is also a very creative activity, we should not destroy that with these kind of silly rules that make it more of an administrative activity.

Re: ES6 Classes

#25
post #15
post #11

Earlier quoted context omitted.

I think the point was to simplify the JS Prototype syntax to be more "normal-looking" and less confusing to devs, not some rich OOP model compared to true OOP languages.

Javascript is a true OOP language. I'm all in favour of simplifying some of the syntax but why call it a "class"? That's only going to further confuse matters. I already have a hard enough time trying to teach Javascript's OOP model without adding to the confusion.

Why not? It's totally reasonable if you learn a language to get familiar with the concepts in it. Every language is different here and there and the 'class' describes well what it does: wrap things together that belong together.

Re: ES6 Classes

#26
post #16

I'm happy about the class syntax additions that came with ES6, especially the method definitions. It's increased my productivity (and happiness) when working with js.

My only complaint is having to manually bind methods in the constructor. I understand why it's necessary but it is tedious. I suppose the answer is to just use TypeScript but we are talking about ES6 here.

Have you looked at using arrow functions in class properties for this? They have some issues, but avoid all the tedious manual binding.

Alternatively, the autobind decorator could help - though I’ve never used that myself.

Re: ES6 Classes

#27
post #12

Earlier quoted context omitted.

Unfortunately in the JS community you cannot say these things without being punished. If you do anything else than strict eslint with "airbnb settings", or default prettier settings, your head goes off! Don't ever say this to your JS team mate. It's a very rigid community if it comes to these futilities.

This is... puzzling. Where do you see these attitudes? Both ways are perfectly cromulent and it's fine for people to have opinions. In my years of JS dev the main thing I've seen from the community is "pick a style and stick to it. Be consistent." And that's pretty typical and sage advice of all programming. The reason to downvote parent is that it's bringing up a tired debate. We may as well discuss tabs vs. spaces…

If I remember correctly it used to be the case that every JavaScript repo came with it's own eccentric style. I'm certainly guilty of that. Now -- when we have these style "standard" style guides -- we've sort of settled on 3 or 4 competing standards. None of them look particularly bad (or at least not worse then my personal style cerca 2012).

Don't get me wrong. We still engage in stupid bikeshedding about semi vs. no semi, tabs vs. spaces etc. but I feel overall we've embiggened our spirit when it comes to code style debates.

Re: ES6 Classes

#28
post #9

I think ES6 classes are the worst of all worlds. I know people from a Java background really want classes in their Javascript but this is not that. ES6 classes look like kind of sort of like a class but if you poke it at all you see lots of sharp edges and weird behaviours that make no sense unless you know what it's sugar for.

It's "javascript classes" why should it 1:1 copy Java or other languages? Just for the sake of people coming from other language background I don't see it justified.

Do people complain about differences between C++, Java, C# classes?

Re: ES6 Classes

#29
post #9

I think ES6 classes are the worst of all worlds. I know people from a Java background really want classes in their Javascript but this is not that. ES6 classes look like kind of sort of like a class but if you poke it at all you see lots of sharp edges and weird behaviours that make no sense unless you know what it's sugar for.

What do you like class in JavaScript to be?

Re: ES6 Classes

#30
post #9

I think ES6 classes are the worst of all worlds. I know people from a Java background really want classes in their Javascript but this is not that. ES6 classes look like kind of sort of like a class but if you poke it at all you see lots of sharp edges and weird behaviours that make no sense unless you know what it's sugar for.

It's "javascript classes" why should it 1:1 copy Java or other languages? Just for the sake of people coming from other language background I don't see it justified. Do people complain about differences between C++, Java, C# classes?

Because it uses a fundamentally different model to C++, Java and C# classes?
Post reply on HN