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 -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -

How to understand 2 main() functions after using uftrace to profile the C++ program? -