Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
construct
Advanced tools
Readme
npm install construct
construct(constructor, arg1, arg2, arg3);
// OR
construct(constructor, arguments);
// OR
construct(constructor, [arg1, arg2, arg3]);
Construct provides a way to create a new instance of an object with an arbitrary number of arguments.
Work-arounds tend to be clumsy, and/or clutter up code. This module aims to be a cure-all for obscure object creations.
Construct can be installed via NPM:
npm install construct
Or as an ender module:
ender add construct
Say you have a library that takes a constructor as an argument that you will construct later.
Everything is hunky-dory until you want to pass it some arguments. So you specify a few arguments manually, and it works, for a while.
But one day, your library gets popular. People start requesting additional numbers of parameters, and your parameter parsing ends up getting longer than your actual code.
With construct, you just pass it your constructor and the arguments. Construct doesn't look at the arguments and passes them exactly as you passed them in to the constructor, then hands you back a new instance.
This is a REAL new instance, meaning prototypes work as expected, and instanceof works too.
Look at the code. There's only three real lines of code:
function construct(constructor) {
function F(args) {
return constructor.apply(this, args);
}
F.prototype = constructor.prototype;
return new F(Array.prototype.slice.call(arguments, 1));
}
Since we can't call apply when using new, and new constructor(arguments) will require us to change the constructor, we have to be clever.
In order to achieve this, we'll chain the constructor to a new constructor. Calling Function.apply(this, arguments) calls che Function with the current context's prototype.
That's where F.prototype
comes into play. We'll assign the constructor's prototype to F's prototype before we construct it.
Array.prototype.slice.call(arguments, 1)
This one's a little confusing at first glance to people not familiar with it. Array.slice basically loops through the array using the length property and array indexing ([]
). This means that any object with a length property can be turned into an array by lying to the slice method, and that's exactly what we're doing.
constructor.apply(this, args);
Now we can use the same idea with apply. Since we called F with new
, the this
context is now a newly constructed object. Callyng apply with this context will chain these two functions together, but since F has constructor's prototype, constructor thinks that it just got called normally with manually passed in parameters.
Cool huh?
FAQs
Calls a constructor with an arbitrary number of arguments.
The npm package construct receives a total of 1,014 weekly downloads. As such, construct popularity was classified as popular.
We found that construct demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.