Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm not. See above, "class free object oriented programming". You don't need prototypical inheritance to have inheritance in JavaScript. Crockford gives examples. You don't need the "class" keyword. All it's going to do is steer people in the wrong direction.


Can you give an example of inheritance without prototypes in javascript?


from D.C.

    function base(spec) {
        var that = {},
            member,
            method = function() {
                // spec, member, method
            };
        that.method = method;
        return that;
    }

    function derived(spec) {
        var that = base(spec),
            member,
            method = function() {
                // spec, member, method
            };
        that.method = method;
        return that;
    }
I'd suggest you watch the talk linked above for this stuff in context.


That is not inheritance. `instanceof` doesn't work. There would be no way to know that an object from derive dis of the type of base.

That's also very memory inefficient. The method function gets made for every instance. With prototypes there would be only 1 function in memory. This stuff matters at scale.


Watch the talk.

You've got a gig of ram in your pocket. Memory doesn't matter for 99.99999% of apps. The memory taken by your objects isn't where your memory goes. It goes to images and other big stuff. A few extra bytes per object isn't going to kill you. Optimize for programmer time not memory, especially not in memory on things that don't matter.

Why are you using "instanceof". The whole point of inheritance is you shouldn't have to ask. If you are asking your doing it wrong


and then how do you implement super with your system? because that's the point of prototypes,the fact that you're not overloading methods but you have access to the prototype chain.much harder to do with your method.


You shouldn't need access to the prototype chain. Accessing that chain is an anti-pattern.


no it isn't,that's how you implement "super" in javascript.


Check out Daniel X. Moore's `{SUPER: SYSTEM}`. http://stackoverflow.com/a/3731253




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: