22

Interviewing a potential candidate...

Me: So, how would you rate your JavaScript knowledge?

Him: Pretty good, although I still don't really understand prototypal inheritance.

Me: Yeah, it's hard to understand something which doesn't exist.

...let the flaming begin 😈

Comments
  • 5
  • 0
    @wokeRoach @C0D4 there is no concept of "inheritance", classical, prototypal, or whatever, in JavaScript. Behaviour delegation, yes. Inheritance, no. Read the ECMAScript spec 😉
  • 14
    @wokeRoach
    I would love to find a rant saying: "I was in an interview and there was this jerk who said there's no prototypal inheritance in JS." :D
  • 1
    The concept is there but it's achieved using the prototype chain even if it's hidden. But the concept "inheritance" is there, even if as a higher not-100%-correct abstraction.

    You can think in classes and inheritance as in normal oop but it's generally better to avoid all of the added complexity (fake constructors, prototype objects etc) if you go the "behavior delegation" way.

    Or even better stick, to function composition + factory functions to avoid the this all together
  • 0
    @wokeRoach no, it's not a volley over semantics. JavaScript DOES NOT support inheritance. Just because you (or MDN) call it inheritance doesn't make it so. As I've already said, read the ECMAScript spec.
  • 0
    @wokeRoach @C0D4 please show me an example of inheritance in JavaScript.
  • 2
    @terminalterror

    Okay, I googled "ecmascript specification" and opened it here:

    https://ecma-international.org/ecma...

    4.2.1 Objects says:

    'Each constructor is a function that has a property named "prototype" that is used to implement prototype-based inheritance and shared properties.'

    Hmmmm...interesting.
  • 0
    @okkimus great, so you read an abstract which uses the word "inheritance". Did you read how to implement that?
  • 4
    @terminalterror

    There's literally A FUCKING MDN PAGE TO WALK YOU TROUGH THAT.

    I really hope you do recruiting just rarely and don't flame your applicants on a regular basis before you actually realise that you are wrong.
  • 0
    Okay, let's put this all into perspective.

    Inheritance requires the ability to make a copy of an object. Since you cannot do that in JavaScript, you cannot have inheritance.

    You can create a new object and link it's __proto__ to another object, which it will delegate behaviour to. However, this is not inheritance.

    People using the word "inheritance" because they were brought up on class-based OOP languages and need to perform mental acrobatics to somehow make JavaScript fit into their 'world view', does not mean shit.

    Sure, the abstract of the ECMAScript spec uses the word "inheritance" a few times, but read the implementation and it's clear that there is no inheriting of anything going on. It is simply not possible in JavaScript.

    Once again, I challenge anybody to provide me with an example of inheritance in JavaScript.

    Good luck 🙂
  • 0
    @okkimus

    Why do you care what an MDN page says?

    I recruit regularly...but we look for developers who understand (or want to understand) JavaScript 😂

    ...and I don't flame applicants...ever.

    As a side note, this candidate progressed to the second stage. It's fine to say that you don't understand "prototypal inheritance", but to claim to understand the prototype mechanism and still call it "inheritance" is just flat out wrong.
  • 6
    I love when I have an interview like this.

    At least I don’t walk away with any ambiguity. Walking away knowing for sure that I don’t want to work there.

    I always find it funny when interviewers fail to understand they are being judged on their behaviour in an interview just as much as the applicant.
  • 3
    And I thought that MDN maintained by Mozilla is the de facto and most reliable source for examples of JS/ES what ever you want to call it.

    Seems to be, that I don't understand anything about programming, so will you please enlighten me, what is inheritance in explicit way?
  • 2
    In oop when you instantiate a new class instance, behavior gets copied from the class (blueprint) to the created object.

    When inheriting from a class hierarchy (superclass -> subclass etc) behavior gets "flattened" and then copied over to the instance.

    Inheritance implies copying behavior and is not dynamic in most cases.

    The prototype mechanic in JavaScript does not copy behavior, it delegates work to the first object that can "respond" in the prototype chain. (Behavior isn't copied)
  • 0
    @terminalterror

    For me "inheritance" means that there's similar objects which share specific properties (methods or attributes). And those shared properties can be defined 'once' and then 'inherit' those properties somewhere else to improve code reuse.

    And there's two ways (that I'm aware of) to implement this:

    1) Class-based programming with superclasses and subclasses.

    2) Prototype-based programming, where you define prototypes and create/clone objects from already existing objects.

    Javascript or ECMAscript falls to the second category. Therefore you can create prototypes which define shared properties for objects. Then you can create objects which share the properties.
  • 1
    @terminalterror

    For me "inheritance" means that there's similar objects which share specific properties (methods or attributes). And those shared properties can be defined 'once' and then 'inherit' those properties somewhere else to improve code reuse.

    And there's two ways (that I'm aware of) to implement this:

    1) Class-based programming with superclasses and subclasses.

    2) Prototype-based programming, where you define prototypes and create/clone objects from already existing objects.

    Javascript or ECMAscript falls to the second category. Therefore you can create prototypes which define shared properties for objects. Then you can create objects which share the properties.
  • 0
    @terminalterror

    Sure I'm not telling you that JS has same kind of class-based inheritance compared with the likes of Java etc. But you can't deny that there's no inheritance in JS. The inheritance in the scope of JS is implemented in different way. I do know about prototype chaining and that function calls in objects get elevated upwards trough the prototype chain, until it finds the property that is called or the call arrives in the Object-class.
  • 0
  • 0
    @okkimus misrepresenting my argument by implying I believe you know nothing about programming doesn't make yours any stronger. I've asked you to provide an example, which you still haven't.

    Read @ObiSwagKenobi's post below which basically explains in more depth what I was trying to say.
  • 2
    "this candidate progressed to the second stage"

    - Ok, I'm happy for him/her.

    "I don't flame applicants"

    - In your rant you imply that you started to flame the candidate by saying "let the flaming begin". Although I see you live in Bristol so your English is better than mine -> therefore there might be slight semantic disconnect here for me.

    "Sure, the abstract of the ECMAScript spec uses the word "inheritance" a few times"

    - Well yes, its funny how many times it's used. Almost so many times that one might become to a conclusion that there is in fact inheritance in JS. Also it's funny that you point us to read the ECMAscript spec and it's actually against your claims.

    "Inheritance requires the ability to make a copy of an object."

    - And...you can do a copy of an object in JS. Here's a example:

    const obj1 = {

    a: "Foo",

    b: "Bar",

    fooBar() { return this.a + this.b }

    };

    const obj2 = Object.assign({}, obj1);

    // And now obj2 has all the same properties.
  • 1
    "I challenge anybody to provide me with an example of inheritance"

    - Here's a example: https://stackoverflow.com/a/...
  • 0
    @okkimus it might mean that to you, that doesn't mean that it's correct. In fact, it's just plain wrong. Inheritance has nothing to do with sharing anything. It is about copying.
  • 0
    @terminalterror '

    And I'm asking you to define it for me, cos I'm plain out wrong. I'm here to learn from others, so please explain this to me. What is inheritance? And give me reference where I can read it from reliable source.

    I'm providing reasoning for my arguments to you, so I wish you do the same for me.
  • 0
    @okkimus "let the flaming begin" was with regards to the likelyhood of me getting flamed for saying there's no inheritance.

    @okkimus in your example, you're not copying the object, you're creating a new empty object and applying obj1's enumerable properties.
  • 0
    @terminalterror

    Okay, you don't flame applicants. I misinterpreted it and I'm sorry about that.

    To your other point, how do you define a copy of an object if mine implementation isn't a copy of it?

    (Edit: add "of")
  • 1
    @okkimus this is an object. A class is not an object, it's just a "container" for behavior.

    You can somehow immitate a class wi
    th the class keyword but in reality that's just a function.

    Very interested conversation, it's a shame I can't get into more details since girlfriend is getting angry :<
  • 1
    @ObiSwagKenobi

    Yeah I follow you with that. I'm not implying that JS has class-based inheritance. But I'm also saying that there is inheritance, just not in the same way that traditional OOP languages.

    And you better take care of her for now :)
  • 1
  • 1
    Sorry...at a quiz night so no phones allowed. Will reply soon.
  • 1
    @terminalterror

    Heh no worries! :) I might be heading to bed soon, but I'll get back to you tomorrow I guess.

    Have a nice quiz night and kick some ass! :)
  • 1
    "I know enough about it to avoid it at all costs."
  • 0
  • 0
    This is going to be a great discussion
Add Comment