I would like suggestions about this structure in JS

Asked

Viewed 40 times

1

Good afternoon. Practicing a little, I decided to build a javascript slider, and chose to follow this construction line with a builder. The code is functional. However, I would like to encapsulate as much as possible the entire code structure. But I could not fit the following section inside the body of the object:

novoSlider.btnProx.onclick = function () {
    novoSlider.prox();
}
novoSlider.btnPrev.onclick = function () {
    novoSlider.prev();
}

Complete code:

<script>
    var sliderJs = function () {
        var elements = [];
        var pos = 0;
        var btnProx;
        var btnPrev;

        this.construtor = function (proxId, prevId, objJson) {
            this.btnProx = document.getElementById(proxId);
            this.btnPrev = document.getElementById(prevId);
            elements = objJson;
            container.childNodes[1].textContent = elements[pos].titulo;
            container.style.transition = "all .3s";
            container.style.backgroundImage = "url("+elements[pos].url+")";
        };

        this.prox = function () {
            if(pos != elements.length-1){
                pos = pos + 1;
                container.childNodes[1].textContent = elements[pos].titulo;
                container.style.backgroundImage = "url("+elements[pos].url+")";
            }else{
                pos = 0;
                container.childNodes[1].textContent = elements[pos].titulo;
                container.style.backgroundImage = "url("+elements[pos].url+")";
            }   
        };
        this.prev = function () {
            if(pos != 0){
                pos = pos -1;
                container.childNodes[1].textContent = elements[pos].titulo;
                container.style.backgroundImage = "url("+elements[pos].url+")";
            }else{
                pos = elements.length-1;
                container.childNodes[1].textContent = elements[pos].titulo;
                container.style.backgroundImage = "url("+elements[pos].url+")";
            }
        };
    };


    window.onload = function () {

        var objElements = [
            {titulo : "Jogador é demitido",
            url : "http://4.bp.blogspot.com/-Vp4z1HqcVWQ/VfcI2mUUjSI/AAAAAAAABMQ/t10j6_O6lEE/s300-c/infidelidade2.jpg" },
            {titulo : "Cunha abre processo de impeatchment",
            url : "http://www.atoananet.com.br/links/2015/08/11/artista-tranforma-fotos-aleatorias-em-divertidas-ilustracoes_300.jpg" },
            {titulo : "MegaSena acumulada", 
            url : "http://1.bp.blogspot.com/-8rt58bwMhTs/U_1SXIg8FSI/AAAAAAAAEMY/A3DZRYdMGSY/s300-c/cat01.jpg" }
        ];

        var novoSlider = new sliderJs();
        novoSlider.construtor("prox","ant",objElements);

        novoSlider.btnProx.onclick = function () {
            novoSlider.prox();
        }
        novoSlider.btnPrev.onclick = function () {
            novoSlider.prev();
        }
    }
</script>
  • The variable container is it global? it comes from where?

  • 1

    Regarding your question you could ask this.btnPrev.addEventListener('click', this.prev); and the same for the prox

  • It worked. I even tested this, but instead of clicking, I put onclick. Thanks. And container, it’s an element that will contain the slider. it is not directly defined in the code pq the reference is being made directly to the elementc id (as if it were a variable, and it is) without getElementById. Thank you!

1 answer

1


From what I see in your code you can improve some things.

You don’t need this constructor logic. You can directly use these arguments when you call new sliderJs.

I joined the container as argument and removed some duplicate code...

 var sliderJs = function(proxId, prevId, elements, container) {
     var pos = 0;
     this.container = container;
     this.btnProx = document.getElementById(proxId);
     this.btnPrev = document.getElementById(prevId);
     this.container.childNodes[1].textContent = elements[pos].titulo;
     this.container.style.transition = "all .3s";
     this.container.style.backgroundImage = "url(" + elements[pos].url + ")";

     this.prox = function() {
         pos = pos != elements.length - 1 ? pos + 1 : 0;
         this.container.childNodes[1].textContent = elements[pos].titulo;
         this.container.style.backgroundImage = "url(" + elements[pos].url + ")";

     };
     this.prev = function() {
         pos = pos != 0 ? pos - 1 : elements.length - 1;
         this.container.childNodes[1].textContent = elements[pos].titulo;
         this.container.style.backgroundImage = "url(" + elements[pos].url + ")";
     };
     this.btnProx.addEventListener('click', this.prox);
     this.btnPrev.addEventListener('click', this.prev);
 };


 window.onload = function() {

     var objElements = [{
         titulo: "Jogador é demitido",
         url: "http://4.bp.blogspot.com/-Vp4z1HqcVWQ/VfcI2mUUjSI/AAAAAAAABMQ/t10j6_O6lEE/s300-c/infidelidade2.jpg"
     }, {
         titulo: "Cunha abre processo de impeatchment",
         url: "http://www.atoananet.com.br/links/2015/08/11/artista-tranforma-fotos-aleatorias-em-divertidas-ilustracoes_300.jpg"
     }, {
         titulo: "MegaSena acumulada",
         url: "http://1.bp.blogspot.com/-8rt58bwMhTs/U_1SXIg8FSI/AAAAAAAAEMY/A3DZRYdMGSY/s300-c/cat01.jpg"
     }];

     var novoSlider = new sliderJs("prox", "ant", objElements, document.getElementById('container'));
 }
  • Looks like this code isn’t working. "Uncaught Typeerror: Cannot read Property 'childNodes' of Undefined" The container variable is pointing to null.

  • 1

    @Ericklemos in my answer I used logic this.container. If you want to keep using with you had the container you must change the code this.container for only container. If it still doesn’t work do a jsFiddle that I help fix.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.