
Research
/Security News
Contagious Interview Campaign Escalates With 67 Malicious npm Packages and New Malware Loader
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
JS class pattern; supports multi-inheritance, static & instance methods, public/protected access...
classes.js
provides a more traditional object oriented programming pattern
than JavaScript's native prototype chaining.
In many cases, this is overkill and a more procedural or functional approach is actually more appropriate in JavaScript. However, in some cases it is really useful to have traditional OO features.
Only dependency is atom.js, which is itself small and is included as a submodule.
To run from command line using node.js:
npm install atom-js
node test.js // brief
node test.js -v // verbose
To run in a browser, open test.html
.
When defining a class, attach static methods to thisClass
, and instance
methods to thisInstance
. Methods are protected by default, meaning they are
available to subclasses.
classes.define('myclass', ['base'], function (thisClass, protoClass) {
thisClass.staticMethod = function () {
// Do something that doesn't require access to instance data.
// Optionally call protoClass.<method> to access methods of the base
// class(es).
return 'My Class is aw3s0m3!!1!';
};
thisClass.instance = function (thisInstance, protoInstance, expose) {
// Private methods and data are just defined inside the instance
// closure.
var foo = 'bar';
function privateMethod() {
return foo + ' baz';
}
thisInstance.protectedMethod = function () {
// Instance methods, since they are defined inside both the class
// closure and the instance closure, have access to all private,
// protected and public methods of both.
return thisClass.staticMethod() + ' ' + privateMethod();
};
thisInstance.publicMethod = function () {
// This function won't actually be exposed as part of the instance's
// public interface until expose() is called.
return 'Here you go: ' + thisInstance.protectedMethod();
};
expose('publicMethod');
};
});
Once a class is defined, invoke it like this:
var instance = classes.instantiate('myclass');
console.log(instance.publicMethod());
Output:
"Here you go: My Class is aw3s0m3!!1! bar baz"
It is also possible to wait for a class to be defined:
classes.once('myclass', function () {
var instance = classes.instantiate('myclass');
});
By default, classes are defined in a global namespace. However, you can easily create a new namespace if you want to ensure against class name collisions:
var myClassNamespace = classes.namespace();
myClassNamespace.define('myclass', [], function (thisClass) {
// ...
});
Multiple inheritance works by specifying an array of "superclasses" as the
second argument to .define()
, like so:
classes.define(
'myclass2',
['superclass1', 'superclass2'],
function (thisClass, protoClass) {
// protoClass is now inherits all the protected and public members of
// both superclass1 and superclass2. In the case of conflicts, the
// superclass specified *last* in the array wins.
// ...
}
);
It is possible to attach properties (as opposed to functions) to a class or instance. However, primitive properties (simple string, number or boolean values) are not recommended, because they will not stay in sync between class and superclass. Instead, use accessor functions, or set properties as on object or array that may have mutable members.
FAQs
JS class pattern; supports multi-inheritance, static & instance methods, public/protected access...
We found that classes-js 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.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.