0

Conventional wisdom seems to be that using JavaScript as a classical language is bad: JavaScript's pillars are object-oriented (prototypal inheritance) and functional, not classical, say all the experts (e.g., @eric-elliott, Douglas Crockford, etc.).

Yet so many of our APIs use constructors and require the use of new when instantiating an object. For example, if I want to fetch data with an AJAX call, I have to do something like let xhr = new XMLHttpRequest().

So, are we stuck with classical behaviour in JavaScript for now, until/unless APIs are updated? Or is there some way to excise all these new declarations from our code, perhaps something along the lines of API factory functions or Object.create(XMLHttpRequest.prototype)?

Derek Henderson
  • 9,388
  • 4
  • 42
  • 71
  • 1
    What does `new` have to do with prototypal vs classical? – Felix Kling Jul 17 '17 at 16:58
  • I'd ask @EricElliot to answer this for me, as he's more experienced, but I'll take a stab at it. @FelixKling, the `new` operator invokes a constructor to create an instance of a class: that's the very definition of classical inheritance, unless I've missed a big point. Prototypal inheritance is class-less. @AndrewLi, JS is not a class-based language, and `new` is one of the 'bad parts' of JS. – Derek Henderson Jul 17 '17 at 17:20
  • 3
    @DerekHenderson `new` creates an instance derived from the prototype, and initialises it by calling the `constructor` on it - the very definition of prototype inheritance. That we call the combination of a constructor and a prototype "class" does not matter, it does not do the same as in a class-based language. No, [new is a bad part](https://stackoverflow.com/q/383402/1048572) and Crockford's opinion is ignored by many other experts. – Bergi Jul 17 '17 at 17:55
  • @DerekHenderson: FWIW, here is a simplified (and approximate) implementation of `new`: `function my_new(F, ...args) { var obj = Object.create(F.prototype); F.apply(obj, args); return obj; }`. Sure, you can call `Object.create(...)` + a function for initialization yourself, but why? :) I also want to point out that *syntax* and actual *behavior* of a language are not necessarily related. E.g. having `class` syntax does not mean the language uses "classical" class concepts. – Felix Kling Jul 17 '17 at 19:12
  • I think we can agree that creating a new "instance"/object can require two parts: Inheritance (`Object.create(...)`) and initialization. You can do this easily via `Object.assign({x: 0, y: 0}, Object.create(point))`. But if you have 100 points, you'd have to write this 100 times. At this point you'd probably use a (factory) function to make it reusable. And in a way, that's exactly what a constructor is as well: A way to make the initialization logic reusable. Idk, maybe there is something I'm missing from the prototype vs classical debate, but in general it seems rather pointless to me. – Felix Kling Jul 17 '17 at 19:18
  • Maybe relevant: https://stackoverflow.com/questions/19633762/classical-inheritance-vs-protoypal-inheritance-in-javascript – Felix Kling Jul 17 '17 at 19:20
  • @FelixKling, technically, yes, all inheritance in JS is prototypal and there is no class inheritance. But Eich was told to make JS look like Java, so he gave us constructors and the `new` operator to emulate classical inheritance in JS. We're still using prototype delegation and object concatenation, but building a layer on top of that to work like class inheritance with all its associated problems and brittle architecture. If we're thinking in terms of "something is" instead of "something has", we're using classical inheritance patterns instead of prototypal inheritance patterns. – Derek Henderson Jul 17 '17 at 20:58
  • @FelixKling, (continued) apologies for being imprecise with terminology in my question, but this was the gist of what I was getting at: use of `new` with constructors is not the right way to create objects in JS. Rather than using `new` and constructor functions to emulate class, we should be favouring object composition. Yet so many of our APIs expose constructors instead of factory functions. I guess what I'm trying to find out is if there is a way to avoid using constructors when using APIs such as XMLHttpRequest or are we stuck with them? – Derek Henderson Jul 17 '17 at 21:01
  • @FelixKling, btw, here is a relevant link: https://medium.com/javascript-scene/common-misconceptions-about-inheritance-in-javascript-d5d9bab29b0a – Derek Henderson Jul 17 '17 at 21:02

1 Answers1

-1

Object.create does literally different thing - it is used to create class children. You can look more detailed here, and it seems that there's no such feature in ecmascript. Maybe there should be some hacks in implemented browsers, who knows

alexcleac
  • 29
  • 4