0

CASE 1:

function Test(name){
   var privateName = name;
   this.getName = function(){
      return privateName;
   }
}

var name = new Test("unknown");
console.log(name.getName())

CASE 2:

function getName(pName){
    var name = pName;
    return name;
}

var name = getName("unknown")
console.log(name)

It seems both are doing the same thing returning the name passed to the function. Then what is the difference between the two implementations? What are we trying to achieve in CASE 1?

  • I would ask the question "What are we trying to achieve in CASE 2?". Because in case one you set the name when instantiating the object (which is a common practice) but in case to you give the function a name and just return it which is sort of a futile attempt! – Iman Tahriri Jun 13 '21 at 08:43

0 Answers0