Ramda.js R.construct() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

Wraps a constructor function inside a curried function that can be called with the same arguments and returns the same type (official Ramda docs). You drop new at the call site so the factory fits map, compose, and friends.

Stable since v0.1.0: R.construct is a core Ramda helper — not deprecated, experimental, or non-standard. For variadic constructors (fn.length === 0), use R.constructN instead.

Syntax

Call R.construct. Start with this line.

Ramda
R.construct(fn)
CallWhat it does
R.construct(fn)Primary signature.
R.construct(...)Call the function helper.

Prefer import { construct } from "ramda" when you want a named import.

Library declarations
Ramda
R.construct(fn)

Parameters

ParameterDescription
fnRequired. Constructor to wrap; arity from fn.length

Return Value

Return ValueDescription
*Curried factory; instance when fully applied

Ramda Examples

Example 1 — Official AnimalConstructor

Wrap the constructor, call without new, then use a prototype method.

Ramda
function Animal(kind) {
  this.kind = kind;
}
Animal.prototype.sighting = function () {
  return "It's a " + this.kind + "!";
};

const AnimalConstructor = R.construct(Animal);
const pig = AnimalConstructor('Pig');

console.log(pig.kind);
console.log(pig.sighting());
Try It Yourself

How It Works

  1. Wrap once. R.construct(Animal) reads Animal.length (1) and returns a unary factory.
  2. Call without new. AnimalConstructor('Pig') builds a real Animal instance.
  3. Prototype stays. sighting still lives on Animal.prototype.
  4. Same type. You get the same kind of object as new Animal('Pig').

Example 2 — Official Map of Sightings

Compose the factory with R.invoker, then map over kinds — second half of the docs sample.

Ramda
function Animal(kind) {
  this.kind = kind;
}
Animal.prototype.sighting = function () {
  return "It's a " + this.kind + "!";
};

const AnimalConstructor = R.construct(Animal);
const animalTypes = ['Lion', 'Tiger', 'Bear'];
const animalSighting = R.invoker(0, 'sighting');
const sightNewAnimal = R.compose(animalSighting, AnimalConstructor);

console.log(R.map(sightNewAnimal, animalTypes).join('\n'));
Try It Yourself

How It Works

  1. Build first (right). AnimalConstructor turns each kind into an instance.
  2. Call the method (left). R.invoker(0, 'sighting') runs sighting() with no extra args.
  3. Map the pipeline. R.map repeats that for Lion, Tiger, and Bear.
  4. Why construct matters. You cannot pass new Animal into map — new is not a first-class function.

Example 3 — Curried Two-Argument Constructor

A Point takes x and y — pass them together or one at a time.

Ramda
function Point(x, y) {
  this.x = x;
  this.y = y;
}

const MakePoint = R.construct(Point);

console.log(JSON.stringify(MakePoint(3)(4)));
console.log(JSON.stringify(MakePoint(3, 4)));
Try It Yourself

How It Works

  1. Arity 2. Point.length is 2, so the factory is binary and curried.
  2. Partial call. MakePoint(3) is still a function waiting for y.
  3. Full apply. The instance appears only after both args arrive.
  4. Either style. (3)(4) and (3, 4) build the same point.

Example 4 — R.construct vs new

Same constructor, two call styles — matching kind and instanceof.

Ramda
function Animal(kind) {
  this.kind = kind;
}

const AnimalConstructor = R.construct(Animal);
const a = new Animal('Pig');
const b = AnimalConstructor('Pig');

console.log(a.kind === b.kind);
console.log(a instanceof Animal);
console.log(b instanceof Animal);
Try It Yourself

How It Works

  1. Same constructor. Construct does not replace Animal — it wraps it.
  2. new inside. The factory still uses new under the hood.
  3. Same type. Both results are instanceof Animal with kind === 'Pig'.
  4. When to choose. Use construct when the call site must be a function; use new for a one-off instance.

Example 5 — R.construct vs R.constructN

A variadic Salad has length 0 — constructN is the documented fix.

Ramda
function Salad() {
  this.ingredients = Array.prototype.slice.call(arguments);
}

const byFnLength = R.construct(Salad);
const threeLayer = R.constructN(3, Salad);

console.log(JSON.stringify(byFnLength('Mayo', 'Chips', 'Ketchup').ingredients));
console.log(JSON.stringify(threeLayer('Mayonnaise')('Potato Chips')('Ketchup').ingredients));
Try It Yourself

How It Works

  1. Length 0 trap. Rest-only constructors report length 0.
  2. Construct misfires. It calls new Salad() with no args — extras never arrive → [].
  3. ConstructN sets arity. R.constructN(3, Salad) waits for three ingredients.
  4. Rule of thumb. Named params → construct; variadic → constructN.

Browser & Runtime Support

R.construct() is ordinary JavaScript once Ramda is loaded. It needs no special browser API — only a JS runtime that can run Ramda (v0.1.0+).

✓ Ramda 0.1.0+ · Any JS runtime

Ramda.js R.construct()

Works in every modern browser and runtime where you can load Ramda (CDN or npm). Internet Explorer needs a build that targets that environment; there is no native IE API to polyfill.

100% With Ramda loaded
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
R.construct() Universal

Bottom line: Safe in any project that includes Ramda 0.1.0 or later. Load ramda.min.js from a CDN in the browser, or npm install ramda for Node, Bun, and Deno.

Frequently Asked Questions

R.construct(fn) wraps a constructor function in a curried factory. You call the factory with the same arguments the constructor expects and get back an instance of the same type — without writing new at the call site.
Wrap Animal with R.construct, then AnimalConstructor('Pig') is an Animal with kind Pig. Mapping R.compose(R.invoker(0, 'sighting'), AnimalConstructor) over Lion, Tiger, and Bear yields It's a Lion!, It's a Tiger!, and It's a Bear!. Added in v0.1.0. See also R.invoker.
Not at the call site. R.construct uses new for you when every argument is supplied. AnimalConstructor('Pig') is the same kind of object as new Animal('Pig').
R.construct reads fn.length to decide how many arguments to wait for. That fails for variadic constructors whose length is 0. R.constructN(n, fn) lets you set the arity yourself, like the official three-layer Salad sample.
Yes. The factory waits for fn.length arguments, one call or several. A two-argument Point can be MakePoint(3)(4) or MakePoint(3, 4). The result is a real instance, not another function.
No. R.construct is a stable Ramda function helper from v0.1.0. It is not deprecated, experimental, or non-standard.

Next: R.constructN()

Wrap a constructor with an explicit arity — the fix for variadic Salad-style constructors.

R.constructN() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful