Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
IIFE to class in javascript

Having a little trouble with modules / IIFE etc. I have a script that used to be an IIFE and uses a lot of this key word etc.. I am trying to turn it into a module.

(function () {
  var _symbol = d3.svg.symbol(),
    _line = d3.svg.line();

  d3.superformula = function () {
    var type = _symbol.type(),
      size = _symbol.size(),
      segments = size,
      params = {};

    function superformula(d, i) {
      var n,
        p = _superformulaTypes[type.call(this, d, i)];
      for (n in params) p[n] = params[n].call(this, d, i);
      return _superformulaPath(
        p,
        segments.call(this, d, i),
        Math.sqrt(size.call(this, d, i))
      );
    }

    superformula.type = function (x) {
      if (!arguments.length) return type;
      type = d3.functor(x);
      return superformula;
    };

    // size of superformula in square pixels
    superformula.size = function (x) {
      if (!arguments.length) return size;
      size = d3.functor(x);
      return superformula;
    };

    // number of discrete line segments
    superformula.segments = function (x) {
      if (!arguments.length) return segments;
      segments = d3.functor(x);
      return superformula;
    };

    return superformula;
  };

  function _superformulaPath(params, n, diameter) {
    var i = -1,
      dt = (2 * Math.PI) / n,
      t,
      r = 0,
      x,
      y,
      points = [];

    while (++i < n) {
      t = (params.m * (i * dt - Math.PI)) / 4;
      t = Math.pow(
        Math.abs(
          Math.pow(Math.abs(Math.cos(t) / params.a), params.n2) +
            Math.pow(Math.abs(Math.sin(t) / params.b), params.n3)
        ),
        -1 / params.n1
      );
      if (t > r) r = t;
      points.push(t);
    }

    r = (diameter * Math.SQRT1_2) / r;
    i = -1;
    while (++i < n) {
      x = (t = points[i] * r) * Math.cos(i * dt);
      y = t * Math.sin(i * dt);
      points[i] = [Math.abs(x) < 1e-6 ? 0 : x, Math.abs(y) < 1e-6 ? 0 : y];
    }

    return _line(points) + "Z";
  }

  var _superformulaTypes = {
    asterisk: { m: 12, n1: 0.3, n2: 0, n3: 10, a: 1, b: 1 },
    bean: { m: 2, n1: 1, n2: 4, n3: 8, a: 1, b: 1 },
  };

  d3.superformulaTypes = d3.keys(_superformulaTypes);
})();

I am getting confused with how to define the constructor if I use class, or es6 would also works if someone can help But I am not sure how to go about it. If someone can help with the same that would be great. Thanks

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!