Why is this code not working?

function Person(name){ this.name = name; } Person.prototype.greet = function(otherName){ return "Hi " + otherName + ", my name is " + name; } 

Closed due to the fact that off-topic participants Streletz , aleksandr barakin , Dmitriy Simushev , cheops , D-side 3 Jun '16 at 12:42 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Streletz, aleksandr barakin, cheops, D-side
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • "predictable" - by whom? - Igor
  • one
    And what do you want? - andreymal
  • 2
    And on the topic - the name variable in the greet function does not exist - andreymal
  • one
    apparently an old problem: stackoverflow.com/questions/17713630/… - MasterAlex
  • one
    @andreymal, but there is a global one. Yes, and with super fiction - saved when the page is updated) - Qwertiy ♦

1 answer 1

 Person.prototype.greet = function(otherName){ return "Hi " + otherName + ", my name is " + this.name; } 

Missing this keyword

 var dave = new Person("Dave"); var hello = dave.greet("Alan"); // "Hi Alan, my name is Dave"; 
  • damn, everything was so simple - darkwoolf
  • somehow did not immediately notice - darkwoolf