Jakub Tarkowski
Jakub Tarkowski

Reputation: 3

Constructors similar to three.js ones

I create a simple library for drawing graphs of math functions. I would like to make a constructors similar to three.js ones.

var renderer = new THREE.WebGLRenderer();
var camera = new THREE.PerspectiveCamera();

For example, in my library to create a grid, you must write something like this:

var grid = new Charter.Grid();

I discovered that it can be done by Object.assign() method, but i don't know details of this solution. Therefore, I'd like to ask how to do this? I would be grateful for some examples of code.

Upvotes: 0

Views: 41

Answers (1)

georg
georg

Reputation: 215009

Simpler than you think.

var MYLIB = {};

MYLIB.whatever = function(x) {
  this.x = x;
}

//

obj = new MYLIB.whatever(42);
console.log(obj);

Do note, however, that this pattern is outdated, it's better to use modules (require() or import) in modern javascript.

Upvotes: 1

Related Questions