0

I have tried to create a object using function where in the function I declared the variables and referenced using this operator then object is created for that function and values were passed then I print the message

<body>
<p id="fun"></p>
<script>
    function func(name,age){
        this.name=name;
        this.age=age;
    }
    var fun=func("superman",211)
    document.getElementById("fun").innerHTML="name is : "+fun.name + " age is "+fun.age


 </script>
   </body>

I am getting Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'name') at (c:\Users\HARISH A K\Desktop\internPandM\js basics\objectLiteral.html:36:67) for the code

How I can resolve this. I need clarity why this error appears.

0 Answers0