1

I created a simple object that has a method to return some calculation but it doesn't work.

I declared the object with its properties and method as in the attached code.

<p id="demo"></p>
<script>
    var person = new Object();
    person.firstName= "John";
    person.lastName= "Doe";
    person.age=40;
    person.retireAtAge= function (){ return (this.age+25);};
    document.getElementById("demo").innerHTML=person.firstName + " Retires at " + person.retireAtAge;
 </script>

The expected output is: John Retires at 65

but I get now is: John Retires at function (){ return (this.age+25);}

1 Answer 1

3

You need to call the method by putting () at the end of function variable.

document.getElementById("demo").innerHTML=person.firstName + " Retires at " + person.retireAtAge();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.