philipwalton / mozart Goto Github PK
View Code? Open in Web Editor NEWA full-featured, classical inheritance library for Node.js and the browser.
License: MIT License
A full-featured, classical inheritance library for Node.js and the browser.
License: MIT License
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.
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?
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?
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.
Please add a non-minified version of the library to the repository to allow myself and others the ability to contribute.
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.
Seems like it is possible.
Maybe the implementation would have to avoid super
because it is static:
Any thoughts?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.