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 -

python - ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'> -

java - How to provide dependency injections in Eclipse RCP 3.x? -