3

I've made my library and want to use these methods in my front-end app. How do I do this?

This is a simple front-end app with vanilla js, I'm not using any framework, neither nodejs...

this is my library antecipacao.js:

var _ = (function (val, parc, taxaMdr) {

    'use strict';
    var valor=val;
    var parcela=parc;
    var mdr=taxaMdr;
    valorParc = (valor - (((valor / parcela) * mdr)) * parcela) / parcela;
    valorTotalAntecipado = valorParc * parcela;
    valorParcela = 0

    // Create the methods object
    var methods = {};

    methods.parcelaCalculo = function (valor, parcelas, mdr, numero){
        return (valorParc - ((numero * mdr) * valorParc));
    }

    methods.umDia = function (valorTotalAntecipado, parcelas, mdr) {
        for (var i = 0; i < parcelas; i++) {
            if(i == 0){
                valorParcela += parcelaCalculo(valorParc,parcelas, (mdr / 30 * 29), i+1);
            }else if(i != 0){
                valorParcela += parcelaCalculo(valorParc,parcelas,mdr, i+1);
            }
        }
        return console.log(valorParcela);
    };

    methods.trintaDias = function (valorTotalAntecipado, parcelas, mdr) {
        for (var i = 0; i < parcelas; i++) {
            if(i > 0){
                valorParcela += parcelaCalculo(valorParc,parcelas,mdr, i);
            }else{
                valorParcela += valorParc;
            }
        }
        return console.log(valorParcela);
    };

    methods.quinzeDias = function (valorTotalAntecipado, parcelas, mdr) {
        for (var i = 0; i < parcelas; i++) {
            if(i == 0){
                valorParcela += parcelaCalculo(valorParc,parcelas,mdr, 0.5);
            }else if(i != 0){
                valorParcela += parcelaCalculo(valorParc,parcelas,mdr, i+0.5);
            }
        }
        return console.log(valorParcela);
    };

    methods.noventaDias = function (valorTotalAntecipado, parcelas, mdr) {
        for (var i = 0; i < parcelas; i++) {
            if(i > 3){
                valorParcela += parcelaCalculo(valorParc,parcelas,mdr, i);
            }else{
                valorParcela += valorParc;
            }
        }
        return console.log(valorParcela);
    };

    // Expose the public methods
    return methods;

})();

_.umDia();
_.quinzeDias();
_.trintaDias();
_.quarentaDias();

this is my js.js:



function calcular(){
  var valor = document.getElementById("valor").value;
  var parcelas = document.getElementById("parcelas").value;
  var mdr = document.getElementById("mdr").value;

  if(valor != "" && parcelas != "" && mdr != ""){
    console.log("test")
  }
}

So How could I use the library methods in my html page through js.js?

  • 4
    If you load both onto the page, just `js.js` *after* `antecipacao.js` -- You should be able to use methods from `antecipacao.js` within `js.js` -- Unless I am misunderstanding the question? – Zak Oct 15 '19 at 19:52
  • 1
    The comment above is the dead-simple way to do it. If you want to try something cutting-edge, you can use ES6 modules. See the [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) keyword for more info. – Romen Oct 15 '19 at 20:01
  • Yeah, i'm beginner on it. But are throwing another error now. 'valorParc' is not defined. You know how to fix? –  Oct 15 '19 at 20:07
  • 1
    you didn't define `valorParc` - just make the first place you refer to it define it as `var valorParc` – Always Learning Oct 15 '19 at 20:10
  • 1
    Normally if you omit the "var" assignment on a variable, the variable would be attached to the global scope. But since you're using 'use strict'; here, valorParc isn't being defined because you're not explicitly declaring it as a variable with var. – Thomas Preston Oct 15 '19 at 20:13

1 Answers1

1

I think the best way to it is to include your library in your html file like jQuery

<html>
<body>
  <script src='antecipacao.js'></script>
  <script src='myJS.js'></script>
</body>
</html>
Amir Makram
  • 12,030
  • 3
  • 13
  • 25