R
R
RUSgrigoryev2014-04-30 23:24:16
JavaScript
RUSgrigoryev, 2014-04-30 23:24:16

Why is this code bad?

Below I wrote a code that, when you click on the inner element, hides the parent element. Question: is it one of the JS patterns or not?

<div id="first"><p class="inner">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fuga non sit ratione atque minus voluptates tempora aliquid aut perspiciatis impedit harum id et sed molestiae ad asperiores iusto nobis alias.</p></div>

<div id="second"><p class="inner2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatem odit molestias velit consectetur molestiae. Minima obcaecati similique odio incidunt recusandae consequuntur ipsam provident inventore nesciunt numquam dignissimos nihil harum eveniet.</p></div>

function closeElem (elem, parent) {
    var obj = {
        otps : {
            elem  : elem,
            parent: parent
        },
        init: function () {
            $(elem).on('click', function () {
                $(this).closest(parent).hide();
            });
        }
    };
    obj.init();
    return obj;
}

var closediv = closeElem('.inner', '#first');
var closediv = closeElem('.inner2', '#second');

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aumk, 2014-04-30
@aumk

This is the best code I have ever seen. BRAVO!

S
Sergey, 2014-04-30
Protko @Fesor

I don't even know where to start... Everyone... At least there is no point in the opts property, you already have the elem and parent variables available in the parent scope. They would use at least full-fledged objects, although again I don’t understand the meaning. Somehow too much code...
What should be in closedDiv? And why such difficulties? It seems to me that it is not the code that is bad, but the approach.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question