How is the privileged method of closures implemented in js?

The

completion factory function meets the following requirements
saves the passed parameter name to a private variable
function returns an object, and the object has a privileged method getName, to return the value of the object"s private variable name
as follows:

var person = createPerson("Jero");
console.log("person.name);  //underfind
console.log("person.getName());  //Jero

I just started to learn the concept of closure, but I don"t quite understand the concept of privileged methods, so how can this be implemented to match the output of the two console.log above?
Thank you!

Mar.12,2021

function createPersonname{
  return {
    getName:function(){
      return name
    }
  }
}

right solution upstairs.

it is suggested that the subject should not pursue the answer to this question. It will be easy to read the concept of closure several times, understand it, do a few exercises, and then solve the problem.


function createPerson (name) {

    // name 
    var privateName = name;
    return {
        // name 
        getName : function getName(){
        return privateName;
        }
    }
}
var person = createPerson('Jero');
console.log(person.name); //undefined 
console.log(person.getName());//Jero
Menu