Teammates: JS: Avoid using jquery object multiple times, extract as variable

Created on 25 Aug 2017  路  4Comments  路  Source: TEAMMATES/teammates

when using a jQuery object multiple times (as is the case here for $(entry)), it is preferable to extract the jQuery object as a variable first (prefixing the variable name with a $ to indicate it is a jQuery object), and then reuse that variable in later calls eg:

Expected:

         const $entry = $(e.target); 
         const courseId = $entry.data('courseid'); 
         const studentEmail = $entry.data('studentemail'); 
         const googleId = $entry.data('googleid'); 

Current:
https://github.com/TEAMMATES/teammates/blob/cb4f03f4071ac4f37e5a1d683e5587508f0a622c/src/main/webapp/dev/js/main/adminSearch.es6#L125-L128

The main difference between declaring entry = e.target, followed by using $(entry) later, and declaring $entry = $(e.target) and reusing $entry would be efficiency (no need to create the same object multiple times in the latter case).

help wanted p.Low

Most helpful comment

The scope of this issue can be widened and similar violations can be fixed in the same PR.

All 4 comments

Can I take this?

@AlexM9200 I think this is a d.FirstTimer issue. Let's wait for the confirmation from any of our @TEAMMATES/seniordevs

This is the same thing in teammates/src/main/webapp/dev/js/main/instructorFeedbackResultsQuestion.es6.
Should I fix it in this pull request or create a new issue?

    const panelHeading = $(this);
    if ($('#show-stats-checkbox').is(':checked')) {
        $(panelHeading).find('[id^="showStats-"]').val('on');
    } else {
        $(panelHeading).find('[id^="showStats-"]').val('off');
    }

    const displayIcon = $(this).find('.display-icon');
    const formObject = $(this).children('form');
    const panelCollapse = $(this).parent().children('.panel-collapse');

The scope of this issue can be widened and similar violations can be fixed in the same PR.

Was this page helpful?
0 / 5 - 0 ratings