简体   繁体   中英

Call a Javascript function and return a response

I recently started working with Javascript and I saw this question but there was no explanation for this question. I have the following Javascript code:

var A = (function(){
  function Person(name){
    this.name = name;
  }

  var m = function(){
    return "Hello " + this.name;
  };

  Person.prototype.getGreeting = m;

  return Person;
})();

What would I have to write so that it can call above code and have it return "Hello World"? I am still learning so pardon my ignorance.

This code defines a Person object as A , with a constructor that accepts the parameter to print in the "greeting". So immediately after this code you could write something like:

var somePerson = new A('world');
console.log(somePerson.getGreeting());

This would create an instance of A (which wraps Person ) in a variable called somePerson and would log the output of getGreeting() on that instance.

With a little more consistent spacing and indentation you can see the structure more clearly. Example .

  var A=(function(){ function Person(name){ this.name=name; } var m=function(){ return"Hello "+this.name; }; Person.prototype.getGreeting=m; return Person; })(); var B = new A(); B.name = "World"; console.log(B.getGreeting()); 

You can start simple and build up to the full example:

function Person(name){
  this.name = name;
}

var m = function(){
  return "Hello " + this.name;
};

Person.prototype.getGreeting = m;

var p = new Person('anybody');

console.log(p.getGreeting());

If you are coming from another programming language, a good book to get started with is "JavaScript: The Good Parts" by Douglas Crockford. After that, a good read is "Effective JavaScript" by David Herman. Those will answer your questions about the code example better than any of our answers here.

new A("World").getGreeting()

The definition of A creates a function and then immediately calls it. The function creates another function called Person. This function is a JavaScript constructor and can be used to create a new object via "new Person". The Person constructor accepts a name parameter and stores it internally. It also sets the getGreeting member of the Person function's prototype to a function that prints "Hello" followed by the name of the Person.

To use, do new A("World") to call the constructor A (which is really Person), passing in World, then call the getGreeting method of the resulting object, which will invoke the getGreeting function of the prototype.

Your code uses an idiom called an Immediately-Invoked Function Expression , which keeps the variables/functions Person and m private/hidden inside the scope of that anonymous function. You then immediately call the anonymous function, which sets up the Player class and then returns it. The result (the Player constructor itself) gets stored in A . (JavaScript doesn't actually have classes, but that's another discussion.)

To create an instance of this class, you'd do something like:

var joe = new A("Joe");

Then you could use the joe object (an instance of the Person / A class):

console.log(joe.getGreeting()); // Hello Joe

Full working example:

 var A = (function(){ function Person(name){ this.name = name; } var m = function(){ return "Hello " + this.name; }; Person.prototype.getGreeting = m; return Person; })(); var joe = new A("Joe"); console.log(joe.getGreeting()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM