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

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -