Hacker News new | past | comments | ask | show | jobs | submit login
Prototypal Inheritance (2008) (crockford.com)
29 points by Tomte 17 days ago | hide | past | favorite | 13 comments



For anyone interested in prototypal inheritance you might checkout Io [1] and Lua [2]. Interestingly enough the "Object.create" function that Crockford suggests in 2008 was already how Io worked in 2002 with "Object clone".

[1] https://iolanguage.org/

[2] https://www.lua.org/


Very interesting to see this here!

I've been having a lot of conversations recently with people about LLMs and how a prototypal inheritance-like approach to working with them seems to be surprisingly effective. Especially for writing code.

Also, this quote jumped out at me: "Insights from the dynamic world can have application in the static."


Could you say a bit more about the link between prototypal inheritance and LLMs? Not obvious to me what it is.


Ahh yeah - so I've found prototypal inheritance (or what Hofstadter calls The Prototype Principle) is a useful way to think about what's going on with in-context learning and few shot prompting and ways to take it further.

If you provide a "prototype" to a model, you can operate on that prototype and create new instances or variations of it. This is useful for working with code by defining an abstraction or a class, and then having the LLM work with and modify it, generating new code that fits or inherits conceptually from that original prototype.


He more recently espoused what he calls "class-free object oriented programming," where you use closures for everything instead of prototypes. This uses more memory, but he says that it doesn't matter.

I do like writing Javascript that way, though:

    function Thing(otherThing) {
      const self = otherThing || {};
      self.method1 = function () { /* ... */ };
      // self. [...]
      return self;
    }

    function main() {
      // no `new`, no `this`
      const thing = Thing();
      // ...
    }


Lua has something similar called metatables [1]. I don't think crockford likes JS anymore [2] and seems to be hacking on C now [3].

[1]: https://www.lua.org/pil/13.1.html

[2]: https://www.youtube.com/watch?v=lc5Np9OqDHU

[3]: https://github.com/douglascrockford?tab=repositories


Just to be clear, this is what he actually says in that video:

> And it's still maybe for its field of application the best language in the world for doing that kind of stuff but that's not good enough. We should be moving on to the next generation of languages. It used to be that we'd get new computer languages about every generation. That started with Fortran and then C and C++ and Java and JavaScript and so on and then it kind of stopped. There are still people developing languages but nobody cares. One person can make a programming language, a really good one, but you can't get adoption for it.

This isn't Crockford becoming converted to the anti-JS crowd and hopping back to C, it's Crockford arguing that JavaScript shouldn't be the final iteration of programming languages. Presumably he believes that about C as well.

It looks like he's working on writing a new language, Misty, intended to (maybe) replace JavaScript:

https://www.crockford.com/misty/


So is this a more streamlined version of languages where classes are objects (a la Python/Ruby) but where any ole object can act as a class?

Because prototype chains feel a lot like MRO but without the facilities to support multiple inheritance (which can be good or bad depending on the programmer).


It's this thing https://en.wikipedia.org/wiki/Metaobject and how objects work in JavaScript. JS is not class-oriented, unlike Java.

This so called inheritance is best understood as linked-lists which "help track the method that acts upon this object". So this' not the OOP in Python (where you have C3 inheritance), nor the one in C++ where you have DFS inheritance, and does not even "support" multiple inheritance (https://en.wikipedia.org/wiki/Multiple_inheritance). I want to say is not inheritance, but... well it was billed as one, so you can think of it as one. But is in fact a peculiar way of doing method resolution.


But object in Python is the metaobject, right? Classes are instances of type, type is an instance of object.

Is the difference that one draws a line between classes and instances? So like in JS an object is a "subclass" of its parent in addition to being an instance of it because the line between them doesn't exist.

If in JS every object had .prototypes plural it seems like it could work with C3 linearization in exactly the same way.


There is no such thing as multiple inheritance in JS. You can do mixins, but is not the same thing as it is on object level. There are no classes in JS, but prototypes. So it’s really prototype oriented and very much Lisp-ish in all aspects.


What they describe there is not necessary. You can use the built-in Object.create function. The code shown at the bottom can be used in case you are using a old version of JavaScript that does not have Object.create.

Like they say, there are many benefits of prototype inheritance compared with class inheritance, and I agree with that.

I think that it is not a good idea to affect Object.prototype, and I think it might have been a bad idea even to put anything there at first when they invented JavaScript. (For this reason, sometimes Object.create(null) is used, which creates an object with no prototype.)


> What they describe there is not necessary. You can use the built-in Object.create function.

What they describe there was the inspiration for the existence of Object.create. This is a historical post from Crockford, the godfather of JS.




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

Search: