Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Simple but powerful classical inheritence for JavaScript.
$ npm install classes
If using Node.js, you may want to make classes global using code such as this near the beginning of your application.
global.Class = require('classes').Class;
// If using Node.js, the module needs to be required (unless global as shown above)
var Class = require('classes').Class;
// A general animal class
Class('Animal', {
construct: function(name) {
this.name = name;
},
makeNoise: function(noise) {
alert(noise);
}
});
// Create a class for making people
Class('Person').Extends('Animal', {
speak: function(sayWhat) {
this.makeNoise(this.name + ' says: "' + sayWhat + '"');
}
});
// Create an instance of our person class
var james = new Person('James');
// And make them talk
james.speak('Hello, World');
There are multiple allowed syntaxes for extending classes, which you can choose at your preference.
// Create a base class first
Class('A', {
...
});
// Using the Extends() syntax
Class('B').Extends(A, {
...
});
// Using the complex Class() syntax
Class('B', A, {
...
});
// Using the extend() syntax
A.extend('B', {
...
});
Also note, that when using the Extends()
syntax or the complex Class()
syntax, both a parent class variable (A
) or string ('A'
) is allowed, but if you use the string syntax, the parent class must exist on the namespace object.
You can call the super of any method at any time. This is done using the parent
method on your class methods.
Class('A').Extends(SomeOtherClass, {
method: function() {
// Call the method's super
this.method.parent(this);
},
methodWithArgs: function(arg1) {
// Call this method's super, passing the argument along
this.methodWithArgs.parent(this, arg1);
},
anotherMethod: function() {
// Call the super, but pass in the arguments object
this.anotherMethod.parentApply(this, arguments);
},
});
Using class.js, classes don't have to be assigned a name. You can also tell the Class()
function to simply return the constructed class function by passing a falsey first param (like Class(null)
) or by simply not giving one as seen below.
// This class will automatically be declared at global.Animal
Class('Animal', {
...
});
// This class will not be declared globally, but instead just returned
var Snake = Class().Extends(Animal, {
...
});
// A slightly cleaner syntax
var Snake = Animal.extend({
// ...
});
To define a class, but assign it somewhere other that the global object, you pass in a two key array as the class name. The first value is the object to define the class on, and the second is the class name.
// This is where we will put the class
var someObject = { };
// Now define the class
Class([someObject, 'Animal'], {
...
});
// And use the class
var animal = new someObject.Animal();
This is equivilent to the following:
someObject.Animal = Class({
...
});
As of version 0.2.0, mixins are supported. It should be noted that mixins are not the same as sub-class inheritence. A single class can implement both a parent class as well as mixins. Mixins are defined using the Class.mixin
method.
Class.mixin('CanFoo', {
foo: function() {
alert('Foo!');
}
});
Once created, a mixin is used with the uses
method when defining a class.
Class('Thing').Uses([ 'CanFoo' ], {
// ...
});
var thing = new Thing();
thing.foo();
Mixins are different from inheritence in the sense that they do no add to the inheritence chain, they simply extend the current class with certain functionality. You cannot use instanceof
to determine mixin inheritence because classes are not instances of mixins; In fact, there is no such thing as an instance of a mixin, they are just objects.
Before version 0.2.0, all new classes and mixins were defined, by default, on the global object, and if you wanted to define one elsewhere, you would have to use either anonymous classes or the array syntax (eg. Class([exports, 'Foo'], ...)
). There is now a new way of defining namespaced classes that should prove useful, especially in the case of Node.js.
var Class = require('classes').Class;
Class.namespace(exports);
Class('Foo', {
// ...
});
var foo = new exports.Foo();
The Class.namespace()
function sets the default namespace, allowing shorter, more readable class declarations. Simply call Class.namespace(exports)
at the top of your modules and your classes will automatically be defined in the correct space.
FAQs
A classical inheritence model with support for mixins
The npm package classes receives a total of 0 weekly downloads. As such, classes popularity was classified as not popular.
We found that classes 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
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.