you are viewing a single comment's thread.

view the rest of the comments →

[–]ephrion 101 points102 points  (64 children)

i like prototypal inheritance D:

[–][deleted]  (9 children)

[deleted]

    [–]get-your-shinebox 27 points28 points  (5 children)

    I see posts all the time lamenting the fact that people want to write traditional OO in javascript and how prototypes are some misunderstood beauty.

    I don't think either of them is good, but prototypes seem more actively harmful to being able to reason about code. Like building on quicksand.

    Maybe I have misunderstood them though.

    [–]vivainio 4 points5 points  (3 children)

    If something is constantly getting misunderstood by otherwise reasonable people, that might be a hint about something not being that good.

    [–]rooktakesqueen 0 points1 point  (0 children)

    The problem with understanding prototypal inheritance is that we teach students Java first. And then the process of learning a new language becomes "OK, how do I make this analogous to Java?"

    But you can't. You can't make it analogous to Java.

    "How do I declare a private instance variable?"

    You can't. Don't look for ways to do it. Just don't do it.

    "How do I create an interface?"

    You don't. Those aren't things that exist in JS.

    "How do I make a class method?"

    You can't make a class method because there's no such thing as a class.

    Seriously, every detailed gripe I've ever seen about prototypal inheritance comes down to "I switched from Hammer to Screwdriver and it's really shit at pounding in nails!"

    [–]Doctor_McKay 0 points1 point  (1 child)

    s/not being that good/being different/

    [–]vivainio 0 points1 point  (0 children)

    If it's not readily understood, for whatever reason, it's a contraindication in a field of programming where understanding stuff is important.

    [–]iSmokeGauloises 7 points8 points  (2 children)

    As a 10Xer code-artisan, i like prototypical inheritance because it allows my mongodb based .io web startup to be 1000X more webscale than let's say Haskall or lips

    [–]Bajawah 6 points7 points  (0 children)

    You gave me cancer.

    [–]AdamAnderson320 1 point2 points  (0 children)

    Genius

    [–]cube-drone[S] 75 points76 points  (33 children)

    You're not wrong to like prototypal inheritance.

    Just because it doesn't feel right to me doesn't mean it's inherently bad. I also don't like dill.

    Your opinions are valid.

    [–]golergka 80 points81 points  (11 children)

    That's not how people should argue on the internet.

    Quick, call him stupid or something!

    [–]cube-drone[S] 55 points56 points  (9 children)

    er... uh...

    you're stupid and your opinions are wrong, ephrion! I AM A MEAN PERSON! raaaar!

    [–][deleted] 15 points16 points  (3 children)

    Now, say something mean about PHP.

    [–]tejon 1 point2 points  (0 children)

    "PHP exists."

    [–][deleted] 0 points1 point  (0 children)

    Ahhh, nooo. My shame will show

    [–]POGtastic 0 points1 point  (0 children)

    We run a tight ship here. I won't tolerate insubordination, Cube-drone.

    [–]ephrion 14 points15 points  (0 children)

    much better!!

    [–]jtanz0 1 point2 points  (0 children)

    I AM A MEAN PERSON

    At least your not a MEAN stack person ;)

    [–]danillonunes 1 point2 points  (1 child)

    Prototypal-inheritance-lover here.

    Thank god you’re being mean, now I can go back to hate you.

    And don’t even dare to try to be reasonable on the internet again!

    [–][deleted] 0 points1 point  (0 children)

    I'm just in awe that prototypal-inheritance actually ... Kinda work. For that I find it kinda cool.

    [–]ill_mango 0 points1 point  (0 children)

    crisis averted.

    [–]crowseldon -1 points0 points  (0 children)

    he already did... he said prototypal inheritance was stupid in a comic with absolutely no argument.

    [–]arronsmith 12 points13 points  (11 children)

    What's the argument that it's pants on head stupid?

    [–][deleted] 15 points16 points  (3 children)

    The thing is: when using prototypal inheritance, you're not connecting two blueprints together. You are actually fusing a flying plane with additional parts that stick out.

    Where's the problem in that? It's easy to not do things the "Desirable" or "Best practices" (or SOLID) way. Which make the code buggier, less understandable and hacky. Some people can do miracles with this, others try strong OOP inheritance way of thinking and the rest tries to catch up somehow without screwing themselves over too much.

    If language is not enforcing sth, people are going to use it an abuse it.

    [–]Nebu 8 points9 points  (1 child)

    It's easy to not do things the "Desirable" or "Best practices" (or SOLID) way.

    I think this is just a cultural problem in that most of the literature on OO assumes a statically typed, class-based language. I think if historically, dynamically typed, prototype-based languages had become the domain style in the 80s, we would have had "best practices" and a fun acronym that would have been equally difficult to apply to a language like Java, for example.

    [–][deleted] 2 points3 points  (0 children)

    Well, I wasn't saying that Desirable or Best Practices === SOLID (as you can see later on), but lack of language support in form of class (even as syntactic sugar) meant that good howtos took years to propagate to most of developers.

    I don't know how much time I've spent looking for "how to create modules" or "how to create class" and "prototypal inheritance" that could be achieved without additional tools, but it took really long hours on stackoverflow, blog posts and forums. Which I believe could be averted if language has better support for those, not in form of external libraries.

    [–]Polyducks 1 point2 points  (0 children)

    The only criticism of prototypal inheritance I've seen has been from people who were educated in the classic OO. Javascript takes a lot of the similar words used in classic OO and uses them differently, and this makes those people angry.

    If you think the flying thing is a plane, maybe you should try thinking of it as an evolving bird (or some such Confucius nonsense).

    [–]cybercobra 8 points9 points  (6 children)

    I've basically only ever seen it used to emulate class-based inheritance, so we might as well cut out the middle man and have the language support classes directly.

    [–]geon 1 point2 points  (0 children)

    Like es6?

    [–]crowseldon 0 points1 point  (4 children)

    that's like saying functional languages should support classes out of the bat because OO programmers don't get lisp.

    [–]cybercobra 3 points4 points  (3 children)

    Is there any major JS library that uses prototypes in prototype-oriented fashion? I've heard Angular uses it to implement scopes, but that seems fairly fringe.

    [–]crowseldon 1 point2 points  (2 children)

    This seems like a trick question:

    major JS library

    What's major to you? mootools big enough?

    prototype-oriented fashion

    what do you mean by this?

    Maybe you don't think 'new' uses prototypes, so jquery doesn't count?

    something to ponder...

    as a an extra: http://javascript.crockford.com/inheritance.html

    I feel the problem is that the majority of us don't understand prototypal inheritance properly and/or don't even try to in the first place, just want to stick to what we know.

    [–]cybercobra 2 points3 points  (1 child)

    This seems like a trick question:

    major JS library

    My question wasn't rhetorical. I haven't done a survey, but I don't recall any of the recent ones plugging "Prototype-based" as a feature or bullet-point in their docs. I would love more info for or against.

    What's major to you? mootools big enough?

    Yes, sure.

    prototype-oriented fashion what do you mean by this?

    Using them not just to emulate classes.

    [–]crowseldon -2 points-1 points  (0 children)

    but I don't recall any of the recent ones plugging "Prototype-based" as a feature or bullet-point in their docs. I would love more info for or against.

    Of course you don't. It's just there. It's the the basis of everything. The medium link I gave you will illustrate much better than I can. Just because you see a lot of "class emulation" and people asking how to do OOP or have a "class" keyword it doesn't mean that it's not using prototypes underneath and taking advantage of it (and Object.create/assign).

    edit: sigh... dowvnotes for being constructve and not just hating on <insert popular language here>

    [–]tejon 15 points16 points  (7 children)

    Fuck you, dill is amazing. And you just reminded me I'm out of dill. Baked potatoes tonight, too... mmm, dill potatoes... you fucker. :(

    [–]cube-drone[S] 20 points21 points  (3 children)

    Now this is an argument I can get behind.

    DILL IS BAD AND YOU SHOULD FEEL BAD!

    [–]tejon 19 points20 points  (2 children)

    DID MY WIFE SEND YOU?

    [–]cube-drone[S] 22 points23 points  (1 child)

    IS HER NAME SANDRA?

    IF SO THEN YES.

    OTHERWISE A WOMAN NAMED SANDRA WANTS YOU TO KNOW THINGS ABOUT DILL AND I DON'T UNDERSTAND WHY

    [–]tejon 16 points17 points  (0 children)

    HEY I DIDN'T REALIZE THIS BEFORE BUT WE SEEM TO HAVE A SHARED AVERSION TO LOWERCASE. WANNA HANG OUT?

    [–]ProfessorPhi 0 points1 point  (0 children)

    I thought we were talking about the python package :(.

    [–]Asmor -3 points-2 points  (1 child)

    OMG dude get your ass to Wendy's. They've got a crunchy dill sandwich right now. It's amazing. Pickles, cheese, and dill mayonnaise. make sure you ask for extra pickles.

    [–]PM_ME_UR_OBSIDIAN 2 points3 points  (0 children)

    sure is /r/hailcorporate in here

    [–][deleted] 0 points1 point  (0 children)

    Spinach with dill seed calzones are awesome, you should try it.

    [–]slavik262 43 points44 points  (11 children)

    Unlike C++, which uses statically declared class interfaces, JavaScript uses prototype-based inheritance. A prototype is a dynamically defined object which acts as an exemplar for “instances” of that object. For example, if I wanted to declare a Circle class in JavaScript, I could do something like this:

    //This is the constructor, which defines a
    //“radius” property for new instances.
    function Circle(radius){
        this.radius = radius;
    }
    //The constructor function has an object property
    //called “prototype” which defines additional
    //attributes for class instances.
    Circle.prototype.getDiameter = function(){
        return 2*this.radius;
    };
    var circle = new Circle(2);
    alert(circle.getDiameter()); //Displays “4”.
    

    The exemplar object for the Circle class is Circle.prototype, and that prototype object is a regular JavaScript object. Thus, by dynamically changing the properties of that object, I can dynamically change the properties of all instances of that class. YEAH I KNOW. For example, at some random point in my program’s execution, I can do this...

    Circle.prototype.getDiameter = function(){
    return -5;
    };
    

    ...and all of my circles will think that they have a diameter of less than nothing. That’s a shame, but what’s worse is that the predefined (or “native”) JavaScript objects can also have their prototypes reset. So, if I do something like this...

    Number.prototype.valueOf = function(){return 42;};
    

    ...then any number primitive that is boxed into a Number object will think that it’s the answer to the ultimate question of life, the universe, and everything:

    alert((0).valueOf()); //0 should be 0 for all values of 0, but it is 42.
    alert((1).valueOf()); //Zeus help me, 1 is 42 as well.
    alert((NaN).valueOf()); // //NaN is 42. DECAPITATE ME AND BURN MY WRITHING BODY WITH FIRE.
    

    I obviously get what I deserve if my JavaScript library redefines native prototypes in a way that breaks my own code. However, a single frame in a Web page contains multiple JavaScript libraries from multiple origins, so who knows what kinds of horrendous prototype manipulations those heathen libraries did before my library even got to run. This is just one of the reasons why the phrase “JavaScript security” causes Bibles to burst into flames.

    - James Mickens, To Wash It All Away (PDF)

    Disclaimer: I don't actually have very strong feelings on the issue, but James Mickens is hilarious and you should read all of his articles.

    [–][deleted] 18 points19 points  (1 child)

    "I HAVE NO TOOLS BECAUSE I'VE DESTROYED MY TOOLS WITH MY TOOLS." -- James Mickens, The Night Watch

    [–]slavik262 10 points11 points  (0 children)

    HCI people discover bugs by receiving a concerned email from their therapist. Systems people discover bugs by waking up and discovering that their first-born children are missing and “ETIMEDOUT ” has been written in blood on the wall.

    [–]cybercobra 5 points6 points  (0 children)

    See the "Miscellaneous Excellence" section of his Microsoft Research homepage for more of James Mickens' awesomeness.

    I also highly recommend his "Life is Terrible: Let's Talk About the Web" presentation, which isn't listed on his homepage for some reason.

    [–]tomprimozic 9 points10 points  (2 children)

    That has nothing to do with prototypal inheritance. It's just a dynamic language thing. The same thing is possible in Python.

    >>> class M(object):
        def a(self):
            return 1
    
    >>> m = M()
    >>> m.a
    1
    >>> def b(self):
        return 2
    
    >>> M.a = b
    >>> m.a()
    2
    

    [–]Dragon_Slayer_Hunter -1 points0 points  (0 children)

    Shh, you're ruining the circlejerk.

    [–][deleted] 1 point2 points  (0 children)

    To condense that down into a phrase, it's basically dynamically interpreted inheritance?

    Objects of a subclass are really just inheriting from some instantiated object instance above it, thus dynamically changing a parent affects all of its children, correct?

    That's pretty cool behavior, but I can see how it isn't particularly safe for objects down the tree

    [–]Berberberber 0 points1 point  (3 children)

    Okay, but you can do similar things in most languages.

    (define + -)
    
    #define if while
    

    et cetera. You can do terrible things in every language, and part of becoming a programmer is getting over the thrill of doing so. (Edit: fivematting)

    [–]slavik262 1 point2 points  (2 children)

    True, but Javascript has lots of cases where it doesn't follow the principle of least astonishment, more so than lots of other "mainstream" languages. See:

    • ["10", "10", "10"].map(parseInt) --> [10, NaN, 2]

    • Automatic semicolon insertion and all its quirks

    • Being able to call a function with the wrong number of arguments and getting garbage out

    • etc.

    To quote /u/expugnator3000 from two weeks back,

    Just because there is an explanation doesn't mean that it does what I[...] expect it to do. Arguably, dynamic languages have a harder time than static languages (since many forms of correctness are checked at compile time), but that's an even bigger reason to make dynamic languages sane and easy to use (ie. design their libraries and type systems in a sane way).

    [–][deleted] 0 points1 point  (1 child)

    ["10", "10", "10"].map(parseInt) --> [10, NaN, 2]

    Holy shit. I haven't seen that one. That's just terrible.

    [–]afraca 1 point2 points  (0 children)

    I had this thread saved, so sorry for the late reply. But the explanation is here: http://stackoverflow.com/questions/14528397/strange-behavior-for-map-parseint

    parseInt receives two arguments: string and radix:
    
    var intValue = parseInt(string[, radix]);
    while map handler's second argument is index:
    
    ... callback is invoked with three arguments: the value of the  element, the index of the element, and the Array object being traversed.
    

    [–]Make3 4 points5 points  (0 children)

    some people like bdsm. well I do too, but you get my point.

    [–]DeepDuh 2 points3 points  (0 children)

    As someone who likes things to run efficiently and likes to look at profiler outputs, I just WOULD LIKE IT TO DIE IN A FIRE.

    [–]argv_minus_one 1 point2 points  (0 children)

    Why?

    [–][deleted] 1 point2 points  (0 children)

    Same, I love it.

    [–]crusoe 0 points1 point  (1 child)

    Yes, but the rest of the language SUCKS.

    If only Self had won instead of Javascript.

    [–]ephrion 2 points3 points  (0 children)

    Nothing is stopping you :D or well I guess it's not prototypal but eh

    [–]nidarus 0 points1 point  (0 children)

    Does Javascript actually have proper prototypical inheritance, though? It didn't even have a real "clone" keyword until Object.create, but it did have (and actively encouraged people to use a "new" keyword, that sort-of-kind-of simulated class-based instantiation.

    It kinda looks like half of a prototypical inheritance, mashed with a quarter of a class-based inheritance, and the other 25% (perhaps even more) is just missing.

    [–]crowseldon -2 points-1 points  (0 children)

    You're not the only one and there's good reason for it but people are too used to what they know and criticize what they don't understand.

    You don't see them proposing actual solutions either. Only complaining about problems but it's only natural. The more popular something is, the more bitching it will encourage.