27 points · by octosphere · 7y ago · 47 comments
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 it's unfortunate that classes now have more syntax conveniences than that for building prototypal object hierarchies. I find `Object.create(...)` and `Object.assign(...)` unclear to read and clunky.
As another aside: is there any reason to bother with JS constructors and "functional" OOP? I just use `Object.[create|assign](...)`, done.
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++;
}
}And available now as: https://babeljs.io/docs/en/babel-plugin-proposal-class-prope...
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.
Do people complain about differences between C++, Java, C# classes?
static isDog (animal) {
return Object.getPrototypeOf(animal).constructor.name === this.name;
}
Yes- static isDog (animal) {
return animal instanceof Dog;
} static isDog (animal) {
return Object.getPrototypeOf(animal).constructor === this;
}
Definitely no need for the `.name` check—that weakens it enormously, especially in the presence of minification.Alternatively, the autobind decorator could help - though I’ve never used that myself.
> JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax does not introduce a new object-oriented inheritance model to JavaScript.
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
I found the ES6 classes to be very helpful in conceptualizing stuff. When I started playing with them I’ve built myself a demo https://codepen.io/ronilan/pen/PObzew as a clone of same done previously in Ruby. Then I found classes to be very useful for bigger “vanilla” projects (https://github.com/ronilan/BlockLike)
I’d call them “syntactical vitamins”.
I would argue that, more often than not, it is, and it becomes less healthy the more of it you have.
Javascript, for all its faults, is syntactically a simple language and it should have been kept that way.
My favourite subtle distinction is that methods are not constructors—this is true of object shorthand as well.
You were taught that these two were equivalent?
var x = { foo() {} };
var x = { foo: function() {} };
Try this on each: new x.foo
The second definition works, but the first fails because x.foo is not a constructor. This has actually caught me, in a codebase with an old-style Class() creator (and no, `class` is not quite a suitable replacement there, at this stage, for reasons I won’t go into). `Class({ init: function(){} })` would work fine, but `Class({ init() {} })` would yield an uninstantiable class—except that the result was being fed through Bublé, which reinstated the `: function` for old browsers’ sake. So it was only when I removed Bublé from the pipeline that it blew up!(To demonstrate this same point in ES6 classes, `new (new class { foo() {} }).foo` will fail.)
function foo (a, b) {
compared to the more common: function foo(a, b) {It always seemed like an odd choice to me that would make people less likely to adopt it.
> But this isn't a real web standard!
> Of course it's not! The style laid out here is not affiliated with any official web standards groups, which is why this repo is called standard/standard and not ECMA/standard.
> The word "standard" has more meanings than just "web standard" :-)
I’m not a fan of the naming, but I could also be biased because I disagree with some of the formatting rules.
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 while we're at it.
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.