-1

Exists follow text in ECMAScript specification, sounds like:

Although ECMAScript objects are not inherently class-based, it is often convenient to define class-like abstractions based upon a common pattern of constructor functions, prototype objects, and methods. The ECMAScript built-in objects themselves follow such a class-like pattern. Beginning with ECMAScript 2015, the ECMAScript language includes syntactic class definitions that permit programmers to concisely define objects that conform to the same class-like abstraction pattern used by the built-in objects.

From all of these I interested in:

The ECMAScript built-in objects themselves follow such a class-like pattern

What does it mean?


I think the class-like pattern means that the programming style is similar to the programming style using classes.

For example:

Accessing the property: obj.property; or calling the method: obj.method();

If it is not it, explain why?

MaximPro
  • 563
  • 8
  • 21
  • https://stackoverflow.com/questions/816071/prototype-based-vs-class-based-inheritance – yqlim Jun 20 '19 at 09:16
  • Consider an array. Though you can create it using special syntax `const arr = [1, 2, 3]` it is still constructed using `Array` constructor. Which has a prototype with a bunch of array methods `map, filter, etc`. This is pretty much true for every built-in objects `RegExp, Map, Set, Function, Object, Promise, etc` So you could say that built-in objects follow the pattern. – Yury Tarabanko Jun 20 '19 at 09:43
  • Do you know what the "class-like pattern" is or does that need explaining as well? – Bergi Jun 20 '19 at 10:57
  • @Bergi I have suggestions, but it will not be superfluous, and explain it – MaximPro Jun 20 '19 at 11:02
  • @MaximPro OK I'll wait for your edit – Bergi Jun 20 '19 at 11:11
  • @Bergi a little, but added – MaximPro Jun 20 '19 at 16:04

1 Answers1

1

I think the "class-like pattern" means that the programming style is similar to the programming style using classes.

Yes, exactly. See class-based programming on Wikipedia.

Accessing a property or calling a method

No, that's basic object-orientation. It doesn't need prototypes or classes for that.

What makes a class is the instantiation of alike objects (with the new operator), the initialisation of instance data members (in the constructor), and the shared implementation of methods (in the class body).

Additionally, most class systems also feature inheritance with a superclass hierarchy. JavaScript achieves that (and the sharing of methods) via prototype inheritance.

You should be able to easily see how all the ECMAScript built-in objects follow this pattern.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Hmm. Regarding property access and method invocation. Why this does not apply to the `class-like pattern`. Can you give an example when accessing a property and calling a method does not relate to this style? What makes the class clear. When you said about the superclass, I correctly understand that inheritance goes like this: `class base {};` `class bar extends base {};` - in this example, base is a superclass, right? If this is the case then, as applied to ECMAScript, the Object class (more precisely, the `constructor`) is a superclass for functions, arrays, numbers, strings, and so on. – MaximPro Jun 21 '19 at 06:19
  • I once again looked at the specification and found such a sentence in the definition of the Object constructor (it occurs a lot where it is, almost in all built-in objects): `is designed to be subclassable. It may be used as the value of an extends clause of a class definition.` Then I'm confused. If no Object constructor is a superclass then what? – MaximPro Jun 21 '19 at 06:36
  • @MaximPro Property access and method invocation are features of objects, it's a general OOP pattern. Although classes of course do create objects, they're not necessary for this - in object-oriented languages that are not class-based there are many other ways to create objects. – Bergi Jun 21 '19 at 06:38
  • @MaximPro Regarding inheritance, yes that's what `extends` does. Most things inherit from `Object`, `TypeError` inherits from `Error`, `Uint8Array` inherits from `TypedArray`, and so on. You can try to map out the JS builtin hierarchy as an exercise. – Bergi Jun 21 '19 at 06:40
  • @MaximPro "designed to be subclassable" refers to the fact that the builtins could not be subclassed before ES6 introduced [its subclassing feature](https://stackoverflow.com/a/32458960/1048572). I'm not sure what you're asking by "*If no Object constructor is a superclass*". – Bergi Jun 21 '19 at 06:45
  • About trying to build a hierarchy of some things. I understand how it will look and what exactly it will expand. Especially using the developer console - it will not cause difficulties. *I'm not sure what you're asking by "If no Object constructor is a superclass"* - I meant that if the Object constructor is not a superclass, then what is the superclass in this case? I didn't really understand your link and the words: `builtins could not be subclassed before ES6 introduced its subclassing feature` – MaximPro Jun 21 '19 at 07:02
  • @MaximPro In which case? `Object` is a superclass of a few things, and not the superclass of other things. Regarding ES6: [It was not possible to subclass most builtins, like `Array`, before](http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/). – Bergi Jun 21 '19 at 08:08
  • I feel that I do not correctly understand that there is a superclass. Suppose an example: `class main {};` `class sub extends main {};` `class end extends sub {};` In this example, is `class main` a superclass for `class end`? We just create the end class using the `sub class`, but not the `class main`. And is the `class sub` a superclass for the `class end`? Well, then if `Object` is a superclass for some things, but not for others, then I don’t understand this line for `Object` as I wrote above: `is designed to be subclassable. It can be used to extend the clauses of the class definition.` – MaximPro Jun 21 '19 at 10:00
  • @MaximPro `Object`, `main`, and `sub` are all superclasses of `end`, but only `sub` is the direct superclass of `end` - afaik at least. But this really depends on your exact definition of the term "superclass" and has absolutely nothing to do with your original question. – Bergi Jun 21 '19 at 12:25
  • Ok, now it's clearer. But I'm still interested in: `Object is a superclass of a few things, and not the superclass of other things.` What did you mean by `...and not the superclass of other things`(for Object)? Is there a superclass for `Object`? As for this sentence: `is designed to be subclassable. It can be used to extend the clauses of the class definition.` There was some confusion due to the fact that I did not understand the word "subclassable". As I understand it, this word in this sentence means the following meaning: `from which one can derive or inherit.` Right? – MaximPro Jun 22 '19 at 17:33
  • @MaximPro Yes, you understood "subclassable" right. And regarding things that `Object` is not a superclass of, you already found the first: `Object` (inheritance hierarchies are acyclic). Others are: things that are not classes, `class extends null {…}`, or classes from other realms. – Bergi Jun 22 '19 at 17:50
  • I almost understood everything. Is there a superclass for `Object` class? This is the last thing I would like to know. – MaximPro Jun 22 '19 at 18:56
  • Is it mistake? `class extends null {…}`? You did not specify class for extending. – MaximPro Jun 22 '19 at 20:14
  • @MaximPro You should be able to algorithmically determine the superclass of a class now. Try to apply this algorithm to `Object` and see what the result is. Hint: When I explicitly write `null`, I mean it. You may check the spec to see how this case is handled. – Bergi Jun 22 '19 at 21:03
  • What application of the `Object` algorithm are we talking about? About the hint, too, did not understand. I could check it in the specification if I understood what it was about. – MaximPro Jun 22 '19 at 23:46
  • I assume you are talking about this:`has a [[Prototype]] internal slot whose value is null.` in 19.1.3 – MaximPro Jun 23 '19 at 05:17
  • @MaximPro Yes, §19.1.3. shows the superclass of `Object`. – Bergi Jun 23 '19 at 10:09
  • I'm not sure what **null** is a superclass for `Object`. Since this is just a value like any other literal. Although you may have meant that there is no superclass for `Object`(Right?). I just had the ambiguity of your phrase. That is why I ask again. – MaximPro Jun 24 '19 at 02:25
  • Yes, `null` is not a class. As I said, the inheritance hierarchy is acyclic, so there must be something that doesn't have a superclass. – Bergi Jun 24 '19 at 04:22
  • Yeah, I understand you, it was nice to talk to you again. Now my question is completely clear to me. – MaximPro Jun 24 '19 at 04:34