Installations
npm install function-create
Developer Guide
Typescript
No
Module System
CommonJS
NPM Version
1.3.14
Score
73.2
Supply Chain
98.4
Quality
75
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
dillonkrug
Download Statistics
Total Downloads
1,111
Last Day
1
Last Week
1
Last Month
12
Last Year
63
GitHub Statistics
MIT License
15 Commits
2 Watchers
1 Branches
1 Contributors
Updated on May 03, 2014
Bundle Size
2.23 kB
Minified
975.00 B
Minified + Gzipped
Package Meta Information
Latest Version
0.0.1
Package Id
function-create@0.0.1
Size
8.57 kB
NPM Version
1.3.14
Total Downloads
Cumulative downloads
Total Downloads
1,111
Last Day
0%
1
Compared to previous day
Last Week
-83.3%
1
Compared to previous week
Last Month
140%
12
Compared to previous month
Last Year
-42.2%
63
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads

No dependencies detected.
Function.create
Function.create(proto, fn, props) — Object.create() for functions
Just as you might create an object with a given prototype, this module allows you to create a function with a given prototype.
When used with two functions, if the first parameter of the second function is $super
, it will be automatically filled with a reference to the parent function, bound to the inheriting function's scope.
An example is probably better than trying to figure out what that actually means...
1 2var Parent = function(msg){ 3 this.field = msg 4} 5 6Parent.staticProperty = 'I\'m a test class!'; 7 8var Test = Function.create(Parent, function($super){ 9 $super('hello'); 10 this.field += ' world.'; 11}); 12 13console.log(Test.staticProperty); // -> logs: "I'm a test class!" 14 15var obj = new Test(); 16 17console.log(obj.field); // -> logs: "hello world." 18
Usage
Function.create(prototype, inputFunction, propertyDescriptor, preserveArity)
- prototype: the prototype for the target function
- inputFunction: the target function
- propertyDescriptor: a property descriptor (MDN Reference)
- preserveArity: if true, the name and parameters for the
inputFunction
will be preserved. NOTE this uses eval and is accordingly slower.
NOTE:
this is kinda wonky, but sometimes Function.create returns inputFunction
, and sometimes it is a new function (which calls inputFunction
).
The intended use is Function.create(proto, function literal(){ /* not a variable */ })
, so it shouldn't be a problem. I may standardize it in the future. for now it just does whatever will run faster. If the prototype
passed in is not a function, it will always return inputFunction
More Examples
1var fn = Function.create({ 2 propA: 'hello', 3}, function() { 4 return 'Hello World!'; 5}, { 6 own: { 7 value: 'test' 8 } 9}); 10 11fn(); // -> "Hello World" 12 13fn.hasOwnProperty('propA') // -> false 14fn.propA // -> "hello" 15 16fn.hasOwnProperty('own') // -> true 17fn.own // -> "test" 18
1 2var Fighter = function(skill, endurance, name, catchPhrase){ 3 this.skill = skill; 4 this.end = endurance; 5 this.str = 100; 6 this.exp = 0; 7 this.catchPhrase = catchPhrase; 8}; 9 10Fighter.prototype.attack = function(target){ 11 console.log(this.skill, this.skill*this.str, target.skill*target.str); 12 var won = this.skill*this.str > target.skill*target.str; 13 if(won) console.log(this.catchPhrase); 14 this.str -= Math.round(100/(this.end)); 15 this.exp++; 16 return won; 17}; 18 19var Ninja = Function.create(Fighter, function($super, name){ 20 $super(8, 4, name, '[ silence... ]'); 21}); 22 23var Pirate = Function.create(Fighter, function($super, name){ 24 $super(6, 6, name, 'Yarrrr!'); 25 this.booty = 0; 26}); 27 28Pirate.prototype.attack = Function.create(Pirate.prototype.attack, function($super, target){ 29 var won = $super(target); 30 this.booty += (won ? 1 : -1) * 25; 31}); 32 33var pirate = new Pirate('RedBeard'); 34 35var ninja = new Ninja('Fred'); 36 37 38ninja.attack(pirate); // -> logs: "[ silence... ]" 39pirate.attack(ninja); // [DRAW] 40ninja.attack(pirate); // -> logs: "[ silence... ]" 41pirate.attack(ninja); // -> logs: "Yarrrr!" 42ninja.attack(pirate); // -> logs: "[ silence... ]" 43pirate.attack(ninja); // -> logs: "Yarrrr!" 44ninja.attack(pirate); // [LOSS] 45pirate.attack(ninja); // -> logs: "Yarrrr!" 46 47console.log(ninja); // -> logs: { skill: 8, end: 4, str: 0, exp: 4, catchPhrase: '[ silence... ]' } 48console.log(pirate); // -> logs: { skill: 6, end: 6, str: 32, exp: 4, catchPhrase: 'Yarrrr!', booty: 50 } 49 50
Info
This is a little bit hackish. It uses Object.setPrototypeOf if it's available, but otherwise it sets the proto property directly which is not ideal.
My hope in creating this module is that it will eventually be replace by a built-in method.

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 0/15 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3
/10
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to function-create
@jest/create-cache-key-function
This module creates a function which is used for generating cache keys used by code transformers in Jest.
p-cancelable
Create a promise that can be canceled
omit.js
Utility function to create a shallow copy of an object which had dropped some fields.
lodash.create
The Lo-Dash function `_.create` as a Node.js module generated by lodash-cli.