Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Is this a good practice?

In our company we're starting a new web project, I'm new in the company, and there are previous apps where document ready is made like this. (BTW we're using jQuery)

var AccountingMovements = function AccountingMovements() {

    //sometimes they use var _that = this;
    var _this = this;

    _this.Constructor = function Constructor() {
        //here goes the code when document is ready
    }
}

var $AccountingMovements = new AccountingMovements();
$(document).ready($AccountingMovements.Constructor);

When I ask my coworkers why they use this pattern, they tell me that Javascript code must have they're own constructor, and that code should be wrriten like this, and when I ask if it's a company rule to do it, they say it is not, there's no company standard that states this should be written like this. It seems they just do it this way because "It's how it has always be done", rather than saying that is a common javascript standarized practice or something similar.

By my side I learned to use IIFE with jquery document ready, which I found in many web examples given by javascript gurus, the actual explanation of use, and even a Wikipedia article. I write my code this way

(function (code) {
    code(window.jQuery, window, document);
}(function ($, window, document) {
    $(function () {
        //here goes the code when document is ready
    });
}));

My development leader (which doesn't not know too much about javascript, because he comes from a desktop-development background, but he is good when it comes to business logic) told me, that because it's a new project, whenever I see a new solution, a better implementation of something, or something helpful to work with, I must not hesitate to tell him, and he will discuss it with the other leaders. So I told him about my approach and that I didn't understand the purpose of the old constructor code, and that nobody gave me a convincing explanation, he told me to look up by myself if there's something that backs up that approach given that I showed him resources that backup mine, in order to tell the orders in case mine is better, throwing away the other method.

So after all this story, my question is,

Does the javascript constructor approach have a name? I mean, Is it an actual standard, practice or recommendation? Because I could not find any information about it. It could be that I'm wrong and the constructor approach is better or valid, but because I have no info about it, I need to be sure in order to discuss my implementation.

Thanks in advance.

like image 678
jecarfor Avatar asked May 19 '26 14:05

jecarfor


1 Answers

There is no special meaning to the word Constructor in the JavaScript language; only what meanings your coworkers have been assigning to it. Some JS frameworks like dojo create similarly named functions, but they also have specialized, central ways of defining objects, and centralized places they call said constructor function (instead of manually calling it on the very next line, leaving room for errors)

It's also perhaps misleading to believe that any JavaScript objects must wait until the DOM is ready before constructing themselves. How do you think JQuery creates itself? (And it has to very early, before people write $().ready to make their onload function)

I don't know of any specific issue with the way that your coworkers are writing their objects, as long as they assign a very different semantic meaning to their current Constructor function. The code above such as var _this = this is the constructor. The contents of the was-Constructor function could best be described onDOM or similar. If they'd prefer to follow this system, it might be best if their class system was going through a central library; be it your own or someone else's.

like image 102
Katana314 Avatar answered May 22 '26 02:05

Katana314