Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Constructor function event listener returning 'undefined'

I have a constructor function that has one event listener attached. I wrapped that event listener in an IIFE, and stored that in a variable called 'init' - I then return an object that holds the init function.

function TabFilter(props) {
    this._el = props.el;
    this._content = props.content;
    this._props = props;
    this._parent_element = props.parentElement;
    var parentElement = document.querySelector(this._parent_element);
    var tab = parentElement.querySelectorAll(this._el);
    var tiles = parentElement.querySelectorAll(this._content);
    var self = this;

    var init = (function() {

        tab.forEach(function(tab) {

            tab.addEventListener('click', function(e) {
                e.preventDefault();
                var clickedTabAttr = tab.getAttribute('data-category');
                tiles.forEach(function (tile) {
                    var tileAttr = tile.getAttribute('data-category');
                    if (clickedTabAttr === 'all') {
                        tile.classList.remove('hide');
                    } else if (clickedTabAttr === tileAttr) {
                        tile.classList.remove('hide');
                    } else {
                        tile.classList.add('hide');
                    }
                });

            }.bind(self));

        });

    })();

    return {
        init: init
    }



}

var TileTab = new TabFilter({
    parentElement: '#tile-wrap',
    el: '.tab',
    content: '.item-container'
});

var TileTab2 = new TabFilter({
    parentElement: '#tile-wrap2',
    el: '.tab',
    content: '.item-container'
});

var TileTab3 = new TabFilter({
    parentElement: '#img-wrap',
    el: '.tab',
    content: '.img'
});

It actually works perfectly fine. However, I checked the 'tileTab' variable in the console, and the new instance says the init variable returned is 'undefined' - Why is that?

I'm following the "Revealing Prototype Pattern" design pattern

https://codepen.io/Woodenchops/pen/VOxVPB?editors=0010

Thanks

1 Answer

Steven Parker
Steven Parker
243,134 Points

The IIFE that provides the value to initialize "init" doesn't seem to return anything, so by default the value would be "undefined".

Also, the IIFE doesn't seem to be necessary since all it does is call "tab.forEach". But while that would simplify the code, it still wouldn't affect the value in "init", since a "forEach" returns "undefined".

What were you intending for "init" to have?