Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda