
Security News
Official Go SDK for MCP in Development, Stable Release Expected in August
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.
function.create
Advanced tools
Function.create allows you to create named functions easily in JavaScript.
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.
The npm package function.create receives a total of 4 weekly downloads. As such, function.create popularity was classified as not popular.
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.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.
Security News
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.