javascript - Is there any reason to refactor declaration functions into an expression function? -


i have these 3 functions, different of them happen in same context, related, have components in commons (ex: class names).

function btnsubmitloading($btn, cleanerror, classtoremove) {     // 5-10 lines of code }  function btnsubmitcomplete($btn, classtoadd) {     // other 5-10 lines of code }  function btnfeedback($btn, $msg, classtoadd) {     // more 5-10 lines of code } 

they declared before $(document).ready(), global , called in different pages. when need 1 of them:

btnfeedback($('button'), 'success', 'fa-check'); 

my question here if there reason transform these functions expression function, resulting in this:

var buttonsubmit = function() {      this.btnsubmitloading = function($btn, cleanerror, classtoremove) {         // 5-10 lines of code     }      this.btnsubmitcomplete = function($btn, classtoadd) {          // other 5-10 lines of code     }      this.btnfeedback = function($btn, $msg) {         // more 5-10 lines of code     } } 

call it:

btnfancy = new buttonsubmit(); 

and when need it:

btnfancy.btnfeedback($('button'), 'success', 'fa-check'); 

i've been learning expression / declaration functions , closure / prototype, still don't understand should apply each of them.

thanks.

since buttonsubmit class/constructor function doesn't anything, that's pretty pointless. @ least you'd want use simplify use of object this:

let btnfancy = new buttonsubmit($('button')); btnfancy.feedback('success', 'fa-check'); 

(i don't know if makes sense use case or not.)

you'd implement this:

function buttonsubmit(button) {     this.button = button; }  buttonsubmit.prototype.submitloading = function (cleanerror, classtoremove) {     this.button....     // 5-10 lines of code }  buttonsubmit.prototype.submitcomplete = function (classtoadd) {     this.button....     // 5-10 lines of code }  buttonsubmit.prototype.feedback = function ($msg) {     this.button....     // 5-10 lines of code } 

if doesn't make lot of sense , want namespace functions, don't need function or new:

let btn = {     submitloading: function ($btn, cleanerror, classtoremove) {         // 5-10 lines of code     }     submitcomplete: function ($btn, classtoadd) {         // other 5-10 lines of code     },     feedback: function ($btn, $msg, classtoadd) {         // more 5-10 lines of code     } }  btn.feedback($('button'), ...); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -