GithubHelp home page GithubHelp logo

mozart's People

Contributors

philipwalton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mozart's Issues

Protected methods are not protected.

The protectedToPrototypeMap is a singleton, and passing any instance into any protected key function will currently return any instance's protected members.

For example:

const Class = require('mozart')

const Animal = Class('Animal', function(proto, _, _protected) {
    proto.foo = function() {
        _(this).method()
    }
    _protected.method = function() { console.log('Animal protected method') }
})
const Dog = Animal.subclass('Dog', function(proto, _, _protected) {
    _protected.method = function() { _protected.super.method.call(this); console.log('Dog protected method') }
})

const UnrelatedClass = Class('UnrelatedClass', function(proto, _, _protected) {
  proto.someMethod = function() {
    let d = new Dog
    _(d).method() // Access Dog's protected stuff, it's not really protected.
  }
})

let u = new UnrelatedClass
u.someMethod()

An instanceof check might solve the problem.

Discrepancy in docs?

Hey there, I like this concept. It's the best protected/private class implementation. :D

There's something in the README that I'm unsure about. The first example accesses age without using the key, in the protected instance:

  _protected.allowedToVote = function() {
    return this.age > 18;
  };

However, in the second exampmle, the protected instance accesses crime with the key:

  _protected.allowedToVote = function() {
    return _(this).crime != 'felony'
      && _protected.super.allowedToVote.call(this);
  };

In both examples, the public prototype methods access those properties with the key.

This makes it seem like one of the examples is wrong because the usage is inconsistent. Are both examples correct?

If they are correct, how come in the first example the public prototype method accesses _(this).age while the protected prototype method accesses this.age, and in the second example crime is accessed as _(this).crime in both the public and protected prototype methods?

Actual state of this library

Hey,

I've read your article about private properties in JS and it leads me here.

According to readme:

It's not a finished product, it's a work in progress.

Time passed, article is quite old and your last contribution was done more than one year ago.

Do you have any new thoughts about solution which you proposed with mozart library?

API suggestion

I like what you've done, but I think the API can be improved.

Wouldn't it be more intuitive if we can define a class like this?

Class(Bar).extends(Foo).declare(function (prototype, _, _protected) {
     // define public, protected, private members here
});

Bar could either be a function or string, but Foo has to be a function.

Actually I'm working on my library that does similar things as yours. You can define a function as a class and the function will have a class object attached to it and by default every class inherits from a default class called AbstractObject (like in Java, although in Java it's the Object class).

However mine doesn't support private and protected members at the moment.

I think it would be perfect if I integrate your library with mine.

Non-minified version

Please add a non-minified version of the library to the repository to allow myself and others the ability to contribute.

Calling a public method from a protected method

It seems it's not possible to access public properties from a protected method because there is no way to get the instance of the public prototype.

For example:

var Foo = ctor("Foo", function (prototype, _, _protected) {
    Object.assign(prototype, {
        init: function (name) {
            this._name = name;
        },
        saySecret: function () {
            console.log(_(this).secret());
        },
        getName: function () {
            return this._name;
        },
        printName: function () {
            _(this).sayName();
        }
    });

    Object.assign(_protected, {
        secret: function () {
            return "This is a secret";
        },
        sayName: function () {
            console.log(this.getName()); // this doesn't work
            console.log(prototype.getName.call(this)); // this doesn't work either
        }
    });
});

var foo = new Foo("Foo");

foo.saySecret(); 
foo.printName(); // this fails

Although we can access the protected properties from anywhere using "_(this)", we can only access the public properties within the public context (inside the methods declared for the public prototype).

It would be nice to have a function like "_()" that gives back the instance of the public prototype by passing in the instance of the protected or private prototype.

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.