The front-end Xiaobai wrote a navigation bar component, and the seniors gave some advice to see how to improve it.

function Navigation (container) {

    this.defaultConfig = {
        container: container,
        prop: [
            {value: "0", color: "red"},
            {value: "1", color: "blue"},
            {value: "2", color: "blue"},
            {value: "3", color: "blue"}
        ],

        fontColor: "white",
        fontSize: "20px",
        align: "center",
    };


    this.init = function () {
        this.createList();
        this.defaultConfig.container.children[0].isClick = false;
        this.defaultConfig.container.children[0].addEventListener("click", function () {
            this.isClick = !this.isClick;
            if (this.isClick) {
                this.parentNode.style.height = this.offsetHeight + "px";
            }else{
                this.parentNode.style.height = 200 + "px";
            }
        })
    }


}

Navigation.prototype.createList = function () {
    for (var i = 0; i < this.defaultConfig.prop.length; iPP) {
        var div = document.createElement("div");
        div.innerHTML = this.defaultConfig.prop[i].value;
        div.style.color = this.defaultConfig.fontColor;
        div.style.fontSize = this.defaultConfig.fontSize;
        div.style.backgroundColor = this.defaultConfig.prop[i].color;
        div.style.textAlign = this.defaultConfig.align;
        div.style.width = 200 + "px";
        div.style.height = 50 + "px";
        div.style.lineHeight = 50 + "px";
        div.style.border = "1px solid";
        this.defaultConfig.container.appendChild(div);
    }

}

the front-end rookie, after learning JS for a period of time, only did the most important functions, but did not write some other small functions. I always feel that the code I write is very bad, but I don"t know how to improve it. I hope you can give me some guidance.


it seems that the subject is not a front end, so I recommend using jquery :

directly.

such as a navigation, that's pretty much the code

var ACTIVE_CLASS = 'is-active'
var $navLi = $('.nav-li');
$navLi.on('click', function() {
    $navLi.removeClass(ACTIVE_CLASS);
    $(this).addClass(ACTIVE_CLASS);
});

even with native code, it is recommended that after the navigation is activated, a class is added to the active navigation, and the style changes are written in the css to achieve the separation of action and style.

in addition, if you want to encapsulate components, front-end frameworks such as vue or react are recommended in 2019.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3fb8e-2bc31.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3fb8e-2bc31.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?