Giter Site home page Giter Site logo

private's Introduction

Private

Private: simple class and inheritance system, with easy private member and method support!

  • Private members
  • Private methods
  • Abstract methods
  • Final methods
  • Lots of fun

Take a look:

A = Private.class({
    getX: function(){
        return this.x;
    },
    setX: function(x){
        this.x = x;
    }
});

var a = new A();
a.setX(42);
console.log(a.x); //undefined
console.log(a.getX()); //42

Check out what you can do: https://github.com/zoltan-mihalyi/Private/blob/master/example.js

How does it work?

The main problem is to hide private members and methods. We use prototype chain to achieve this. If the private methods and members are "lower" in the prototype chain, we can't access them through a reference of "new A()" but a private method can see the public and private methods and members too. But how can the public methods access the private methods and members, like getters and setters? The key is the using of proxy methods. The object's prototype contains the proxy methods, which call the original methods with the private object as context, so each method can work with the private data, even the public ones.


                +---------+
                I Public  I Same for each object
                I proxy   I -----+
                I methods I      I
                +----^----+      I
                     I           I
           __proto__ I           I
                     I           I
                +----+----+      I
new A() ------> I Public  I      I
                I members I      I
                +----^----+      I
                     I           I
           __proto__ I           I
                     I           I
                +----+----+      I
                I Private I      I
                I members,I <----+ original methods are called with this context
                I methods I
                +---------+

How does it look like with inheritance assuming B is a superclass of A?


               +-----------+
               I Public B  I
               I proxy     I -------+ The methods of B use the private data of B as context
               I methods   I        I
               +-----^-----+        I
                     I              I
           __proto__ I              I
                     I              I
               +-----+-----+        I
               I Public A  I        I
               I proxy     I --+    I The methods of A use the private data of A as context
               I methods   I   I    I
               +-----^-----+   I    I
                     I         I    I
           __proto__ I         I    I
                     I         I    I
                +----+----+    I    I
new A() ------> I Public  I    I    I
                I members I    I    I
                +-^-----^-+    I    I
                 /       \     I    I
                /__proto__\    I    I
               /           \   I    I
         +----+----+   +----+--v-+  I
         I Private I   I Private I  I
         I data of I   I data of I  I    Different classes in the inheritance chain
         I B       I   I A       I  I    don't share their private data.
         +------^--+   +---------+  I
                I                   I
                +-------------------+

private's People

Contributors

zoltan-mihalyi avatar

Stargazers

Valentin Vichnal avatar

Watchers

James Cloos 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.