Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
newclass
Advanced tools
Readme
Create php-like classes and extend them (supports private, protected and public visibility).
###Beware
This document differs between classes and objects. Classes are created with var MyClass = newClass({...})
, objects are created with new MyClass()
, so the class is actually only the blueprint for the object.
Because i like the way how php (and most other languages) handles the visibility of properties and methods, so you can decide what the extending class shall be able to access. I searched for other solutions but did not find any good ones, they were either too complicated (in my eyes) or too concentrated on using the normal prototype way (so they were actually just a workaround or an abstraction).
I tried to be as close to the javascript-style as possible, but still provide an easy way to create classes that can be extended with ease.
There are a couple of examples in the examples
-dir that show you how things are done, but sure, here is a simple class:
var MyClass = newClass({
"private": {
"iAmPrivate": "Secret things no other class should know"
},
"protected": {
"number": null
},
"public": {
"__": function(number) { //"__construct" is also possible
this.protected.number = number;
},
"getNumber": function() {
return this.protected.number;
},
"showNumber": function() {
console.log(this.public.getNumber());
}
}
});
var myClass = new MyClass(13);
myClass.showNumber(); //13
You don't need to declare all visibilities, every single class-option is optional. So a class can actually be also an empty object:
var AnotherClass = newClass({});
this.parent
-object (like in php)####The class-options are saved as a public property of the prototype-object, why do you still call it private?
That's right, with some hacking around you can actually even change the private properties and methods of the class you want to extend (before you extend her), but hacking is always possible. For example you could also read the text file and parse the data out of that (and then create a new class with the changed data).
####Does extending work over multiple files (with require) or even over multiple modules?
Yes, both works because the class-options are saved as a public property in the prototype of the class-object (the thing newClass() returns).
####Aren't private, protected, public, extends and so one reserved words in javascript?
Yeah, they are, but as of ECMASCript 5, reserved words are allowed as properties of objects and can be accessed with and without scare-brackets and quotes. So this.protected.myVariable
is completely fine. :)
This software is licensed under the Mozilla Public License v. 2.0. For more information, read the file LICENSE
.
FAQs
Create php-like classes and extend them (supports private, protected and public visibility).
The npm package newclass receives a total of 1 weekly downloads. As such, newclass popularity was classified as not popular.
We found that newclass demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.