Javascript constructor functions different patterns -


this question has answer here:

what's difference between 2 code snippets below. understand second 1 uses iife, unable understand what's benefit of 1 on other. can please explain.

//first******* var student=function student(name) {     this.name = name; } student.prototype.printmessage = function () {     console.log(this.name); }; var st = new student("test"); st.printmessage();  //second** var student = (function () { function student(name) {     this.name = name; } student.prototype.printmessage = function () {     console.log(this.name); }; return student; }()); var st = new student("test"); st.printmessage(); 

in simple example, there no advantage using iife. purpose of such construction if had variables other student didn't want expose global scope.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -