Giter VIP home page Giter VIP logo

obtotype's Introduction

obtotype

Experimenting with better use of prototypes

Demos: http://http://adrianmay.github.io/obtotype/

prototype.constructor is silly. If it must exist it should be set in the constructor so that prototypes can correctly have their own constructor. Given that, prototypes can be used as regular objects. But we're not going to construct them any more, rather, we'll clone or project them.

Cloning means making an exact and independent copy. It shares the __proto__ of the clonee and has own properties which are deep copies of the clonee's own properties. Even basic typed properties must be copied.

Projecting means creating an object with the projectee as the prototype. Basic typed properties are not copied but JS will make a property in the projection as soon as an attempt is made to change the value. Until then, updates to the projectee's property are seen in the projection. Object-valued properties must be deep copied to prevent changes to their object valued properties affecting the prototype. For instance:

p = { a:1, o: { b:2, c:3}}
d = Object.create(p);
d.a=10;
d.o.b = 20;
console.log(p.a + "," + p.o.b);

prints 1,20.

Sometimes we actually want a projection or clone to contain a reference to the same object that its original references and not a deep copy. Indeed the prototype itself is a case in point. To that end, properties with names beginning with $$ are not deep copied upon cloning or projecting. This distinction is only meaningful for object valued properties.

The uses of prototypes go way beyond inheritance. DarwinScript uses it for containment and we can also make an undo buffer this way. If prototypes know their referers we can also make a nice dirtying mechanism. Sample scripts will be written in the repo as examples.

Other prototype-based languages have more than one __proto__, e.g. Self has as many as you like and DarwinScript has one for inheritance and another for containment. In JS, we'd have to do this with multiplexers. A multiplexer starts off as an empty object and is then told to proxy for one delegate after another. It gives itself properties for everything in the delegate which explicitly delegate to them for read access. Write access shouldn't be delegated this far by the interpreter anyway.

obtotype's People

Contributors

adrianmay avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.