Class.isClass

I think the point here is that having a class.isClass() function that didn't recognize normal functions as classes would be very similar to having a Function.isFunction() that only recognized arrow functions as functions, and returned false for anything else. This would just be confusing and inaccurate.

The "old way" for classes is just as obsolete as the "old way" for functions. Neither is deprecated, and both are still widely in use today. There's use cases for all of these. So, we must recognize both kinds of functions as functions, and we must recognize both kinds of classes as classes.

calm down ;), I didn't want to hurt your feelings ;)
let me ask you a short question:
If you had to start a project today, would you do it with the es6 syntax or the old way?

Of course I’d use the new syntax - but I’d expect to be handed normal functions too, because “other code” exists.

it is not necessary to look good with God and with the devil ;)

Well, the two options are:

  • We treat old-style classes for what they are - classes. es6 classes and old-style classes should be completely interchangeable, as that's how es6 classes were designed to be. I should be able to call a function that takes a class as an argument and provide it with any valid javascript class.
  • We deprecate old-style classes and discourage their use, that way the only "real" classes that are left are es6 classes.

We can't just start designing APIs that don't function properly with old-style classes without deprecating them. There's really no in-between option here. And Javascript does not deprecate lightly.

yes, i agree with you,
but, again, my proporsal is to add the Class.isClass, and it is resolved inside the "magic" of the browser, maybe these kids can make it compatible with es6 or the old way funcion, its not our field.

But, that's the issue. In the old-style of classes, you don't make a class, you make a function that's intended to be used as a class. The users of the function "new" it and treat it as a class. Nowhere in the code do you specifically say that "This is a class".

e.g. This is a valid class:

function MyClass() {}

In fact, in a very real sense, all functions are valid classes, we just choose not to use "new" on them to instantiate them. Many functions would behave oddly if we attempted to do so, some will throw an error, etc.

This is why @claudepache's suggestion to just check if something is a function makes a lot of sense. This is also why it's impossible for Javascript to distinguish functions from classes - because functions are classes.

One would use the newer, more concise syntax of course. But one needs to recognise that it's just that, a syntax, and still the same old constructor+prototype=class pattern. There is no semantic difference, and there shouldn't be.

Here, I'll respond to you in DM so we can keep this thread on-track

1 Like

I disagree. The protocol is actually the cleanest solution to your problem.

1 Like