3

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.

KernelPanic
  • 600
  • 8
  • 19
user1234
  • 145
  • 1
  • 12
  • Possible duplicate of [How to call a function in Javascript](http://stackoverflow.com/questions/18258579/how-to-call-a-function-in-javascript) – KernelPanic Feb 18 '17 at 04:14
  • @KernelPanic so it will be A("World"); right? – user1234 Feb 18 '17 at 04:14
  • Is your ultimate goal to write a hello world program? You can do that in one line: `console.log("Hello world");` – qxz Feb 18 '17 at 04:15
  • @qxz no that's not my goal.. I am just trying to understand functions in javascript so basis on my above code it will be called like this `A("World");` right? – user1234 Feb 18 '17 at 04:16
  • I'll write up an answer – qxz Feb 18 '17 at 04:17
  • I'll admit this example is a bit more complex than my suggested duplicate. If @qxz is writing up an answer, I'll retract my flag – KernelPanic Feb 18 '17 at 04:19

5 Answers5

2

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.

Jim Flood
  • 8,144
  • 3
  • 36
  • 48
1

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.

David
  • 208,112
  • 36
  • 198
  • 279
1

    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());
Igor
  • 15,833
  • 1
  • 27
  • 32
0
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.

Willis Blackburn
  • 8,068
  • 19
  • 36
0

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());
Community
  • 1
  • 1
qxz
  • 3,814
  • 1
  • 14
  • 29