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

'hasOwnProperty' in javascript -

c# - Update a combobox from a presenter (MVP) -

android - Unable to generate FCM token from dynamically instantiated Firebase -