Research
Recent Trends in Malicious Packages Targeting Discord
The Socket research team breaks down a sampling of malicious packages that download and execute files, among other suspicious behaviors, targeting the popular Discord platform.
function.create
Advanced tools
Readme
This allows you to create named functions easily in JavaScript. Tested browsers:
It is similar to the sketched Function.create
in ECMAScript (two years old). The proposal was never accepted, so this function is not based on any standards, but it's still very useful.
Download:
Installing:
npm install function.create
Loading it in Node.JS:
require('function.create'); // now loaded globally
Loading it in the browser:
<script src="Function.create.js"></script>
Two functions are provided:
The proto
argument sets the prototype of the newly created function. This is available in all browsers except MSIE, where it is simulated by copying the whole prototype chain.
Example 1 (creating unnamed function):
var anon = Function.create(null, function(str) {
console.log('anon called:', str);
});
anon('Hello, anon!');
Example 2 (creating simple named function):
var simple = Function.create('simple', function(str) {
console.log('simple called:', str);
});
console.log(simple.name); // "simple"
simple('Hello, simple!');
Example 3 (creating simple named constructor):
var Constr = Function.create('Constr', function(n) {
this.n = n;
});
Constr.prototype.say = function(text) {
console.log('say: ' + text + ', ' + this.n + '!');
};
console.log(Constr.name); // "Constr"
var c = new Constr('simple named constructor');
c.say('Bonjour'); // say: Bonjour, simple named constructor!
Example 4 (creating named function and constructor):
var Person = Function.create('Person', function(name) {
return new Person(name);
}, function(name) {
this.name = name;
});
console.log(Person.name); // "Person"
var p1 = new Person('Bobby');
var p2 = Person('Bobby');
console.log('Same person?', p1.name === p2.name); // true
Example 5 (getting name of function):
var func = function fancyFunction() {};
console.log('Function name:', Function.getDisplayNameOf(func));
Example 6 (named classes):
function createClass(name, properties) {
var Class = Function.create(name, function() {
if (typeof(this.initialize) === 'function') {
this.initialize.apply(this, arguments);
}
});
Class.prototype = properties;
Class.prototype.constructor = Class;
return Class;
}
var Person = createClass('Person', {
initialize: function(name) {
this.name = name;
},
getName: function() {
return this.name;
}
});
var andy = new Person('Andy');
console.log(andy instanceof Person); // true
console.log(andy.getName()); // "Andy"
Example 7 (functor, or the story about function inheriting object):
function createFunctor(name, properties) {
return Function.create(name, function() {
if (typeof(this.invoke) === 'function') {
this.invoke.apply(this, arguments);
}
}, null, properties);
}
function Module() {}
Module.prototype = new Function();
Module.prototype.constructor = Module;
Module.prototype.say = function(message) {
console.log('I want to say: ' + message);
};
Module.prototype.invoke = function(a, b) {
this.say(a + ' + ' + b + ' = ' + (a + b));
return a + b;
};
var M = createFunctor('Module', new Module());
console.log(M.name); // "Module"
console.log(typeof(M)); // "function"
console.log(M instanceof Module); // true, in all browsers except MSIE
M(20, 22); // outputs "I want to say: 20 + 22 = 42"
FAQs
Function.create allows you to create named functions easily in JavaScript.
We found that function.create 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
The Socket research team breaks down a sampling of malicious packages that download and execute files, among other suspicious behaviors, targeting the popular Discord platform.
Security News
Socket CEO Feross Aboukhadijeh joins a16z partners to discuss how modern, sophisticated supply chain attacks require AI-driven defenses and explore the challenges and solutions in leveraging AI for threat detection early in the development life cycle.
Security News
NIST's new AI Risk Management Framework aims to enhance the security and reliability of generative AI systems and address the unique challenges of malicious AI exploits.